]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Improve linter rules for array formatting
authorJoshua Spence <josh@joshuaspence.com>
Tue, 11 Aug 2015 12:34:38 +0000 (22:34 +1000)
committerJoshua Spence <josh@joshuaspence.com>
Tue, 11 Aug 2015 12:34:39 +0000 (22:34 +1000)
Summary: Modify `ArcanistParenthesesSpacingXHPASTLinterRule` and `ArcanistCallParenthesesXHPASTLinterRule` to apply to `array()` and `list()` as well. Depends on D13858.

Test Plan: Added unit tests.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin

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

src/lint/linter/__tests__/phlxhp/array-formatting.lint-test [new file with mode: 0644]
src/lint/linter/xhpast/rules/ArcanistCallParenthesesXHPASTLinterRule.php
src/lint/linter/xhpast/rules/ArcanistParenthesesSpacingXHPASTLinterRule.php
src/unit/engine/ArcanistConfigurationDrivenUnitTestEngine.php

diff --git a/src/lint/linter/__tests__/phlxhp/array-formatting.lint-test b/src/lint/linter/__tests__/phlxhp/array-formatting.lint-test
new file mode 100644 (file)
index 0000000..3dda317
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+
+array ( 1, 2, 3 );
+list ( $x, $y ) = array();
+~~~~~~~~~~
+warning:3:6
+warning:3:8
+warning:3:16
+warning:4:5
+warning:4:7
+warning:4:14
+~~~~~~~~~~
+<?php
+
+array(1, 2, 3);
+list($x, $y) = array();
index 5d6bc2cf6a2690ec8172b03c344fbab560c7c261..67cb3676a8df928f15d3894ebc218a77f38a072e 100644 (file)
@@ -14,13 +14,33 @@ final class ArcanistCallParenthesesXHPASTLinterRule
   }
 
   public function process(XHPASTNode $root) {
-    $calls = $root->selectDescendantsOfTypes(array(
+    $nodes = $root->selectDescendantsOfTypes(array(
+      'n_ARRAY_LITERAL',
       'n_FUNCTION_CALL',
       'n_METHOD_CALL',
+      'n_LIST',
     ));
 
-    foreach ($calls as $call) {
-      $params = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
+    foreach ($nodes as $node) {
+      switch ($node->getTypeName()) {
+        case 'n_ARRAY_LITERAL':
+          $params = $node->getChildOfType(0, 'n_ARRAY_VALUE_LIST');
+          break;
+
+        case 'n_FUNCTION_CALL':
+        case 'n_METHOD_CALL':
+          $params = $node->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
+          break;
+
+        case 'n_LIST':
+          $params = $node->getChildOfType(0, 'n_ASSIGNMENT_LIST');
+          break;
+
+        default:
+          throw new Exception(
+            pht("Unexpected node of type '%s'!", $node->getTypeName()));
+      }
+
       $tokens = $params->getTokens();
       $first  = head($tokens);
 
@@ -29,17 +49,13 @@ final class ArcanistCallParenthesesXHPASTLinterRule
       if (preg_match('/^\s+$/', $leading_text)) {
         $this->raiseLintAtOffset(
           $first->getOffset() - strlen($leading_text),
-          pht('Convention: no spaces before opening parenthesis in calls.'),
+          pht('Convention: no spaces before opening parentheses.'),
           $leading_text,
           '');
       }
-    }
 
-    foreach ($calls as $call) {
       // If the last parameter of a call is a HEREDOC, don't apply this rule.
-      $params = $call
-        ->getChildOfType(1, 'n_CALL_PARAMETER_LIST')
-        ->getChildren();
+      $params = $params->getChildren();
 
       if ($params) {
         $last_param = last($params);
@@ -48,15 +64,19 @@ final class ArcanistCallParenthesesXHPASTLinterRule
         }
       }
 
-      $tokens = $call->getTokens();
+      $tokens = $node->getTokens();
       $last = array_pop($tokens);
 
+      if ($node->getTypeName() == 'n_ARRAY_LITERAL') {
+        continue;
+      }
+
       $trailing = $last->getNonsemanticTokensBefore();
       $trailing_text = implode('', mpull($trailing, 'getValue'));
       if (preg_match('/^\s+$/', $trailing_text)) {
         $this->raiseLintAtOffset(
           $last->getOffset() - strlen($trailing_text),
-          pht('Convention: no spaces before closing parenthesis in calls.'),
+          pht('Convention: no spaces before closing parentheses.'),
           $trailing_text,
           '');
       }
index b0bd83400b591ad8a481d98e6f3862cb2a398e2d..7ad16a44c67a0e77cf942b36bdf9b5cc57a4b96e 100644 (file)
@@ -15,11 +15,13 @@ final class ArcanistParenthesesSpacingXHPASTLinterRule
 
   public function process(XHPASTNode $root) {
     $all_paren_groups = $root->selectDescendantsOfTypes(array(
+      'n_ARRAY_VALUE_LIST',
+      'n_ASSIGNMENT_LIST',
       'n_CALL_PARAMETER_LIST',
+      'n_DECLARATION_PARAMETER_LIST',
       'n_CONTROL_CONDITION',
       'n_FOR_EXPRESSION',
       'n_FOREACH_EXPRESSION',
-      'n_DECLARATION_PARAMETER_LIST',
     ));
 
     foreach ($all_paren_groups as $group) {
index 9c5ed8ab249f22ff581709a21805990bcddfebca..e24b4d5f0d7f4e0dbb7cf773cca275b12c02d34a 100644 (file)
@@ -97,14 +97,17 @@ final class ArcanistConfigurationDrivenUnitTestEngine
           $ex);
       }
 
-      $include = (array)idx($spec, 'include', array());
-      $exclude = (array)idx($spec, 'exclude', array());
-      $paths = $this->matchPaths(
-        $all_paths,
-        $include,
-        $exclude);
-
-      $test_engine->setPaths($paths);
+      if ($all_paths) {
+        $include = (array)idx($spec, 'include', array());
+        $exclude = (array)idx($spec, 'exclude', array());
+        $paths = $this->matchPaths(
+          $all_paths,
+          $include,
+          $exclude);
+
+        $test_engine->setPaths($paths);
+      }
+
       $built_test_engines[] = $test_engine;
     }