]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Raise lint warning on missing space after comma
authorepriestley <git@epriestley.com>
Sun, 8 Apr 2012 23:09:11 +0000 (16:09 -0700)
committerepriestley <git@epriestley.com>
Sun, 8 Apr 2012 23:09:11 +0000 (16:09 -0700)
Summary: Saw this in a diff somewhere; complain about it.

Test Plan: Unit coverage.

Reviewers: btrahan, vrana, jungejason

Reviewed By: btrahan

CC: aran
Maniphest Tasks: T1060

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

src/lint/linter/xhpast/ArcanistXHPASTLinter.php
src/lint/linter/xhpast/__tests__/data/space-around-operators.lint-test

index fdbc7d7b5e908fa992a597c2f79f563239435d0d..bac63e5533b07ace0dc4663407fee1fec1102665 100644 (file)
@@ -1333,35 +1333,56 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
   }
 
   protected function lintSpaceAroundBinaryOperators($root) {
+
+    // NOTE: '.' is parsed as n_CONCATENATION_LIST, not n_BINARY_EXPRESSION,
+    // so we don't select it here.
+
     $expressions = $root->selectDescendantsOfType('n_BINARY_EXPRESSION');
     foreach ($expressions as $expression) {
       $operator = $expression->getChildByIndex(1);
       $operator_value = $operator->getConcreteString();
-      if ($operator_value == '.') {
-        // TODO: implement this check
-        continue;
-      } else {
-        list($before, $after) = $operator->getSurroundingNonsemanticTokens();
-
-        $replace = null;
-        if (empty($before) && empty($after)) {
-          $replace = " {$operator_value} ";
-        } else if (empty($before)) {
-          $replace = " {$operator_value}";
-        } else if (empty($after)) {
-          $replace = "{$operator_value} ";
-        }
+      list($before, $after) = $operator->getSurroundingNonsemanticTokens();
+
+      $replace = null;
+      if (empty($before) && empty($after)) {
+        $replace = " {$operator_value} ";
+      } else if (empty($before)) {
+        $replace = " {$operator_value}";
+      } else if (empty($after)) {
+        $replace = "{$operator_value} ";
+      }
 
-        if ($replace !== null) {
-          $this->raiseLintAtNode(
-            $operator,
+      if ($replace !== null) {
+        $this->raiseLintAtNode(
+          $operator,
+          self::LINT_BINARY_EXPRESSION_SPACING,
+          'Convention: logical and arithmetic operators should be '.
+          'surrounded by whitespace.',
+          $replace);
+      }
+    }
+
+    $tokens = $root->selectTokensOfType(',');
+    foreach ($tokens as $token) {
+      $next = $token->getNextToken();
+      switch ($next->getTypeName()) {
+        case ')':
+        case 'T_WHITESPACE':
+          break;
+          break;
+        default:
+          $this->raiseLintAtToken(
+            $token,
             self::LINT_BINARY_EXPRESSION_SPACING,
-            'Convention: logical and arithmetic operators should be '.
-            'surrounded by whitespace.',
-            $replace);
-        }
+            'Convention: comma should be followed by space.',
+            ', ');
+          break;
       }
     }
+
+    // TODO: Spacing around ".".
+    // TODO: Spacing around default parameter assignment in function/method
+    // declarations (which is not n_BINARY_EXPRESSION).
   }
 
   protected function lintDynamicDefines($root) {
index fb4c40371c7b4dad7043520c3e305467a02f7f3e..acf96a3a2b556abfe87f4e5a79734a337e579a37 100644 (file)
@@ -21,6 +21,7 @@ $prev = ($total == 1) ? $navids[0] : $navids[$total-1];
 $next = ($total == 1) ? $navids[0] : $navids[$current+1];
 if ($x instanceof z &&$w) { }
 if ($x instanceof z && $w) { }
+f(1,2);
 ~~~~~~~~~~
 warning:3:3
 warning:4:4
@@ -33,6 +34,7 @@ warning:11:3
 warning:20:52
 warning:21:54
 warning:22:21
+warning:24:4
 ~~~~~~~~~~
 <?php
 $a + $b;
@@ -57,3 +59,4 @@ $prev = ($total == 1) ? $navids[0] : $navids[$total - 1];
 $next = ($total == 1) ? $navids[0] : $navids[$current + 1];
 if ($x instanceof z && $w) { }
 if ($x instanceof z && $w) { }
+f(1, 2);