From: epriestley Date: Sun, 8 Apr 2012 23:09:11 +0000 (-0700) Subject: Raise lint warning on missing space after comma X-Git-Tag: conduit-4~58 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=2c599f89288d80f60f92544ad0ccd1a6c223330d;p=arcanist.git Raise lint warning on missing space after comma Summary: Saw this in a diff somewhere; complain about it. Test Plan: Unit coverage. Reviewers: btrahan, vrana, jungejason Reviewed By: btrahan CC: aran Maniphest Tasks: T1060 Differential Revision: https://secure.phabricator.com/D2153 --- diff --git a/src/lint/linter/xhpast/ArcanistXHPASTLinter.php b/src/lint/linter/xhpast/ArcanistXHPASTLinter.php index fdbc7d7b..bac63e55 100644 --- a/src/lint/linter/xhpast/ArcanistXHPASTLinter.php +++ b/src/lint/linter/xhpast/ArcanistXHPASTLinter.php @@ -1333,35 +1333,56 @@ final class ArcanistXHPASTLinter extends ArcanistLinter { } protected function lintSpaceAroundBinaryOperators($root) { + + // NOTE: '.' is parsed as n_CONCATENATION_LIST, not n_BINARY_EXPRESSION, + // so we don't select it here. + $expressions = $root->selectDescendantsOfType('n_BINARY_EXPRESSION'); foreach ($expressions as $expression) { $operator = $expression->getChildByIndex(1); $operator_value = $operator->getConcreteString(); - if ($operator_value == '.') { - // TODO: implement this check - continue; - } else { - list($before, $after) = $operator->getSurroundingNonsemanticTokens(); - - $replace = null; - if (empty($before) && empty($after)) { - $replace = " {$operator_value} "; - } else if (empty($before)) { - $replace = " {$operator_value}"; - } else if (empty($after)) { - $replace = "{$operator_value} "; - } + list($before, $after) = $operator->getSurroundingNonsemanticTokens(); + + $replace = null; + if (empty($before) && empty($after)) { + $replace = " {$operator_value} "; + } else if (empty($before)) { + $replace = " {$operator_value}"; + } else if (empty($after)) { + $replace = "{$operator_value} "; + } - if ($replace !== null) { - $this->raiseLintAtNode( - $operator, + if ($replace !== null) { + $this->raiseLintAtNode( + $operator, + self::LINT_BINARY_EXPRESSION_SPACING, + 'Convention: logical and arithmetic operators should be '. + 'surrounded by whitespace.', + $replace); + } + } + + $tokens = $root->selectTokensOfType(','); + foreach ($tokens as $token) { + $next = $token->getNextToken(); + switch ($next->getTypeName()) { + case ')': + case 'T_WHITESPACE': + break; + break; + default: + $this->raiseLintAtToken( + $token, self::LINT_BINARY_EXPRESSION_SPACING, - 'Convention: logical and arithmetic operators should be '. - 'surrounded by whitespace.', - $replace); - } + 'Convention: comma should be followed by space.', + ', '); + break; } } + + // TODO: Spacing around ".". + // TODO: Spacing around default parameter assignment in function/method + // declarations (which is not n_BINARY_EXPRESSION). } protected function lintDynamicDefines($root) { diff --git a/src/lint/linter/xhpast/__tests__/data/space-around-operators.lint-test b/src/lint/linter/xhpast/__tests__/data/space-around-operators.lint-test index fb4c4037..acf96a3a 100644 --- a/src/lint/linter/xhpast/__tests__/data/space-around-operators.lint-test +++ b/src/lint/linter/xhpast/__tests__/data/space-around-operators.lint-test @@ -21,6 +21,7 @@ $prev = ($total == 1) ? $navids[0] : $navids[$total-1]; $next = ($total == 1) ? $navids[0] : $navids[$current+1]; if ($x instanceof z &&$w) { } if ($x instanceof z && $w) { } +f(1,2); ~~~~~~~~~~ warning:3:3 warning:4:4 @@ -33,6 +34,7 @@ warning:11:3 warning:20:52 warning:21:54 warning:22:21 +warning:24:4 ~~~~~~~~~~