1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-22 23:02:41 +01:00

Add a linter rule for list assignments

Summary: Add a linter rule to prevent trailing commas in a list assignment. `list($x, $y,)` is equivalent to `list($x, $y)`, but the latter is cleaner and should be preferred.

Test Plan: Added test cases.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin

Differential Revision: https://secure.phabricator.com/D13870
This commit is contained in:
Joshua Spence 2015-08-12 13:22:37 +10:00
parent 807057087d
commit f4c322cb72
3 changed files with 51 additions and 0 deletions

View file

@ -156,6 +156,7 @@ phutil_register_library_map(array(
'ArcanistLinter' => 'lint/linter/ArcanistLinter.php', 'ArcanistLinter' => 'lint/linter/ArcanistLinter.php',
'ArcanistLinterTestCase' => 'lint/linter/__tests__/ArcanistLinterTestCase.php', 'ArcanistLinterTestCase' => 'lint/linter/__tests__/ArcanistLinterTestCase.php',
'ArcanistLintersWorkflow' => 'workflow/ArcanistLintersWorkflow.php', 'ArcanistLintersWorkflow' => 'workflow/ArcanistLintersWorkflow.php',
'ArcanistListAssignmentXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistListAssignmentXHPASTLinterRule.php',
'ArcanistListWorkflow' => 'workflow/ArcanistListWorkflow.php', 'ArcanistListWorkflow' => 'workflow/ArcanistListWorkflow.php',
'ArcanistLogicalOperatorsXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistLogicalOperatorsXHPASTLinterRule.php', 'ArcanistLogicalOperatorsXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistLogicalOperatorsXHPASTLinterRule.php',
'ArcanistLowercaseFunctionsXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistLowercaseFunctionsXHPASTLinterRule.php', 'ArcanistLowercaseFunctionsXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistLowercaseFunctionsXHPASTLinterRule.php',
@ -435,6 +436,7 @@ phutil_register_library_map(array(
'ArcanistLinter' => 'Phobject', 'ArcanistLinter' => 'Phobject',
'ArcanistLinterTestCase' => 'PhutilTestCase', 'ArcanistLinterTestCase' => 'PhutilTestCase',
'ArcanistLintersWorkflow' => 'ArcanistWorkflow', 'ArcanistLintersWorkflow' => 'ArcanistWorkflow',
'ArcanistListAssignmentXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistListWorkflow' => 'ArcanistWorkflow', 'ArcanistListWorkflow' => 'ArcanistWorkflow',
'ArcanistLogicalOperatorsXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', 'ArcanistLogicalOperatorsXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistLowercaseFunctionsXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', 'ArcanistLowercaseFunctionsXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',

View file

@ -0,0 +1,11 @@
<?php
list($x, $y, , ,) = array();
list($x, $y, , , $z) = array();
~~~~~~~~~~
warning:2:12
warning:2:14
warning:2:16
~~~~~~~~~~
<?php
list($x, $y ) = array();
list($x, $y, , , $z) = array();

View file

@ -0,0 +1,38 @@
<?php
final class ArcanistListAssignmentXHPASTLinterRule
extends ArcanistXHPASTLinterRule {
const ID = 77;
public function getLintName() {
return pht('List Assignment');
}
public function getLintSeverity() {
return ArcanistLintSeverity::SEVERITY_WARNING;
}
public function process(XHPASTNode $root) {
$assignment_lists = $root->selectDescendantsOfType('n_ASSIGNMENT_LIST');
foreach ($assignment_lists as $assignment_list) {
$tokens = array_slice($assignment_list->getTokens(), 1, -1);
foreach (array_reverse($tokens) as $token) {
if ($token->getTypeName() == ',') {
$this->raiseLintAtToken(
$token,
pht('Unnecessary comma in list assignment.'),
'');
continue;
}
if ($token->isSemantic()) {
break;
}
}
}
}
}