mirror of
https://we.phorge.it/source/arcanist.git
synced 2025-02-02 09:58:23 +01:00
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
This commit is contained in:
parent
be73bd8716
commit
bd71ba675e
3 changed files with 30 additions and 1 deletions
|
@ -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',
|
||||
|
|
|
@ -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;
|
||||
|
|
17
src/lint/linter/xhpast/ArcanistXHPASTLintSwitchHook.php
Normal file
17
src/lint/linter/xhpast/ArcanistXHPASTLintSwitchHook.php
Normal file
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* You can extend this class and set `lint.xhpast.switchhook` in your
|
||||
* `.arcconfig` to have an opportunity to override results for linting `switch`
|
||||
* statements.
|
||||
*
|
||||
* @group lint
|
||||
*/
|
||||
abstract class ArcanistXHPASTLintSwitchHook {
|
||||
|
||||
/**
|
||||
* @return bool True if token safely ends the block.
|
||||
*/
|
||||
abstract public function checkSwitchToken(XHPASTToken $token);
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue