]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a linter rule for unary prefix expression spacing
authorJoshua Spence <josh@joshuaspence.com>
Wed, 5 Aug 2015 21:00:32 +0000 (07:00 +1000)
committerJoshua Spence <josh@joshuaspence.com>
Wed, 5 Aug 2015 21:08:55 +0000 (07:08 +1000)
Summary: Add a linter rule to ensure that there is no space between the operator and the operand in a unary prefix expression.

Test Plan: Added test cases.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin

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

src/__phutil_library_map__.php
src/lint/linter/__tests__/xhpast/exit-expression.lint-test
src/lint/linter/__tests__/xhpast/unary-prefix-expression-spacing.lint-test [new file with mode: 0644]
src/lint/linter/xhpast/rules/ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule.php [new file with mode: 0644]

index 49ccb0d80d536e00c37f6c258e09184f6627b6ed..64dc1a44ffc2a1f02d333d78a8095e57cb190050 100644 (file)
@@ -237,6 +237,7 @@ phutil_register_library_map(array(
     'ArcanistTodoWorkflow' => 'workflow/ArcanistTodoWorkflow.php',
     'ArcanistUSEnglishTranslation' => 'internationalization/ArcanistUSEnglishTranslation.php',
     'ArcanistUnableToParseXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnableToParseXHPASTLinterRule.php',
+    'ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule.php',
     'ArcanistUndeclaredVariableXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUndeclaredVariableXHPASTLinterRule.php',
     'ArcanistUnitConsoleRenderer' => 'unit/renderer/ArcanistUnitConsoleRenderer.php',
     'ArcanistUnitRenderer' => 'unit/renderer/ArcanistUnitRenderer.php',
@@ -510,6 +511,7 @@ phutil_register_library_map(array(
     'ArcanistTodoWorkflow' => 'ArcanistWorkflow',
     'ArcanistUSEnglishTranslation' => 'PhutilTranslation',
     'ArcanistUnableToParseXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+    'ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistUndeclaredVariableXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistUnitConsoleRenderer' => 'ArcanistUnitRenderer',
     'ArcanistUnitRenderer' => 'Phobject',
index 96fbeb6f02ef1fb5652ea06d91e30c524c3817fa..c9724096160ebc61583460d1bd8e7781c03e7893 100644 (file)
@@ -2,7 +2,14 @@
 exit(-1);
 exit -1;
 strtoupper(33 * exit - 6);
+echo '';
+print '';
+
+$x = new stdClass();
+$y = clone $x;
 ~~~~~~~~~~
 error:3:1
+warning:3:5
 warning:3:6
 error:4:17
+warning:4:21
diff --git a/src/lint/linter/__tests__/xhpast/unary-prefix-expression-spacing.lint-test b/src/lint/linter/__tests__/xhpast/unary-prefix-expression-spacing.lint-test
new file mode 100644 (file)
index 0000000..87b65c6
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+$f = @ fopen($path, 'rb');
+@fclose($f);
+exit ();
+~~~~~~~~~~
+warning:2:7
+warning:4:5
+~~~~~~~~~~
+<?php
+$f = @fopen($path, 'rb');
+@fclose($f);
+exit();
diff --git a/src/lint/linter/xhpast/rules/ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule.php
new file mode 100644 (file)
index 0000000..2d398c4
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+
+final class ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule
+  extends ArcanistXHPASTLinterRule {
+
+  const ID = 73;
+
+  public function getLintName() {
+    return pht('Space After Unary Operator');
+  }
+
+  public function getLintSeverity() {
+    return ArcanistLintSeverity::SEVERITY_WARNING;
+  }
+
+  public function process(XHPASTNode $root) {
+    $expressions = $root->selectDescendantsOfType('n_UNARY_PREFIX_EXPRESSION');
+
+    foreach ($expressions as $expression) {
+      $operator = $expression->getChildOfType(0, 'n_OPERATOR');
+      $operator_value = $operator->getConcreteString();
+      list($before, $after) = $operator->getSurroundingNonsemanticTokens();
+
+      switch (strtolower($operator_value)) {
+        case 'clone':
+        case 'echo':
+        case 'print':
+          break;
+
+        default:
+          if (!empty($after)) {
+            $this->raiseLintAtOffset(
+              $operator->getOffset() + strlen($operator->getConcreteString()),
+              pht('Unary operators should not be followed by whitespace.'),
+              implode('', mpull($after, 'getValue')),
+              '');
+          }
+          break;
+      }
+    }
+  }
+
+}