From: Joshua Spence Date: Wed, 5 Aug 2015 20:57:16 +0000 (+1000) Subject: Improve undeclared variables linter rule X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=bf4e7d4ca89a9ab6eb17f5dba4d3f3b6d6dc4c08;p=arcanist.git Improve undeclared variables linter rule Summary: Currently `ArcanistUndeclaredVariableXHPASTLinterRule` does not properly handle anonymous closures. Test Plan: Added test cases. Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: johnny-bit, Korvin Differential Revision: https://secure.phabricator.com/D13795 --- diff --git a/src/lint/linter/__tests__/xhpast/undeclared-variables.lint-test b/src/lint/linter/__tests__/xhpast/undeclared-variables.lint-test index 3b07c19c..903e620a 100644 --- a/src/lint/linter/__tests__/xhpast/undeclared-variables.lint-test +++ b/src/lint/linter/__tests__/xhpast/undeclared-variables.lint-test @@ -172,6 +172,13 @@ function catchy() { } } +function some_func($x, $y) { + $func = function($z) use ($x) { + echo $x; + echo $y; + echo $z; + }; +} ~~~~~~~~~~ error:28:3 error:30:3 @@ -191,3 +198,4 @@ error:144:8 error:150:9 error:164:9 error:171:5 +error:178:10 diff --git a/src/lint/linter/xhpast/rules/ArcanistUndeclaredVariableXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistUndeclaredVariableXHPASTLinterRule.php index afb07968..c60b216c 100644 --- a/src/lint/linter/xhpast/rules/ArcanistUndeclaredVariableXHPASTLinterRule.php +++ b/src/lint/linter/xhpast/rules/ArcanistUndeclaredVariableXHPASTLinterRule.php @@ -164,6 +164,17 @@ final class ArcanistUndeclaredVariableXHPASTLinterRule $scope_destroyed_at = min($scope_destroyed_at, $call->getOffset()); } + $func_decls = $body->selectDescendantsOfType('n_FUNCTION_DECLARATION'); + foreach ($func_decls as $func_decl) { + if ($func_decl->getChildByIndex(2)->getTypeName() != 'n_EMPTY') { + continue; + } + + foreach ($func_decl->selectDescendantsOfType('n_VARIABLE') as $var) { + $exclude_tokens[$var->getID()] = true; + } + } + // Now we have every declaration except foreach(), handled below. Build // two maps, one which just keeps track of which tokens are part of // declarations ($declaration_tokens) and one which has the first offset