mirror of
https://we.phorge.it/source/arcanist.git
synced 2024-11-26 00:32:41 +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',
|
||||||
// For completeness; these can't actually have anything.
|
'n_CLASS_DECLARATION',
|
||||||
$decls->add($block->selectDescendantsOfType('n_INTERFACE_DECLARATION'));
|
|
||||||
|
// For completeness; these can't actually have anything.
|
||||||
|
'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,20 +2483,18 @@ 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();
|
$parameter_list = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
|
||||||
if (strtolower($name) === 'define') {
|
$defined = $parameter_list->getChildByIndex(0);
|
||||||
$parameter_list = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
|
if (!$defined->isStaticScalar()) {
|
||||||
$defined = $parameter_list->getChildByIndex(0);
|
$this->raiseLintAtNode(
|
||||||
if (!$defined->isStaticScalar()) {
|
$defined,
|
||||||
$this->raiseLintAtNode(
|
self::LINT_DYNAMIC_DEFINE,
|
||||||
$defined,
|
pht(
|
||||||
self::LINT_DYNAMIC_DEFINE,
|
'First argument to %s must be a string literal.',
|
||||||
pht(
|
'define()'));
|
||||||
'First argument to %s must be a string literal.',
|
|
||||||
'define()'));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2549,24 +2553,22 @@ 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();
|
$parameter_list = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
|
||||||
if (strtolower($name) === 'preg_quote') {
|
if (count($parameter_list->getChildren()) !== 2) {
|
||||||
$parameter_list = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
|
$this->raiseLintAtNode(
|
||||||
if (count($parameter_list->getChildren()) !== 2) {
|
$call,
|
||||||
$this->raiseLintAtNode(
|
self::LINT_PREG_QUOTE_MISUSE,
|
||||||
$call,
|
pht(
|
||||||
self::LINT_PREG_QUOTE_MISUSE,
|
'If you use pattern delimiters that require escaping '.
|
||||||
pht(
|
'(such as `%s`, but not `%s`) then you should pass two '.
|
||||||
'If you use pattern delimiters that require escaping '.
|
'arguments to %s, so that %s knows which delimiter to escape.',
|
||||||
'(such as `%s`, but not `%s`) then you should pass two '.
|
'//',
|
||||||
'arguments to %s, so that %s knows which delimiter to escape.',
|
'()',
|
||||||
'//',
|
'preg_quote()',
|
||||||
'()',
|
'preg_quote()'));
|
||||||
'preg_quote()',
|
|
||||||
'preg_quote()'));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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