]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a rule to the XHPAST linter to check for whitespace before a semicolon.
authorJoshua Spence <josh@joshuaspence.com>
Mon, 19 May 2014 14:48:58 +0000 (07:48 -0700)
committerepriestley <git@epriestley.com>
Mon, 19 May 2014 14:48:58 +0000 (07:48 -0700)
Summary: This rule is based on a rule from [[https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/SemicolonSpacingSniff.php | PHP_CodeSniffer]].

Test Plan: Wrote and executed unit tests.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: epriestley, Korvin

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

src/lint/linter/ArcanistXHPASTLinter.php
src/lint/linter/__tests__/xhpast/semicolon-spacing.lint-test [new file with mode: 0644]

index 0be11ed020474bf773ed5df8d063f784f16699ac..7e364d47bec2cb800e75cdd39f1891ba8693febc 100644 (file)
@@ -45,6 +45,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
   const LINT_KEYWORD_CASING            = 40;
   const LINT_DOUBLE_QUOTE              = 41;
   const LINT_ELSEIF_USAGE              = 42;
+  const LINT_SEMICOLON_SPACING         = 43;
 
   private $naminghook;
   private $switchhook;
@@ -103,6 +104,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
       self::LINT_KEYWORD_CASING            => 'Keyword Conventions',
       self::LINT_DOUBLE_QUOTE              => 'Unnecessary Double Quotes',
       self::LINT_ELSEIF_USAGE              => 'ElseIf Usage',
+      self::LINT_SEMICOLON_SPACING         => 'Semicolon Spacing',
     );
   }
 
@@ -138,6 +140,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
       self::LINT_KEYWORD_CASING            => $warning,
       self::LINT_DOUBLE_QUOTE              => $advice,
       self::LINT_ELSEIF_USAGE              => $advice,
+      self::LINT_SEMICOLON_SPACING         => $advice,
 
       // This is disabled by default because it implies a very strict policy
       // which isn't necessary in the general case.
@@ -253,6 +256,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
       'lintKeywordCasing' => self::LINT_KEYWORD_CASING,
       'lintStrings' => self::LINT_DOUBLE_QUOTE,
       'lintElseIfStatements' => self::LINT_ELSEIF_USAGE,
+      'lintSemicolons' => self::LINT_SEMICOLON_SPACING,
     );
 
     foreach ($method_codes as $method => $codes) {
@@ -2454,6 +2458,22 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
     }
   }
 
+  protected function lintSemicolons(XHPASTNode $root) {
+    $tokens = $root->selectTokensOfType(';');
+
+    foreach ($tokens as $token) {
+      $prev = $token->getPrevToken();
+
+      if ($prev->isAnyWhitespace()) {
+        $this->raiseLintAtToken(
+          $prev,
+          self::LINT_SEMICOLON_SPACING,
+          pht('Space found before semicolon.'),
+          '');
+      }
+    }
+  }
+
   public function getSuperGlobalNames() {
     return array(
       '$GLOBALS',
diff --git a/src/lint/linter/__tests__/xhpast/semicolon-spacing.lint-test b/src/lint/linter/__tests__/xhpast/semicolon-spacing.lint-test
new file mode 100644 (file)
index 0000000..c8e587d
--- /dev/null
@@ -0,0 +1,14 @@
+<?php
+foo();
+bar() ;
+baz()
+
+     ;
+~~~~~~~~~~
+advice:3:6
+advice:4:6
+~~~~~~~~~~
+<?php
+foo();
+bar();
+baz();