|
2 | 2 | from contextlib import contextmanager |
3 | 3 | from typing import Iterator, Union, Optional, Any |
4 | 4 |
|
5 | | -from .utils import is_hook_or_element_def, ErrorVisitor, is_hook_function_name |
| 5 | +from .utils import is_hook_def, is_element_def, ErrorVisitor, is_hook_function_name |
6 | 6 |
|
7 | 7 |
|
8 | 8 | class RulesOfHooksVisitor(ErrorVisitor): |
9 | 9 | def __init__(self) -> None: |
10 | 10 | super().__init__() |
11 | | - self._current_hook_or_element: Optional[ast.FunctionDef] = None |
| 11 | + self._current_hook: Optional[ast.FunctionDef] = None |
| 12 | + self._current_element: Optional[ast.FunctionDef] = None |
12 | 13 | self._current_function: Optional[ast.FunctionDef] = None |
13 | 14 | self._current_call: Optional[ast.Call] = None |
14 | 15 | self._current_conditional: Union[None, ast.If, ast.IfExp, ast.Try] = None |
15 | 16 | self._current_loop: Union[None, ast.For, ast.While] = None |
16 | 17 |
|
17 | 18 | def visit_FunctionDef(self, node: ast.FunctionDef) -> None: |
18 | | - if is_hook_or_element_def(node): |
| 19 | + if is_hook_def(node): |
19 | 20 | self._check_if_hook_defined_in_function(node) |
20 | | - with self._set_current(hook_or_element=node, function=node): |
| 21 | + with self._set_current(hook=node, function=node): |
| 22 | + self.generic_visit(node) |
| 23 | + elif is_element_def(node): |
| 24 | + with self._set_current(element=node, function=node): |
21 | 25 | self.generic_visit(node) |
22 | 26 | else: |
23 | 27 | with self._set_current(function=node): |
@@ -60,7 +64,7 @@ def _check_if_propper_hook_usage( |
60 | 64 | if not is_hook_function_name(name): |
61 | 65 | return None |
62 | 66 |
|
63 | | - if self._current_hook_or_element is None: |
| 67 | + if self._current_hook is None and self._current_element is None: |
64 | 68 | msg = f"hook {name!r} used outside element or hook definition" |
65 | 69 | self._save_error(101, node, msg) |
66 | 70 |
|
|
0 commit comments