1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-10-23 17:18:50 +02: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',
'ArcanistUSEnglishTranslation' => 'internationalization/ArcanistUSEnglishTranslation.php',
'ArcanistUnableToParseXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnableToParseXHPASTLinterRule.php',
'ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule.php',
'ArcanistUndeclaredVariableXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUndeclaredVariableXHPASTLinterRule.php',
'ArcanistUnitConsoleRenderer' => 'unit/renderer/ArcanistUnitConsoleRenderer.php',
'ArcanistUnitRenderer' => 'unit/renderer/ArcanistUnitRenderer.php',
@ -510,6 +511,7 @@ phutil_register_library_map(array(
'ArcanistTodoWorkflow' => 'ArcanistWorkflow',
'ArcanistUSEnglishTranslation' => 'PhutilTranslation',
'ArcanistUnableToParseXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistUndeclaredVariableXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistUnitConsoleRenderer' => 'ArcanistUnitRenderer',
'ArcanistUnitRenderer' => 'Phobject',

View file

@ -2,7 +2,14 @@
exit(-1);
exit -1;
strtoupper(33 * exit - 6);
echo '';
print '';
$x = new stdClass();
$y = clone $x;
~~~~~~~~~~
error:3:1
warning:3:5
warning:3:6
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;
}
}
}
}