From 9cdaa6dc7a0ac23c57544a908cc80db4398ee44b Mon Sep 17 00:00:00 2001 From: epriestley Date: Wed, 4 Jan 2012 11:15:33 -0800 Subject: [PATCH] Make XHPASTLinter correct too little space before opening braces Summary: Previously, we would not correct missing space before "{". Test Plan: Ran unit tests. Reviewers: btrahan, jungejason, kiyoto Reviewed By: jungejason CC: aran, jungejason Differential Revision: https://secure.phabricator.com/D1313 --- .../linter/xhpast/ArcanistXHPASTLinter.php | 19 ++++++++++++++++++- .../data/creative-brace-use.lint-test | 5 +++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/lint/linter/xhpast/ArcanistXHPASTLinter.php b/src/lint/linter/xhpast/ArcanistXHPASTLinter.php index c552a32c..0ff6c305 100644 --- a/src/lint/linter/xhpast/ArcanistXHPASTLinter.php +++ b/src/lint/linter/xhpast/ArcanistXHPASTLinter.php @@ -168,7 +168,24 @@ class ArcanistXHPASTLinter extends ArcanistLinter { continue; } list($before, $after) = $list->getSurroundingNonsemanticTokens(); - if (count($before) == 1) { + if (!$before) { + $first = head($tokens); + + // Only insert the space if we're after a closing parenthesis. If + // we're in a construct like "else{}", other rules will insert space + // after the 'else' correctly. + $prev = $first->getPrevToken(); + if (!$prev || $prev->getValue() != ')') { + continue; + } + + $this->raiseLintAtToken( + $first, + self::LINT_FORMATTING_CONVENTIONS, + '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( 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 index dc4f8bc4..3a5ef0ce 100644 --- a/src/lint/linter/xhpast/__tests__/data/creative-brace-use.lint-test +++ b/src/lint/linter/xhpast/__tests__/data/creative-brace-use.lint-test @@ -25,6 +25,8 @@ try {} catch (Exception $x) {} + +function h(){} ~~~~~~~~~~ warning:7:13 warning:12:7 @@ -33,6 +35,7 @@ warning:18:10 warning:21:11 warning:24:4 warning:26:21 +warning:29:13 ~~~~~~~~~~