]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Further improvements to array comma linter rule
authorJoshua Spence <josh@joshuaspence.com>
Tue, 19 May 2015 21:05:34 +0000 (07:05 +1000)
committerJoshua Spence <josh@joshuaspence.com>
Tue, 19 May 2015 21:05:34 +0000 (07:05 +1000)
Summary:
Improve the `ArcanistXHPASTLinter::LINT_ARRAY_SEPARATOR` rule to be able to autofix the following code:

```lang=php
array(
  1,
  2,
  3, /* comment */ );
```

Test Plan: Added unit tests.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: epriestley

Differential Revision: https://secure.phabricator.com/D12308

.editorconfig
src/lint/linter/ArcanistXHPASTLinter.php
src/lint/linter/__tests__/xhpast/array-comma.lint-test

index 0b1c0e67cfbf36e43a079a4413a76a8ade4a6e52..00f7d7c016c9a76aea139e25cc878133d0b515a9 100644 (file)
@@ -13,7 +13,7 @@ max_line_length = 80
 indent_style =
 end_of_line =
 max_line_length =
-trim_trailing_whitespace =
+trim_trailing_whitespace = false
 
 [src/parser/__tests__/bundle/*]
 insert_final_newline = false
index 68161aef4e5f0e0f63da7e48383139d0a2951821..1ebf829fe51a5415275f5d58a83beeddfdeae2ea 100644 (file)
@@ -3223,29 +3223,39 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
       $value = last($values);
       $after = last($value->getTokens())->getNextToken();
 
-      if ($multiline && (!$after || $after->getValue() != ',')) {
-        if ($value->getChildByIndex(1)->getTypeName() == 'n_HEREDOC') {
-          continue;
-        }
+      if ($multiline) {
+        if (!$after || $after->getValue() != ',') {
+          if ($value->getChildByIndex(1)->getTypeName() == 'n_HEREDOC') {
+            continue;
+          }
 
-        list($before, $after) = $value->getSurroundingNonsemanticTokens();
-        $after = implode('', mpull($after, 'getValue'));
+          list($before, $after) = $value->getSurroundingNonsemanticTokens();
+          $after = implode('', mpull($after, 'getValue'));
 
-        $original = $value->getConcreteString();
-        $replacement = $value->getConcreteString().',';
+          $original = $value->getConcreteString();
+          $replacement = $value->getConcreteString().',';
 
-        if (strpos($after, "\n") === false) {
-          $original    .= $after;
-          $replacement .= rtrim($after)."\n".$array->getIndentation();
-        }
+          if (strpos($after, "\n") === false) {
+            $original    .= $after;
+            $replacement .= $after."\n".$array->getIndentation();
+          }
 
-        $this->raiseLintAtOffset(
-          $value->getOffset(),
-          self::LINT_ARRAY_SEPARATOR,
-          pht('Multi-lined arrays should have trailing commas.'),
-          $original,
-          $replacement);
-      } else if (!$multiline && $after && $after->getValue() == ',') {
+          $this->raiseLintAtOffset(
+            $value->getOffset(),
+            self::LINT_ARRAY_SEPARATOR,
+            pht('Multi-lined arrays should have trailing commas.'),
+            $original,
+            $replacement);
+        } else if ($value->getLineNumber() == $array->getEndLineNumber()) {
+          $close = last($array->getTokens());
+
+          $this->raiseLintAtToken(
+            $close,
+            self::LINT_ARRAY_SEPARATOR,
+            pht('Closing parenthesis should be on a new line.'),
+            "\n".$array->getIndentation().$close->getValue());
+        }
+      } else if ($after && $after->getValue() == ',') {
         $this->raiseLintAtToken(
           $after,
           self::LINT_ARRAY_SEPARATOR,
index 595ff28f0314028e1c06d05ffc23dfbb90d9cb29..d02aa5808851b9a64e23fe544320dc262aedf44d 100644 (file)
@@ -28,12 +28,17 @@ array(
   1,
   2,
   3 /* comment */ );
+array(
+  1,
+  2,
+  3, /* comment */ );
 ~~~~~~~~~~
 advice:3:14
 advice:12:3
 advice:16:3
 advice:26:3
 advice:30:3
+advice:34:20
 ~~~~~~~~~~
 <?php
 array(1, 2, 3);
@@ -67,3 +72,8 @@ array(
   2,
   3, /* comment */
 );
+array(
+  1,
+  2,
+  3, /* comment */
+);