]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Fix linting for spacing around default parameter assignment in function/method declar...
authorJoshua Spence <josh@joshuaspence.com>
Wed, 10 Sep 2014 12:30:01 +0000 (22:30 +1000)
committerJoshua Spence <josh@joshuaspence.com>
Wed, 10 Sep 2014 13:02:22 +0000 (23:02 +1000)
Summary: Fix linting for spacing around default parameter assignment in function/method declaration so that `function foo($x = null) {}` is preferred in favor of `function foo($x=null) {}`.

Test Plan: Modified existing unit tests.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: epriestley, Korvin

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

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

index 06363b1c02f6e54aa055f42b1d50abbc53069d4f..5ddba15feaad3d61009e0dab654fae7c3b8ebe89 100644 (file)
@@ -1989,8 +1989,34 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
       }
     }
 
-    // TODO: Spacing around default parameter assignment in function/method
-    // declarations (which is not n_BINARY_EXPRESSION).
+    $parameters = $root->selectDescendantsOfType('n_DECLARATION_PARAMETER');
+    foreach ($parameters as $parameter) {
+      if ($parameter->getChildByIndex(2)->getTypeName() == 'n_EMPTY') {
+        continue;
+      }
+
+      $operator = head($parameter->selectTokensOfType('='));
+      $before = $operator->getNonsemanticTokensBefore();
+      $after = $operator->getNonsemanticTokensAfter();
+
+      $replace = null;
+      if (empty($before) && empty($after)) {
+        $replace = ' = ';
+      } else if (empty($before)) {
+        $replace = ' =';
+      } else if (empty($after)) {
+        $replace = '= ';
+      }
+
+      if ($replace !== null) {
+        $this->raiseLintAtToken(
+          $operator,
+          self::LINT_BINARY_EXPRESSION_SPACING,
+          'Convention: logical and arithmetic operators should be '.
+          'surrounded by whitespace.',
+          $replace);
+      }
+    }
   }
 
   private function lintSpaceAroundConcatenationOperators(XHPASTNode $root) {
index 3b8b554299c11824b0474766040e4408ef9ade6d..2f94003ee9f1f8a6f2cadbf05e1e479d3e5487c1 100644 (file)
@@ -31,6 +31,7 @@ warning:8:3
 warning:9:4
 warning:10:3
 warning:11:3
+warning:13:14
 warning:20:52
 warning:21:54
 warning:22:21
@@ -48,7 +49,7 @@ $a -= $b;
 $a -= $b;
 $a === $b;
 $a.$b;
-function x($n=null) {}
+function x($n = null) {}
 function x($n = null) {}
 
 $y /* ! */ += // ?