]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a linter rule for array separators
authorJoshua Spence <josh@joshuaspence.com>
Wed, 24 Sep 2014 21:38:37 +0000 (07:38 +1000)
committerJoshua Spence <josh@joshuaspence.com>
Wed, 24 Sep 2014 21:40:30 +0000 (07:40 +1000)
Summary:
Adds a rule to `ArcanistXHPASTLinter` which ensures that:

# Single-lined arrays //do not// contain an unnecessary trailing comma separator after the final array value.
# Multi-lined arrays //do// contain a trailing comma seperator after the final value value.

Depends on D10534.

Test Plan: Wrote and executed unit tests.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: epriestley, Korvin

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

src/lint/linter/ArcanistXHPASTLinter.php
src/lint/linter/__tests__/xhpast/array-comma.lint-test [new file with mode: 0644]

index b005ea483031cd766849618eb329594618223e3c..5d443745ca641606b961e869acf8b01372a992da 100644 (file)
@@ -47,6 +47,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
   const LINT_PHP_COMPATIBILITY         = 45;
   const LINT_LANGUAGE_CONSTRUCT_PAREN  = 46;
   const LINT_EMPTY_STATEMENT           = 47;
+  const LINT_ARRAY_SEPARATOR           = 48;
 
   private $naminghook;
   private $switchhook;
@@ -105,6 +106,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
       self::LINT_PHP_COMPATIBILITY         => 'PHP Compatibility',
       self::LINT_LANGUAGE_CONSTRUCT_PAREN  => 'Language Construct Parentheses',
       self::LINT_EMPTY_STATEMENT           => 'Empty Block Statement',
+      self::LINT_ARRAY_SEPARATOR           => 'Array Separator',
     );
   }
 
@@ -144,6 +146,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
       self::LINT_CONCATENATION_OPERATOR    => $warning,
       self::LINT_LANGUAGE_CONSTRUCT_PAREN  => $warning,
       self::LINT_EMPTY_STATEMENT           => $advice,
+      self::LINT_ARRAY_SEPARATOR           => $advice,
     );
   }
 
@@ -193,7 +196,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
 
   public function getVersion() {
     // The version number should be incremented whenever a new rule is added.
-    return '8';
+    return '9';
   }
 
   protected function resolveFuture($path, Future $future) {
@@ -263,6 +266,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
       'lintPHPCompatibility' => self::LINT_PHP_COMPATIBILITY,
       'lintLanguageConstructParentheses' => self::LINT_LANGUAGE_CONSTRUCT_PAREN,
       'lintEmptyBlockStatements' => self::LINT_EMPTY_STATEMENT,
+      'lintArraySeparator' => self::LINT_ARRAY_SEPARATOR,
     );
 
     foreach ($method_codes as $method => $codes) {
@@ -2714,6 +2718,48 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
     }
   }
 
+  protected function lintArraySeparator(XHPASTNode $root) {
+    $arrays = $root->selectDescendantsOfType('n_ARRAY_LITERAL');
+
+    foreach ($arrays as $array) {
+      $commas = $array->selectTokensOfType(',');
+      $values = $array->selectDescendantsOfType('n_ARRAY_VALUE');
+
+      if ($values->count() == 0) {
+        // 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)) {
+        $this->raiseLintAtNode(
+          $last,
+          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;
+        }
+
+        $this->raiseLintAtToken(
+          $last_comma,
+          self::LINT_ARRAY_SEPARATOR,
+          pht('Single lined arrays should not have a trailing comma.'),
+          '');
+      }
+    }
+  }
+
   public function getSuperGlobalNames() {
     return array(
       '$GLOBALS',
diff --git a/src/lint/linter/__tests__/xhpast/array-comma.lint-test b/src/lint/linter/__tests__/xhpast/array-comma.lint-test
new file mode 100644 (file)
index 0000000..26096d2
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+array(1, 2, 3);
+array(1, 2, 3,);
+array(
+  1,
+  2,
+  3,
+);
+array(
+  1,
+  2,
+  3
+);
+~~~~~~~~~~
+advice:3:14
+advice:12:3
+~~~~~~~~~~
+<?php
+array(1, 2, 3);
+array(1, 2, 3);
+array(
+  1,
+  2,
+  3,
+);
+array(
+  1,
+  2,
+  3,
+);