]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a linter rule for unary postfix expression spacing
authorJoshua Spence <josh@joshuaspence.com>
Thu, 6 Aug 2015 01:38:14 +0000 (11:38 +1000)
committerJoshua Spence <josh@joshuaspence.com>
Thu, 6 Aug 2015 01:40:12 +0000 (11:40 +1000)
Summary: Unary postfix expressions should not have a space before the operator.

Test Plan: Added test case.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin

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

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

index 9b12f49d7393a3e043d3ce5ecd6b3242e0003a04..4783a8d6e8d0d4c0cc74b6bdf0268ef3f69b7d52 100644 (file)
@@ -238,6 +238,7 @@ phutil_register_library_map(array(
     'ArcanistTodoWorkflow' => 'workflow/ArcanistTodoWorkflow.php',
     'ArcanistUSEnglishTranslation' => 'internationalization/ArcanistUSEnglishTranslation.php',
     'ArcanistUnableToParseXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnableToParseXHPASTLinterRule.php',
+    'ArcanistUnaryPostfixExpressionSpacingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnaryPostfixExpressionSpacingXHPASTLinterRule.php',
     'ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule.php',
     'ArcanistUndeclaredVariableXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUndeclaredVariableXHPASTLinterRule.php',
     'ArcanistUnitConsoleRenderer' => 'unit/renderer/ArcanistUnitConsoleRenderer.php',
@@ -513,6 +514,7 @@ phutil_register_library_map(array(
     'ArcanistTodoWorkflow' => 'ArcanistWorkflow',
     'ArcanistUSEnglishTranslation' => 'PhutilTranslation',
     'ArcanistUnableToParseXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+    'ArcanistUnaryPostfixExpressionSpacingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistUndeclaredVariableXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistUnitConsoleRenderer' => 'ArcanistUnitRenderer',
diff --git a/src/lint/linter/__tests__/xhpast/unary-postfix-expression-spacing.lint-test b/src/lint/linter/__tests__/xhpast/unary-postfix-expression-spacing.lint-test
new file mode 100644 (file)
index 0000000..6e08218
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+$x ++;
+$y--;
+~~~~~~~~~~
+warning:2:3
+~~~~~~~~~~
+<?php
+$x++;
+$y--;
diff --git a/src/lint/linter/xhpast/rules/ArcanistUnaryPostfixExpressionSpacingXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistUnaryPostfixExpressionSpacingXHPASTLinterRule.php
new file mode 100644 (file)
index 0000000..175cf0d
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+
+final class ArcanistUnaryPostfixExpressionSpacingXHPASTLinterRule
+  extends ArcanistXHPASTLinterRule {
+
+  const ID = 75;
+
+  public function getLintName() {
+    return pht('Space Before Unary Postfix Operator');
+  }
+
+  public function getLintSeverity() {
+    return ArcanistLintSeverity::SEVERITY_WARNING;
+  }
+
+  public function process(XHPASTNode $root) {
+    $expressions = $root->selectDescendantsOfType('n_UNARY_POSTFIX_EXPRESSION');
+
+    foreach ($expressions as $expression) {
+      $operator = $expression->getChildOfType(1, 'n_OPERATOR');
+      $operator_value = $operator->getConcreteString();
+      list($before, $after) = $operator->getSurroundingNonsemanticTokens();
+
+      if (!empty($before)) {
+        $leading_text = implode('', mpull($before, 'getValue'));
+
+        $this->raiseLintAtOffset(
+          $operator->getOffset() - strlen($leading_text),
+          pht('Unary postfix operators should not be prefixed by whitespace.'),
+          $leading_text,
+          '');
+      }
+    }
+  }
+
+}
index 2d398c4e6393cf4f8d83a8e27e2bacc7ad74760f..5fa91ffd4ddbf1c12d5fc4aad3d3539ffb9fe086 100644 (file)
@@ -6,7 +6,7 @@ final class ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule
   const ID = 73;
 
   public function getLintName() {
-    return pht('Space After Unary Operator');
+    return pht('Space After Unary Prefix Operator');
   }
 
   public function getLintSeverity() {
@@ -31,7 +31,8 @@ final class ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule
           if (!empty($after)) {
             $this->raiseLintAtOffset(
               $operator->getOffset() + strlen($operator->getConcreteString()),
-              pht('Unary operators should not be followed by whitespace.'),
+              pht(
+                'Unary prefix operators should not be followed by whitespace.'),
               implode('', mpull($after, 'getValue')),
               '');
           }