mirror of
https://we.phorge.it/source/arcanist.git
synced 2024-11-26 00:32: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:
parent
807057087d
commit
f4c322cb72
3 changed files with 51 additions and 0 deletions
|
@ -156,6 +156,7 @@ phutil_register_library_map(array(
|
|||
'ArcanistLinter' => 'lint/linter/ArcanistLinter.php',
|
||||
'ArcanistLinterTestCase' => 'lint/linter/__tests__/ArcanistLinterTestCase.php',
|
||||
'ArcanistLintersWorkflow' => 'workflow/ArcanistLintersWorkflow.php',
|
||||
'ArcanistListAssignmentXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistListAssignmentXHPASTLinterRule.php',
|
||||
'ArcanistListWorkflow' => 'workflow/ArcanistListWorkflow.php',
|
||||
'ArcanistLogicalOperatorsXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistLogicalOperatorsXHPASTLinterRule.php',
|
||||
'ArcanistLowercaseFunctionsXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistLowercaseFunctionsXHPASTLinterRule.php',
|
||||
|
@ -435,6 +436,7 @@ phutil_register_library_map(array(
|
|||
'ArcanistLinter' => 'Phobject',
|
||||
'ArcanistLinterTestCase' => 'PhutilTestCase',
|
||||
'ArcanistLintersWorkflow' => 'ArcanistWorkflow',
|
||||
'ArcanistListAssignmentXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
|
||||
'ArcanistListWorkflow' => 'ArcanistWorkflow',
|
||||
'ArcanistLogicalOperatorsXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
|
||||
'ArcanistLowercaseFunctionsXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
|
||||
|
|
11
src/lint/linter/__tests__/xhpast/list-assignment.lint-test
Normal file
11
src/lint/linter/__tests__/xhpast/list-assignment.lint-test
Normal 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();
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue