From 17c82ff03cb9a33ad0165970d2cc8be337e82d13 Mon Sep 17 00:00:00 2001 From: epriestley Date: Mon, 1 Aug 2011 13:18:53 -0700 Subject: [PATCH] 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 --- .../linter/xhpast/ArcanistXHPASTLinter.php | 24 ++++++++ .../data/creative-brace-use.lint-test | 56 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 src/lint/linter/xhpast/__tests__/data/creative-brace-use.lint-test 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 @@ +