]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Improve array comma rule
authorJoshua Spence <josh@joshuaspence.com>
Mon, 6 Apr 2015 21:51:52 +0000 (07:51 +1000)
committerJoshua Spence <josh@joshuaspence.com>
Mon, 6 Apr 2015 21:51:52 +0000 (07:51 +1000)
Summary: Improve `ArcanistXHPASTLinter::LINT_ARRAY_SEPARATOR` in handling multi-line arrays, see D12280 and D12281 for example.  Depends on D12295.

Test Plan: Updated unit tests.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: Korvin, epriestley

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

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

index 16f6022fc56d4d247ce1b3976f17e01e2b1cf188..7e7ac94a23d6f189b3b20233a4a6e42290b46a04 100644 (file)
@@ -2951,11 +2951,23 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
           continue;
         }
 
-        $this->raiseLintAtNode(
-          $value,
+        list($before, $after) = $value->getSurroundingNonsemanticTokens();
+        $after = implode('', mpull($after, 'getValue'));
+
+        $original = $value->getConcreteString();
+        $replacement = $value->getConcreteString().',';
+
+        if (strpos($after, "\n") === false) {
+          $original    .= $after;
+          $replacement .= rtrim($after)."\n".$array->getIndentation();
+        }
+
+        $this->raiseLintAtOffset(
+          $value->getOffset(),
           self::LINT_ARRAY_SEPARATOR,
           pht('Multi-lined arrays should have trailing commas.'),
-          $value->getConcreteString().',');
+          $original,
+          $replacement);
       } else if (!$multiline && $after && $after->getValue() == ',') {
         $this->raiseLintAtToken(
           $after,
index 031f3f3c1c17c25e8567f590cea4e767604b32e8..595ff28f0314028e1c06d05ffc23dfbb90d9cb29 100644 (file)
@@ -9,7 +9,7 @@ array(
 array(
   1,
   2,
-  3
+  3 // comment
 );
 array(
   'foo',
@@ -20,10 +20,20 @@ array(
 This is some heredoc text.
 EOTEXT
 );
+array(
+  1,
+  2,
+  3);
+array(
+  1,
+  2,
+  3 /* comment */ );
 ~~~~~~~~~~
 advice:3:14
 advice:12:3
 advice:16:3
+advice:26:3
+advice:30:3
 ~~~~~~~~~~
 <?php
 array(1, 2, 3);
@@ -36,7 +46,7 @@ array(
 array(
   1,
   2,
-  3,
+  3, // comment
 );
 array(
   'foo',
@@ -47,3 +57,13 @@ array(
 This is some heredoc text.
 EOTEXT
 );
+array(
+  1,
+  2,
+  3,
+);
+array(
+  1,
+  2,
+  3, /* comment */
+);