From: epriestley Date: Mon, 1 Aug 2011 20:18:53 +0000 (-0700) Subject: Add an XHPAST lint rule for brace formatting X-Git-Tag: conduit-4~282^2~10 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=17c82ff03cb9a33ad0165970d2cc8be337e82d13;p=arcanist.git Add an XHPAST lint rule for brace formatting Summary: See D753. Let's just have lint fix this. Depends on D754 (it fails to detect all the blocks without that patch, but doesn't do anything bad). Test Plan: Ran unit tests. Reviewed By: jungejason Reviewers: jungejason, tuomaspelkonen, aran CC: hunterbridges, aran, epriestley, jungejason Differential Revision: 755 --- diff --git a/src/lint/linter/xhpast/ArcanistXHPASTLinter.php b/src/lint/linter/xhpast/ArcanistXHPASTLinter.php index 404e13e7..cf616f4c 100644 --- a/src/lint/linter/xhpast/ArcanistXHPASTLinter.php +++ b/src/lint/linter/xhpast/ArcanistXHPASTLinter.php @@ -155,6 +155,30 @@ class ArcanistXHPASTLinter extends ArcanistLinter { $this->lintPlusOperatorOnStrings($root); $this->lintDuplicateKeysInArray($root); $this->lintReusedIterators($root); + $this->lintBraceFormatting($root); + } + + private function lintBraceFormatting($root) { + + foreach ($root->selectDescendantsOfType('n_STATEMENT_LIST') as $list) { + $tokens = $list->getTokens(); + if (!$tokens || head($tokens)->getValue() != '{') { + continue; + } + list($before, $after) = $list->getSurroundingNonsemanticTokens(); + if (count($before) == 1) { + $before = reset($before); + if ($before->getValue() != ' ') { + $this->raiseLintAtToken( + $before, + self::LINT_FORMATTING_CONVENTIONS, + 'Put opening braces on the same line as control statements and '. + 'declarations, with a single space before them.', + ' '); + } + } + } + } private function lintTautologicalExpressions($root) { diff --git a/src/lint/linter/xhpast/__tests__/data/creative-brace-use.lint-test b/src/lint/linter/xhpast/__tests__/data/creative-brace-use.lint-test new file mode 100644 index 00000000..dc4f8bc4 --- /dev/null +++ b/src/lint/linter/xhpast/__tests__/data/creative-brace-use.lint-test @@ -0,0 +1,56 @@ +