mirror of
https://we.phorge.it/source/arcanist.git
synced 2024-11-10 00:42:40 +01:00
Minor tidying of ArcanistXHPASTLinter
Summary: Modernizing a couple of linter rules. Test Plan: Unit tests. Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: Korvin, epriestley Differential Revision: https://secure.phabricator.com/D12393
This commit is contained in:
parent
58d8656696
commit
eb036465dc
1 changed files with 72 additions and 66 deletions
|
@ -922,11 +922,13 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
||||||
foreach ($blocks as $key => $block) {
|
foreach ($blocks as $key => $block) {
|
||||||
// Collect all the tokens in this block which aren't at top level.
|
// Collect all the tokens in this block which aren't at top level.
|
||||||
// We want to ignore "break", and "continue" in these blocks.
|
// We want to ignore "break", and "continue" in these blocks.
|
||||||
$lower_level = $block->selectDescendantsOfType('n_WHILE');
|
$lower_level = $block->selectDescendantsOfTypes(array(
|
||||||
$lower_level->add($block->selectDescendantsOfType('n_DO_WHILE'));
|
'n_WHILE',
|
||||||
$lower_level->add($block->selectDescendantsOfType('n_FOR'));
|
'n_DO_WHILE',
|
||||||
$lower_level->add($block->selectDescendantsOfType('n_FOREACH'));
|
'n_FOR',
|
||||||
$lower_level->add($block->selectDescendantsOfType('n_SWITCH'));
|
'n_FOREACH',
|
||||||
|
'n_SWITCH',
|
||||||
|
));
|
||||||
$lower_level_tokens = array();
|
$lower_level_tokens = array();
|
||||||
foreach ($lower_level as $lower_level_block) {
|
foreach ($lower_level as $lower_level_block) {
|
||||||
$lower_level_tokens += $lower_level_block->getTokens();
|
$lower_level_tokens += $lower_level_block->getTokens();
|
||||||
|
@ -935,10 +937,14 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
||||||
// Collect all the tokens in this block which aren't in this scope
|
// Collect all the tokens in this block which aren't in this scope
|
||||||
// (because they're inside class, function or interface declarations).
|
// (because they're inside class, function or interface declarations).
|
||||||
// We want to ignore all of these tokens.
|
// We want to ignore all of these tokens.
|
||||||
$decls = $block->selectDescendantsOfType('n_FUNCTION_DECLARATION');
|
$decls = $block->selectDescendantsOfTypes(array(
|
||||||
$decls->add($block->selectDescendantsOfType('n_CLASS_DECLARATION'));
|
'n_FUNCTION_DECLARATION',
|
||||||
|
'n_CLASS_DECLARATION',
|
||||||
|
|
||||||
// For completeness; these can't actually have anything.
|
// For completeness; these can't actually have anything.
|
||||||
$decls->add($block->selectDescendantsOfType('n_INTERFACE_DECLARATION'));
|
'n_INTERFACE_DECLARATION',
|
||||||
|
));
|
||||||
|
|
||||||
$different_scope_tokens = array();
|
$different_scope_tokens = array();
|
||||||
foreach ($decls as $decl) {
|
foreach ($decls as $decl) {
|
||||||
$different_scope_tokens += $decl->getTokens();
|
$different_scope_tokens += $decl->getTokens();
|
||||||
|
@ -1323,9 +1329,10 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
||||||
|
|
||||||
$all_loops = $for_loops->add($foreach_loops);
|
$all_loops = $for_loops->add($foreach_loops);
|
||||||
foreach ($all_loops as $loop) {
|
foreach ($all_loops as $loop) {
|
||||||
$child_for_loops = $loop->selectDescendantsOfType('n_FOR');
|
$child_loops = $loop->selectDescendantsOfTypes(array(
|
||||||
$child_foreach_loops = $loop->selectDescendantsOfType('n_FOREACH');
|
'n_FOR',
|
||||||
$child_loops = $child_for_loops->add($child_foreach_loops);
|
'n_FOREACH',
|
||||||
|
));
|
||||||
|
|
||||||
$outer_vars = $used_vars[$loop->getID()];
|
$outer_vars = $used_vars[$loop->getID()];
|
||||||
foreach ($child_loops as $inner_loop) {
|
foreach ($child_loops as $inner_loop) {
|
||||||
|
@ -1366,10 +1373,10 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
protected function lintReusedIteratorReferences(XHPASTNode $root) {
|
protected function lintReusedIteratorReferences(XHPASTNode $root) {
|
||||||
|
$defs = $root->selectDescendantsOfTypes(array(
|
||||||
$fdefs = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION');
|
'n_FUNCTION_DECLARATION',
|
||||||
$mdefs = $root->selectDescendantsOfType('n_METHOD_DECLARATION');
|
'n_METHOD_DECLARATION',
|
||||||
$defs = $fdefs->add($mdefs);
|
));
|
||||||
|
|
||||||
foreach ($defs as $def) {
|
foreach ($defs as $def) {
|
||||||
|
|
||||||
|
@ -1576,9 +1583,10 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
||||||
// TODO: Support functions defined inside other functions which is commonly
|
// TODO: Support functions defined inside other functions which is commonly
|
||||||
// used with anonymous functions.
|
// used with anonymous functions.
|
||||||
|
|
||||||
$fdefs = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION');
|
$defs = $root->selectDescendantsOfTypes(array(
|
||||||
$mdefs = $root->selectDescendantsOfType('n_METHOD_DECLARATION');
|
'n_FUNCTION_DECLARATION',
|
||||||
$defs = $fdefs->add($mdefs);
|
'n_METHOD_DECLARATION',
|
||||||
|
));
|
||||||
|
|
||||||
foreach ($defs as $def) {
|
foreach ($defs as $def) {
|
||||||
|
|
||||||
|
@ -2103,9 +2111,10 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
||||||
true);
|
true);
|
||||||
|
|
||||||
|
|
||||||
$fdefs = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION');
|
$defs = $root->selectDescendantsOfTypes(array(
|
||||||
$mdefs = $root->selectDescendantsOfType('n_METHOD_DECLARATION');
|
'n_FUNCTION_DECLARATION',
|
||||||
$defs = $fdefs->add($mdefs);
|
'n_METHOD_DECLARATION',
|
||||||
|
));
|
||||||
|
|
||||||
foreach ($defs as $def) {
|
foreach ($defs as $def) {
|
||||||
$globals = $def->selectDescendantsOfType('n_GLOBAL_DECLARATION_LIST');
|
$globals = $def->selectDescendantsOfType('n_GLOBAL_DECLARATION_LIST');
|
||||||
|
@ -2235,17 +2244,14 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
||||||
}
|
}
|
||||||
|
|
||||||
private function lintParenthesesShouldHugExpressions(XHPASTNode $root) {
|
private function lintParenthesesShouldHugExpressions(XHPASTNode $root) {
|
||||||
$calls = $root->selectDescendantsOfType('n_CALL_PARAMETER_LIST');
|
$all_paren_groups = $root->selectDescendantsOfTypes(array(
|
||||||
$controls = $root->selectDescendantsOfType('n_CONTROL_CONDITION');
|
'n_CALL_PARAMETER_LIST',
|
||||||
$fors = $root->selectDescendantsOfType('n_FOR_EXPRESSION');
|
'n_CONTROL_CONDITION',
|
||||||
$foreach = $root->selectDescendantsOfType('n_FOREACH_EXPRESSION');
|
'n_FOR_EXPRESSION',
|
||||||
$decl = $root->selectDescendantsOfType('n_DECLARATION_PARAMETER_LIST');
|
'n_FOREACH_EXPRESSION',
|
||||||
|
'n_DECLARATION_PARAMETER_LIST',
|
||||||
|
));
|
||||||
|
|
||||||
$all_paren_groups = $calls
|
|
||||||
->add($controls)
|
|
||||||
->add($fors)
|
|
||||||
->add($foreach)
|
|
||||||
->add($decl);
|
|
||||||
foreach ($all_paren_groups as $group) {
|
foreach ($all_paren_groups as $group) {
|
||||||
$tokens = $group->getTokens();
|
$tokens = $group->getTokens();
|
||||||
|
|
||||||
|
@ -2477,10 +2483,9 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
||||||
}
|
}
|
||||||
|
|
||||||
private function lintDynamicDefines(XHPASTNode $root) {
|
private function lintDynamicDefines(XHPASTNode $root) {
|
||||||
$calls = $root->selectDescendantsOfType('n_FUNCTION_CALL');
|
$calls = $this->getFunctionCalls($root, array('define'));
|
||||||
|
|
||||||
foreach ($calls as $call) {
|
foreach ($calls as $call) {
|
||||||
$name = $call->getChildByIndex(0)->getConcreteString();
|
|
||||||
if (strtolower($name) === 'define') {
|
|
||||||
$parameter_list = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
|
$parameter_list = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
|
||||||
$defined = $parameter_list->getChildByIndex(0);
|
$defined = $parameter_list->getChildByIndex(0);
|
||||||
if (!$defined->isStaticScalar()) {
|
if (!$defined->isStaticScalar()) {
|
||||||
|
@ -2493,7 +2498,6 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private function lintUseOfThisInStaticMethods(XHPASTNode $root) {
|
private function lintUseOfThisInStaticMethods(XHPASTNode $root) {
|
||||||
$classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION');
|
$classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION');
|
||||||
|
@ -2549,10 +2553,9 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
||||||
* wrong.
|
* wrong.
|
||||||
*/
|
*/
|
||||||
private function lintPregQuote(XHPASTNode $root) {
|
private function lintPregQuote(XHPASTNode $root) {
|
||||||
$function_calls = $root->selectDescendantsOfType('n_FUNCTION_CALL');
|
$function_calls = $this->getFunctionCalls($root, array('preg_quote'));
|
||||||
|
|
||||||
foreach ($function_calls as $call) {
|
foreach ($function_calls as $call) {
|
||||||
$name = $call->getChildByIndex(0)->getConcreteString();
|
|
||||||
if (strtolower($name) === 'preg_quote') {
|
|
||||||
$parameter_list = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
|
$parameter_list = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
|
||||||
if (count($parameter_list->getChildren()) !== 2) {
|
if (count($parameter_list->getChildren()) !== 2) {
|
||||||
$this->raiseLintAtNode(
|
$this->raiseLintAtNode(
|
||||||
|
@ -2569,7 +2572,6 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exit is parsed as an expression, but using it as such is almost always
|
* Exit is parsed as an expression, but using it as such is almost always
|
||||||
|
@ -2780,8 +2782,10 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
||||||
}
|
}
|
||||||
|
|
||||||
private function lintClosingCallParen(XHPASTNode $root) {
|
private function lintClosingCallParen(XHPASTNode $root) {
|
||||||
$calls = $root->selectDescendantsOfType('n_FUNCTION_CALL');
|
$calls = $root->selectDescendantsOfTypes(array(
|
||||||
$calls = $calls->add($root->selectDescendantsOfType('n_METHOD_CALL'));
|
'n_FUNCTION_CALL',
|
||||||
|
'n_METHOD_CALL',
|
||||||
|
));
|
||||||
|
|
||||||
foreach ($calls as $call) {
|
foreach ($calls as $call) {
|
||||||
// If the last parameter of a call is a HEREDOC, don't apply this rule.
|
// If the last parameter of a call is a HEREDOC, don't apply this rule.
|
||||||
|
@ -2813,8 +2817,10 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
||||||
}
|
}
|
||||||
|
|
||||||
private function lintClosingDeclarationParen(XHPASTNode $root) {
|
private function lintClosingDeclarationParen(XHPASTNode $root) {
|
||||||
$decs = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION');
|
$decs = $root->selectDescendantsOfTypes(array(
|
||||||
$decs = $decs->add($root->selectDescendantsOfType('n_METHOD_DECLARATION'));
|
'n_FUNCTION_DECLARATION',
|
||||||
|
'n_METHOD_DECLARATION',
|
||||||
|
));
|
||||||
|
|
||||||
foreach ($decs as $dec) {
|
foreach ($decs as $dec) {
|
||||||
$params = $dec->getChildOfType(3, 'n_DECLARATION_PARAMETER_LIST');
|
$params = $dec->getChildOfType(3, 'n_DECLARATION_PARAMETER_LIST');
|
||||||
|
|
Loading…
Reference in a new issue