]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Implement hook for checking switch lint
authorvrana <jakubv@fb.com>
Tue, 5 Feb 2013 19:00:07 +0000 (11:00 -0800)
committervrana <jakubv@fb.com>
Tue, 5 Feb 2013 19:46:59 +0000 (11:46 -0800)
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
src/lint/linter/ArcanistXHPASTLinter.php
src/lint/linter/xhpast/ArcanistXHPASTLintSwitchHook.php [new file with mode: 0644]

index ddd71167e11ef654de5b18c0898eefb82df0afdf..b41fe26475a434ef8a0918eafb4685b754b2f5bc 100644 (file)
@@ -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',
index f078f2136506a59eeb031857655b775fb9068462..70e575d22930c0231d8afce45b4572374dfd5ce2 100644 (file)
@@ -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 (file)
index 0000000..b7e4c33
--- /dev/null
@@ -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);
+
+}