]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Stop XHPASTLinter from eating a newline after else
authorKiyoto Tamura <owenestea@gmail.com>
Thu, 12 Jan 2012 02:03:56 +0000 (18:03 -0800)
committerepriestley <git@epriestley.com>
Thu, 12 Jan 2012 02:03:56 +0000 (18:03 -0800)
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
src/lint/linter/xhpast/__tests__/data/space-after-control-keywords.lint-test

index 0ff6c3051a4acbdfc6f4a635dbd569abf08af982..9e0cd18f6f673c6c78ea4953e8a4ee4f9b6012d0 100644 (file)
@@ -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,
index c66723b67978bf330f3fc6a175537ef81ea0a565..5be41f20fb1ef922ec54325ee1117ec5956eeda9 100644 (file)
@@ -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
 ~~~~~~~~~~
 <?php
 if ($x) {}
@@ -40,3 +48,10 @@ else if ($z) {}
 
 if ($x) {}
 if ($x) {}
+if ($x)
+  echo 100;
+else
+  echo 10;
+do
+  echo 1;
+while (true);