1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-26 08:42:40 +01:00

Add a linter rule for variable reference spacing

Summary: Adds a linter rule for spacing after the `&` token, similar to `ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRule`.

Test Plan: Added test cases.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin

Differential Revision: https://secure.phabricator.com/D14602
This commit is contained in:
Joshua Spence 2015-12-01 07:16:35 +11:00
parent 578f540a92
commit 87306cfe14
5 changed files with 75 additions and 0 deletions

View file

@ -373,6 +373,8 @@ phutil_register_library_map(array(
'ArcanistUselessOverridingMethodXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUselessOverridingMethodXHPASTLinterRule.php', 'ArcanistUselessOverridingMethodXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUselessOverridingMethodXHPASTLinterRule.php',
'ArcanistUselessOverridingMethodXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistUselessOverridingMethodXHPASTLinterRuleTestCase.php', 'ArcanistUselessOverridingMethodXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistUselessOverridingMethodXHPASTLinterRuleTestCase.php',
'ArcanistUserAbortException' => 'exception/usage/ArcanistUserAbortException.php', 'ArcanistUserAbortException' => 'exception/usage/ArcanistUserAbortException.php',
'ArcanistVariableReferenceSpacingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistVariableReferenceSpacingXHPASTLinterRule.php',
'ArcanistVariableReferenceSpacingXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistVariableReferenceSpacingXHPASTLinterRuleTestCase.php',
'ArcanistVariableVariableXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistVariableVariableXHPASTLinterRule.php', 'ArcanistVariableVariableXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistVariableVariableXHPASTLinterRule.php',
'ArcanistVariableVariableXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistVariableVariableXHPASTLinterRuleTestCase.php', 'ArcanistVariableVariableXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistVariableVariableXHPASTLinterRuleTestCase.php',
'ArcanistVersionWorkflow' => 'workflow/ArcanistVersionWorkflow.php', 'ArcanistVersionWorkflow' => 'workflow/ArcanistVersionWorkflow.php',
@ -769,6 +771,8 @@ phutil_register_library_map(array(
'ArcanistUselessOverridingMethodXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', 'ArcanistUselessOverridingMethodXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistUselessOverridingMethodXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase', 'ArcanistUselessOverridingMethodXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistUserAbortException' => 'ArcanistUsageException', 'ArcanistUserAbortException' => 'ArcanistUsageException',
'ArcanistVariableReferenceSpacingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistVariableReferenceSpacingXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistVariableVariableXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', 'ArcanistVariableVariableXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistVariableVariableXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase', 'ArcanistVariableVariableXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistVersionWorkflow' => 'ArcanistWorkflow', 'ArcanistVersionWorkflow' => 'ArcanistWorkflow',

View file

@ -0,0 +1,34 @@
<?php
final class ArcanistVariableReferenceSpacingXHPASTLinterRule
extends ArcanistXHPASTLinterRule {
const ID = 123;
public function getLintName() {
return pht('Variable Reference Spacing');
}
public function getLintSeverity() {
return ArcanistLintSeverity::SEVERITY_WARNING;
}
public function process(XHPASTNode $root) {
$references = $root->selectDescendantsOfType('n_VARIABLE_REFERENCE');
foreach ($references as $reference) {
$variable = $reference->getChildByIndex(0);
list($before, $after) = $variable->getSurroundingNonsemanticTokens();
if ($before) {
$this->raiseLintAtOffset(
head($before)->getOffset(),
pht('Variable references should not be prefixed with whitespace.'),
implode('', mpull($before, 'getValue')),
'');
}
}
}
}

View file

@ -0,0 +1,11 @@
<?php
final class ArcanistVariableReferenceSpacingXHPASTLinterRuleTestCase
extends ArcanistXHPASTLinterRuleTestCase {
public function testLinter() {
$this->executeTestsInDirectory(
dirname(__FILE__).'/variable-reference-spacing/');
}
}

View file

@ -0,0 +1,13 @@
<?php
$x = &some_function();
$x = & some_function();
$x = &
some_function();
~~~~~~~~~~
warning:3:7
warning:4:7
~~~~~~~~~~
<?php
$x = &some_function();
$x = &some_function();
$x = &some_function();

View file

@ -0,0 +1,13 @@
<?php
$x = &$a;
$x = & $b;
$x = &
$c;
~~~~~~~~~~
warning:3:7
warning:4:7
~~~~~~~~~~
<?php
$x = &$a;
$x = &$b;
$x = &$c;