From: Joshua Spence Date: Mon, 8 Dec 2014 12:31:21 +0000 (+1100) Subject: Extend `LINT_BRACE_FORMATTING` to support more scenarios X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=4ca3463df684795b0cec6798256beebb845ee572;p=arcanist.git Extend `LINT_BRACE_FORMATTING` to support more scenarios 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 --- diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php index ed794754..74682cc7 100644 --- a/src/lint/linter/ArcanistXHPASTLinter.php +++ b/src/lint/linter/ArcanistXHPASTLinter.php @@ -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) { diff --git a/src/lint/linter/__tests__/xhpast/creative-brace-use.lint-test b/src/lint/linter/__tests__/xhpast/creative-brace-use.lint-test index add2654f..7d010c6c 100644 --- a/src/lint/linter/__tests__/xhpast/creative-brace-use.lint-test +++ b/src/lint/linter/__tests__/xhpast/creative-brace-use.lint-test @@ -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 ~~~~~~~~~~ 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 ~~~~~~~~~~ m( $x, diff --git a/src/lint/linter/__tests__/xhpast/space-after-control-keywords.lint-test b/src/lint/linter/__tests__/xhpast/space-after-control-keywords.lint-test index 6d0b43d4..444a052f 100644 --- a/src/lint/linter/__tests__/xhpast/space-after-control-keywords.lint-test +++ b/src/lint/linter/__tests__/xhpast/space-after-control-keywords.lint-test @@ -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 ~~~~~~~~~~