From bd71ba675e1f950bf551bb748ca8ea8faf3c61d7 Mon Sep 17 00:00:00 2001 From: vrana Date: Tue, 5 Feb 2013 11:00:07 -0800 Subject: [PATCH] Implement hook for checking switch lint Summary: We want to use it for `yield` and `invariant_violation()` which throws. Having node instead of token would be better but this would be enough. Test Plan: Implemented a hook in FB repo and added a test case there. Reviewers: epriestley Reviewed By: epriestley CC: aran, Korvin Differential Revision: https://secure.phabricator.com/D4821 --- src/__phutil_library_map__.php | 1 + src/lint/linter/ArcanistXHPASTLinter.php | 13 ++++++++++++- .../xhpast/ArcanistXHPASTLintSwitchHook.php | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/lint/linter/xhpast/ArcanistXHPASTLintSwitchHook.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index ddd71167..b41fe264 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -145,6 +145,7 @@ phutil_register_library_map(array( 'ArcanistWorkingCopyIdentity' => 'workingcopyidentity/ArcanistWorkingCopyIdentity.php', 'ArcanistXHPASTLintNamingHook' => 'lint/linter/xhpast/ArcanistXHPASTLintNamingHook.php', 'ArcanistXHPASTLintNamingHookTestCase' => 'lint/linter/xhpast/__tests__/ArcanistXHPASTLintNamingHookTestCase.php', + 'ArcanistXHPASTLintSwitchHook' => 'lint/linter/xhpast/ArcanistXHPASTLintSwitchHook.php', 'ArcanistXHPASTLinter' => 'lint/linter/ArcanistXHPASTLinter.php', 'ArcanistXHPASTLinterTestCase' => 'lint/linter/__tests__/ArcanistXHPASTLinterTestCase.php', 'ComprehensiveLintEngine' => 'lint/engine/ComprehensiveLintEngine.php', diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php index f078f213..70e575d2 100644 --- a/src/lint/linter/ArcanistXHPASTLinter.php +++ b/src/lint/linter/ArcanistXHPASTLinter.php @@ -452,6 +452,16 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { } private function lintImplicitFallthrough($root) { + $hook_obj = null; + $working_copy = $this->getEngine()->getWorkingCopy(); + if ($working_copy) { + $hook_class = $working_copy->getConfig('lint.xhpast.switchhook'); + if ($hook_class) { + $hook_obj = newv($hook_class, array()); + assert_instances_of(array($hook_obj), 'ArcanistXHPASTLintSwitchHook'); + } + } + $switches = $root->selectDescendantsOfType('n_SWITCH'); foreach ($switches as $switch) { $blocks = array(); @@ -574,7 +584,8 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { if ($tok_type == 'T_RETURN' || $tok_type == 'T_THROW' || - $tok_type == 'T_EXIT') { + $tok_type == 'T_EXIT' || + ($hook_obj && $hook_obj->checkSwitchToken($token))) { if (empty($different_scope_tokens[$token_id])) { $statement_ok = true; $block_ok = true; diff --git a/src/lint/linter/xhpast/ArcanistXHPASTLintSwitchHook.php b/src/lint/linter/xhpast/ArcanistXHPASTLintSwitchHook.php new file mode 100644 index 00000000..b7e4c339 --- /dev/null +++ b/src/lint/linter/xhpast/ArcanistXHPASTLintSwitchHook.php @@ -0,0 +1,17 @@ +