]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Fix `LINT_ARRAY_SEPARATOR` for nested arrays
authorJoshua Spence <josh@joshuaspence.com>
Thu, 25 Sep 2014 21:06:19 +0000 (07:06 +1000)
committerJoshua Spence <josh@joshuaspence.com>
Thu, 25 Sep 2014 21:14:55 +0000 (07:14 +1000)
Summary: The `LINT_ARRAY_SEPARATOR` rule was added to `ArcanistXHPASTLinter` in D10535. Unfortunately, it does not correctly handle nested arrays correctly. This patch fixes this. Depends on D10554.

Test Plan: Added a test case.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: epriestley, Korvin

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

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

index b059ddca6fbae6c20e1b84dbfc453633af63f06e..bb952d315f45293400da157cfbc16ced1107aeba 100644 (file)
@@ -2722,37 +2722,28 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
     $arrays = $root->selectDescendantsOfType('n_ARRAY_LITERAL');
 
     foreach ($arrays as $array) {
-      $commas = $array->selectTokensOfType(',');
-      $values = $array->selectDescendantsOfType('n_ARRAY_VALUE');
+      $value_list = $array->getChildOfType(0, 'n_ARRAY_VALUE_LIST');
+      $values = $value_list->getChildrenOfType('n_ARRAY_VALUE');
 
-      if ($values->count() == 0) {
+      if (!$values) {
         // There is no need to check an empty array.
         continue;
       }
 
-      // This is a little messy... we want to do `last($values)` but
-      // `$values` is an `AASTNodeList`.
-      $last = null;
-      foreach ($values as $value) {
-        $last = $value;
-      }
-
       $multiline = $array->getLineNumber() != $array->getEndLineNumber();
 
-      if ($multiline && count($commas) != count($values)) {
+      $value = last($values);
+      $after = last($value->getTokens())->getNextToken();
+
+      if ($multiline && (!$after || $after->getValue() != ',')) {
         $this->raiseLintAtNode(
-          $last,
+          $value,
           self::LINT_ARRAY_SEPARATOR,
           pht('Multi-lined arrays should have trailing commas.'),
-          $last->getConcreteString().',');
-      } else if (!$multiline && count($commas) == count($values)) {
-        $last_comma = null;
-        foreach ($commas as $comma) {
-          $last_comma = $comma;
-        }
-
+          $value->getConcreteString().',');
+      } else if (!$multiline && $after && $after->getValue() == ',') {
         $this->raiseLintAtToken(
-          $last_comma,
+          $after,
           self::LINT_ARRAY_SEPARATOR,
           pht('Single lined arrays should not have a trailing comma.'),
           '');
index 26096d29f54527d2d5153fa26f6337d0da8f1a90..74856a04d2c480aa24fa2b8c842c765ca7b522e4 100644 (file)
@@ -11,9 +11,14 @@ array(
   2,
   3
 );
+array(
+  'foo',
+  array('foo')
+);
 ~~~~~~~~~~
 advice:3:14
 advice:12:3
+advice:16:3
 ~~~~~~~~~~
 <?php
 array(1, 2, 3);
@@ -28,3 +33,7 @@ array(
   2,
   3,
 );
+array(
+  'foo',
+  array('foo'),
+);