1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-29 10:12:41 +01:00

Add a linter rule for unary prefix expression spacing

Summary: Add a linter rule to ensure that there is no space between the operator and the operand in a unary prefix expression.

Test Plan: Added test cases.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin

Differential Revision: https://secure.phabricator.com/D13797
This commit is contained in:
Joshua Spence 2015-08-06 07:00:32 +10:00
parent 1e75302e77
commit c8f0deffab
4 changed files with 64 additions and 0 deletions

View file

@ -237,6 +237,7 @@ phutil_register_library_map(array(
'ArcanistTodoWorkflow' => 'workflow/ArcanistTodoWorkflow.php', 'ArcanistTodoWorkflow' => 'workflow/ArcanistTodoWorkflow.php',
'ArcanistUSEnglishTranslation' => 'internationalization/ArcanistUSEnglishTranslation.php', 'ArcanistUSEnglishTranslation' => 'internationalization/ArcanistUSEnglishTranslation.php',
'ArcanistUnableToParseXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnableToParseXHPASTLinterRule.php', 'ArcanistUnableToParseXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnableToParseXHPASTLinterRule.php',
'ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule.php',
'ArcanistUndeclaredVariableXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUndeclaredVariableXHPASTLinterRule.php', 'ArcanistUndeclaredVariableXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUndeclaredVariableXHPASTLinterRule.php',
'ArcanistUnitConsoleRenderer' => 'unit/renderer/ArcanistUnitConsoleRenderer.php', 'ArcanistUnitConsoleRenderer' => 'unit/renderer/ArcanistUnitConsoleRenderer.php',
'ArcanistUnitRenderer' => 'unit/renderer/ArcanistUnitRenderer.php', 'ArcanistUnitRenderer' => 'unit/renderer/ArcanistUnitRenderer.php',
@ -510,6 +511,7 @@ phutil_register_library_map(array(
'ArcanistTodoWorkflow' => 'ArcanistWorkflow', 'ArcanistTodoWorkflow' => 'ArcanistWorkflow',
'ArcanistUSEnglishTranslation' => 'PhutilTranslation', 'ArcanistUSEnglishTranslation' => 'PhutilTranslation',
'ArcanistUnableToParseXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', 'ArcanistUnableToParseXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistUndeclaredVariableXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', 'ArcanistUndeclaredVariableXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistUnitConsoleRenderer' => 'ArcanistUnitRenderer', 'ArcanistUnitConsoleRenderer' => 'ArcanistUnitRenderer',
'ArcanistUnitRenderer' => 'Phobject', 'ArcanistUnitRenderer' => 'Phobject',

View file

@ -2,7 +2,14 @@
exit(-1); exit(-1);
exit -1; exit -1;
strtoupper(33 * exit - 6); strtoupper(33 * exit - 6);
echo '';
print '';
$x = new stdClass();
$y = clone $x;
~~~~~~~~~~ ~~~~~~~~~~
error:3:1 error:3:1
warning:3:5
warning:3:6 warning:3:6
error:4:17 error:4:17
warning:4:21

View file

@ -0,0 +1,12 @@
<?php
$f = @ fopen($path, 'rb');
@fclose($f);
exit ();
~~~~~~~~~~
warning:2:7
warning:4:5
~~~~~~~~~~
<?php
$f = @fopen($path, 'rb');
@fclose($f);
exit();

View file

@ -0,0 +1,43 @@
<?php
final class ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule
extends ArcanistXHPASTLinterRule {
const ID = 73;
public function getLintName() {
return pht('Space After Unary Operator');
}
public function getLintSeverity() {
return ArcanistLintSeverity::SEVERITY_WARNING;
}
public function process(XHPASTNode $root) {
$expressions = $root->selectDescendantsOfType('n_UNARY_PREFIX_EXPRESSION');
foreach ($expressions as $expression) {
$operator = $expression->getChildOfType(0, 'n_OPERATOR');
$operator_value = $operator->getConcreteString();
list($before, $after) = $operator->getSurroundingNonsemanticTokens();
switch (strtolower($operator_value)) {
case 'clone':
case 'echo':
case 'print':
break;
default:
if (!empty($after)) {
$this->raiseLintAtOffset(
$operator->getOffset() + strlen($operator->getConcreteString()),
pht('Unary operators should not be followed by whitespace.'),
implode('', mpull($after, 'getValue')),
'');
}
break;
}
}
}
}