mirror of
https://we.phorge.it/source/arcanist.git
synced 2024-11-22 06:42:41 +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:
parent
986f5d82d0
commit
968f4ae5d7
4 changed files with 50 additions and 2 deletions
|
@ -238,6 +238,7 @@ phutil_register_library_map(array(
|
|||
'ArcanistTodoWorkflow' => 'workflow/ArcanistTodoWorkflow.php',
|
||||
'ArcanistUSEnglishTranslation' => 'internationalization/ArcanistUSEnglishTranslation.php',
|
||||
'ArcanistUnableToParseXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnableToParseXHPASTLinterRule.php',
|
||||
'ArcanistUnaryPostfixExpressionSpacingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnaryPostfixExpressionSpacingXHPASTLinterRule.php',
|
||||
'ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule.php',
|
||||
'ArcanistUndeclaredVariableXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUndeclaredVariableXHPASTLinterRule.php',
|
||||
'ArcanistUnitConsoleRenderer' => 'unit/renderer/ArcanistUnitConsoleRenderer.php',
|
||||
|
@ -513,6 +514,7 @@ phutil_register_library_map(array(
|
|||
'ArcanistTodoWorkflow' => 'ArcanistWorkflow',
|
||||
'ArcanistUSEnglishTranslation' => 'PhutilTranslation',
|
||||
'ArcanistUnableToParseXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
|
||||
'ArcanistUnaryPostfixExpressionSpacingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
|
||||
'ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
|
||||
'ArcanistUndeclaredVariableXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
|
||||
'ArcanistUnitConsoleRenderer' => 'ArcanistUnitRenderer',
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
$x ++;
|
||||
$y--;
|
||||
~~~~~~~~~~
|
||||
warning:2:3
|
||||
~~~~~~~~~~
|
||||
<?php
|
||||
$x++;
|
||||
$y--;
|
|
@ -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,
|
||||
'');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -6,7 +6,7 @@ final class ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule
|
|||
const ID = 73;
|
||||
|
||||
public function getLintName() {
|
||||
return pht('Space After Unary Operator');
|
||||
return pht('Space After Unary Prefix Operator');
|
||||
}
|
||||
|
||||
public function getLintSeverity() {
|
||||
|
@ -31,7 +31,8 @@ final class ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule
|
|||
if (!empty($after)) {
|
||||
$this->raiseLintAtOffset(
|
||||
$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')),
|
||||
'');
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue