]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Extend `LINT_BRACE_FORMATTING` to support more scenarios
authorJoshua Spence <josh@joshuaspence.com>
Mon, 8 Dec 2014 12:31:21 +0000 (23:31 +1100)
committerJoshua Spence <josh@joshuaspence.com>
Mon, 8 Dec 2014 12:31:21 +0000 (23:31 +1100)
Summary: Extend the `ArcanistXHPASTLinter::LINT_BRACE_FORMATTING` rule to support a bunch of extra cases.

Test Plan: Added test cases.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: epriestley, Korvin

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

src/lint/linter/ArcanistXHPASTLinter.php
src/lint/linter/__tests__/xhpast/creative-brace-use.lint-test
src/lint/linter/__tests__/xhpast/parens-hug-contents.lint-test
src/lint/linter/__tests__/xhpast/space-after-control-keywords.lint-test

index ed7947543e594e0b6c1e78783d1a1dff5f5b15d9..74682cc706e6c1d4a0bf140c4871322d41585fc7 100644 (file)
@@ -902,6 +902,39 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
             'Use braces to surround a statement block.');
       }
     }
+
+    $nodes = $root->selectDescendantsOfTypes(array(
+      'n_DO_WHILE',
+      'n_ELSE',
+      'n_ELSEIF',
+    ));
+    foreach ($nodes as $list) {
+      $tokens = $list->getTokens();
+      if (!$tokens || last($tokens)->getValue() != '}') {
+        continue;
+      }
+      list($before, $after) = $list->getSurroundingNonsemanticTokens();
+      if (!$before) {
+        $first = last($tokens);
+
+        $this->raiseLintAtToken(
+          $first,
+          self::LINT_BRACE_FORMATTING,
+          'Put opening braces on the same line as control statements and '.
+          'declarations, with a single space before them.',
+          ' '.$first->getValue());
+      } else if (count($before) === 1) {
+        $before = reset($before);
+        if ($before->getValue() !== ' ') {
+          $this->raiseLintAtToken(
+            $before,
+            self::LINT_BRACE_FORMATTING,
+            'Put opening braces on the same line as control statements and '.
+            'declarations, with a single space before them.',
+            ' ');
+        }
+      }
+    }
   }
 
   private function lintTautologicalExpressions(XHPASTNode $root) {
index add2654f3399f220a37e91747406f226278e3c33..7d010c6cb2ec086d49b82505c2ead898d43c6678 100644 (file)
@@ -30,6 +30,10 @@ function h(){}
 if ($x) foo();
 else bar();
 do baz(); while ($x);
+
+if ($x) {}
+else if ($y) {}
+else {}
 ~~~~~~~~~~
 advice:3:14
 warning:7:13
@@ -44,6 +48,8 @@ warning:29:13
 warning:30:9
 warning:31:6
 warning:32:4
+warning:34:11
+warning:35:16
 ~~~~~~~~~~
 <?php
 
@@ -66,3 +72,5 @@ function h() {}
 if ($x) foo();
 else bar();
 do baz(); while ($x);
+
+if ($x) {} else if ($y) {} else {}
index 5d2e0ada0a2d0ca6bafc78385012b4b106fcc1f8..c0058bc0198821fc38da3d1ad0348af4fbcdf792 100644 (file)
@@ -3,8 +3,7 @@ if ( $x ) {}
 f( );
 q(                      );
 g();
-if ($x) {}
-else if ( $y ) {}
+if ($x) {} else if ( $y ) {}
 
 $obj->m(
   $x,
@@ -25,27 +24,26 @@ warning:2:5
 warning:2:8
 warning:3:3
 warning:4:3
-warning:7:10
-warning:7:13
-warning:13:6
-warning:13:30
-warning:14:10
-warning:14:19
-warning:15:12
-warning:15:15
-error:16:13 XHP19 Class-Filename Mismatch
-warning:17:21
-warning:17:24
-warning:19:10
-warning:19:25
+warning:6:21
+warning:6:24
+warning:12:6
+warning:12:30
+warning:13:10
+warning:13:19
+warning:14:12
+warning:14:15
+error:15:13 XHP19 Class-Filename Mismatch
+warning:16:21
+warning:16:24
+warning:18:10
+warning:18:25
 ~~~~~~~~~~
 <?php
 if ($x) {}
 f();
 q();
 g();
-if ($x) {}
-else if ($y) {}
+if ($x) {} else if ($y) {}
 
 $obj->m(
   $x,
index 6d0b43d4bf2a07fdcf5798021f30965e4b2befb1..444a052f0cb0c6c509a0b04a531b0468a35679c9 100644 (file)
@@ -24,6 +24,7 @@ do {
 } while(true);
 ~~~~~~~~~~
 warning:2:1
+warning:2:10
 warning:3:1
 warning:4:1
 warning:5:1
@@ -31,21 +32,20 @@ warning:6:1
 warning:7:1
 warning:7:6
 warning:8:1
+warning:9:11
+warning:10:16
 warning:13:3
 warning:14:3
 warning:24:3
 ~~~~~~~~~~
 <?php
-if ($x) {}
-else {}
+if ($x) {} else {}
 for (;;) {}
 foreach ($x as $y) {}
 while ($x) {}
 do {} while ($x);
 switch ($x) {}
-if ($x) {}
-else if ($y) {}
-else if ($z) {}
+if ($x) {} else if ($y) {} else if ($z) {}
 
 if ($x) {}
 if ($x) {}