From 6dfa45a8b304e393f86865fc00b813a47c22e7a0 Mon Sep 17 00:00:00 2001 From: Kiyoto Tamura Date: Wed, 11 Jan 2012 18:03:56 -0800 Subject: [PATCH] Stop XHPASTLinter from eating a newline after else Summary: If the else clause does not have braces (one-liner), XHPASTLinter eats the newline and brings the body statement of the else clause to the same line. Test Plan: added a new test to space-after-control-keywords.lint-test Reviewers: epriestley CC: aran, epriestley, kiyoto Differential Revision: https://secure.phabricator.com/D1367 --- src/lint/linter/xhpast/ArcanistXHPASTLinter.php | 15 +++++++++++++++ .../data/space-after-control-keywords.lint-test | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/lint/linter/xhpast/ArcanistXHPASTLinter.php b/src/lint/linter/xhpast/ArcanistXHPASTLinter.php index 0ff6c305..9e0cd18f 100644 --- a/src/lint/linter/xhpast/ArcanistXHPASTLinter.php +++ b/src/lint/linter/xhpast/ArcanistXHPASTLinter.php @@ -973,6 +973,21 @@ class ArcanistXHPASTLinter extends ArcanistLinter { $token->getValue().' '); } else if (count($after) == 1) { $space = head($after); + + // If we have an else clause with braces, $space may not be + // a single white space. e.g., + // + // if ($x) + // echo 'foo' + // else // <- $space is not " " but "\n ". + // echo 'bar' + // + // We just require it starts with either a whitespace or a newline. + if ($token->getTypeName() == 'T_ELSE' || + $token->getTypeName() == 'T_DO') { + break; + } + if ($space->isAnyWhitespace() && $space->getValue() != ' ') { $this->raiseLintAtToken( $space, diff --git a/src/lint/linter/xhpast/__tests__/data/space-after-control-keywords.lint-test b/src/lint/linter/xhpast/__tests__/data/space-after-control-keywords.lint-test index c66723b6..5be41f20 100644 --- a/src/lint/linter/xhpast/__tests__/data/space-after-control-keywords.lint-test +++ b/src/lint/linter/xhpast/__tests__/data/space-after-control-keywords.lint-test @@ -14,6 +14,13 @@ if ($x) {} if ($x) {} +if ($x) + echo 100; +else + echo 10; +do + echo 1; +while(true); ~~~~~~~~~~ warning:2:1 warning:3:1 @@ -25,6 +32,7 @@ warning:7:6 warning:8:1 warning:13:3 warning:14:3 +warning:23:1 ~~~~~~~~~~