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 ~~~~~~~~~~