1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-25 16:22:42 +01:00

Add a linter rule for unary postfix expression spacing

Summary: Unary postfix expressions should not have a space before the operator.

Test Plan: Added test case.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin

Differential Revision: https://secure.phabricator.com/D13805
This commit is contained in:
Joshua Spence 2015-08-06 11:38:14 +10:00
parent 986f5d82d0
commit 968f4ae5d7
4 changed files with 50 additions and 2 deletions

View file

@ -238,6 +238,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',
'ArcanistUnaryPostfixExpressionSpacingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnaryPostfixExpressionSpacingXHPASTLinterRule.php',
'ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule.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',
@ -513,6 +514,7 @@ phutil_register_library_map(array(
'ArcanistTodoWorkflow' => 'ArcanistWorkflow', 'ArcanistTodoWorkflow' => 'ArcanistWorkflow',
'ArcanistUSEnglishTranslation' => 'PhutilTranslation', 'ArcanistUSEnglishTranslation' => 'PhutilTranslation',
'ArcanistUnableToParseXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', 'ArcanistUnableToParseXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistUnaryPostfixExpressionSpacingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', 'ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistUndeclaredVariableXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', 'ArcanistUndeclaredVariableXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistUnitConsoleRenderer' => 'ArcanistUnitRenderer', 'ArcanistUnitConsoleRenderer' => 'ArcanistUnitRenderer',

View file

@ -0,0 +1,9 @@
<?php
$x ++;
$y--;
~~~~~~~~~~
warning:2:3
~~~~~~~~~~
<?php
$x++;
$y--;

View file

@ -0,0 +1,36 @@
<?php
final class ArcanistUnaryPostfixExpressionSpacingXHPASTLinterRule
extends ArcanistXHPASTLinterRule {
const ID = 75;
public function getLintName() {
return pht('Space Before Unary Postfix Operator');
}
public function getLintSeverity() {
return ArcanistLintSeverity::SEVERITY_WARNING;
}
public function process(XHPASTNode $root) {
$expressions = $root->selectDescendantsOfType('n_UNARY_POSTFIX_EXPRESSION');
foreach ($expressions as $expression) {
$operator = $expression->getChildOfType(1, 'n_OPERATOR');
$operator_value = $operator->getConcreteString();
list($before, $after) = $operator->getSurroundingNonsemanticTokens();
if (!empty($before)) {
$leading_text = implode('', mpull($before, 'getValue'));
$this->raiseLintAtOffset(
$operator->getOffset() - strlen($leading_text),
pht('Unary postfix operators should not be prefixed by whitespace.'),
$leading_text,
'');
}
}
}
}

View file

@ -6,7 +6,7 @@ final class ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule
const ID = 73; const ID = 73;
public function getLintName() { public function getLintName() {
return pht('Space After Unary Operator'); return pht('Space After Unary Prefix Operator');
} }
public function getLintSeverity() { public function getLintSeverity() {
@ -31,7 +31,8 @@ final class ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule
if (!empty($after)) { if (!empty($after)) {
$this->raiseLintAtOffset( $this->raiseLintAtOffset(
$operator->getOffset() + strlen($operator->getConcreteString()), $operator->getOffset() + strlen($operator->getConcreteString()),
pht('Unary operators should not be followed by whitespace.'), pht(
'Unary prefix operators should not be followed by whitespace.'),
implode('', mpull($after, 'getValue')), implode('', mpull($after, 'getValue')),
''); '');
} }