|
| 1 | +import ast |
| 2 | +from typing import Optional, List, Union, Set |
| 3 | + |
| 4 | +from .utils import is_hook_or_element_def, ErrorVisitor |
| 5 | + |
| 6 | + |
| 7 | +HOOKS_WITH_DEPS = ("use_effect", "use_callback", "use_memo") |
| 8 | + |
| 9 | + |
| 10 | +class ExhaustiveDepsVisitor(ErrorVisitor): |
| 11 | + def __init__(self) -> None: |
| 12 | + super().__init__() |
| 13 | + self._current_hook_or_element: Optional[ast.FunctionDef] = None |
| 14 | + |
| 15 | + def visit_FunctionDef(self, node: ast.FunctionDef) -> None: |
| 16 | + if is_hook_or_element_def(node): |
| 17 | + self._current_hook_or_element = node |
| 18 | + self.generic_visit(node) |
| 19 | + self._current_hook_or_element = None |
| 20 | + elif self._current_hook_or_element is not None: |
| 21 | + for deco in node.decorator_list: |
| 22 | + if not isinstance(deco, ast.Call): |
| 23 | + continue |
| 24 | + |
| 25 | + called_func = deco.func |
| 26 | + if isinstance(called_func, ast.Name): |
| 27 | + called_func_name = called_func.id |
| 28 | + elif isinstance(called_func, ast.Attribute): |
| 29 | + called_func_name = called_func.attr |
| 30 | + else: # pragma: no cover |
| 31 | + continue |
| 32 | + |
| 33 | + if called_func_name not in HOOKS_WITH_DEPS: |
| 34 | + continue |
| 35 | + |
| 36 | + for kw in deco.keywords: |
| 37 | + if kw.arg == "args": |
| 38 | + self._check_hook_dependency_list_is_exhaustive( |
| 39 | + called_func_name, |
| 40 | + node, |
| 41 | + kw.value, |
| 42 | + ) |
| 43 | + break |
| 44 | + |
| 45 | + def visit_Call(self, node: ast.Call) -> None: |
| 46 | + if self._current_hook_or_element is None: |
| 47 | + return |
| 48 | + |
| 49 | + called_func = node.func |
| 50 | + |
| 51 | + if isinstance(called_func, ast.Name): |
| 52 | + called_func_name = called_func.id |
| 53 | + elif isinstance(called_func, ast.Attribute): |
| 54 | + called_func_name = called_func.attr |
| 55 | + else: # pragma: no cover |
| 56 | + return None |
| 57 | + |
| 58 | + if called_func_name not in HOOKS_WITH_DEPS: |
| 59 | + return None |
| 60 | + |
| 61 | + func: Optional[ast.expr] = None |
| 62 | + args: Optional[ast.expr] = None |
| 63 | + |
| 64 | + if len(node.args) == 2: |
| 65 | + func, args = node.args |
| 66 | + else: |
| 67 | + if len(node.args) == 1: |
| 68 | + func = node.args[0] |
| 69 | + for kw in node.keywords: |
| 70 | + if kw.arg == "function": |
| 71 | + func = kw.value |
| 72 | + elif kw.arg == "args": |
| 73 | + args = kw.value |
| 74 | + |
| 75 | + if isinstance(func, ast.Lambda): |
| 76 | + self._check_hook_dependency_list_is_exhaustive( |
| 77 | + called_func_name, |
| 78 | + func, |
| 79 | + args, |
| 80 | + ) |
| 81 | + |
| 82 | + def _check_hook_dependency_list_is_exhaustive( |
| 83 | + self, |
| 84 | + hook_name: str, |
| 85 | + func: Union[ast.FunctionDef, ast.Lambda], |
| 86 | + dependency_expr: Optional[ast.expr], |
| 87 | + ) -> None: |
| 88 | + dep_names = self._get_dependency_names_from_expression( |
| 89 | + hook_name, dependency_expr |
| 90 | + ) |
| 91 | + |
| 92 | + if dep_names is None: |
| 93 | + return None |
| 94 | + |
| 95 | + func_name = "lambda" if isinstance(func, ast.Lambda) else func.name |
| 96 | + |
| 97 | + visitor = _MissingNameOrAttrVisitor( |
| 98 | + hook_name, |
| 99 | + func_name, |
| 100 | + _param_names_of_function_def(func), |
| 101 | + dep_names, |
| 102 | + ) |
| 103 | + if isinstance(func.body, list): |
| 104 | + for b in func.body: |
| 105 | + visitor.visit(b) |
| 106 | + else: |
| 107 | + visitor.visit(func.body) |
| 108 | + |
| 109 | + self.errors.extend(visitor.errors) |
| 110 | + |
| 111 | + def _get_dependency_names_from_expression( |
| 112 | + self, hook_name: str, dependency_expr: Optional[ast.expr] |
| 113 | + ) -> Optional[List[str]]: |
| 114 | + if dependency_expr is None: |
| 115 | + return [] |
| 116 | + elif isinstance(dependency_expr, (ast.List, ast.Tuple)): |
| 117 | + dep_names: List[str] = [] |
| 118 | + for elt in dependency_expr.elts: |
| 119 | + if isinstance(elt, ast.Name): |
| 120 | + dep_names.append(elt.id) |
| 121 | + else: |
| 122 | + # ideally we could deal with some common use cases, but since React's |
| 123 | + # own linter doesn't do this we'll just take the easy route for now: |
| 124 | + # https://github.com/facebook/react/issues/16265 |
| 125 | + self._save_error( |
| 126 | + 201, |
| 127 | + elt, |
| 128 | + ( |
| 129 | + f"dependency arg of {hook_name!r} is not destructured - " |
| 130 | + "dependencies should be refered to directly, not via an " |
| 131 | + "attribute or key of an object" |
| 132 | + ), |
| 133 | + ) |
| 134 | + return dep_names |
| 135 | + else: |
| 136 | + self._save_error( |
| 137 | + 202, |
| 138 | + dependency_expr, |
| 139 | + ( |
| 140 | + f"dependency args of {hook_name!r} should be a literal list or " |
| 141 | + f"tuple - not expression type {type(dependency_expr).__name__!r}" |
| 142 | + ), |
| 143 | + ) |
| 144 | + return None |
| 145 | + |
| 146 | + |
| 147 | +class _MissingNameOrAttrVisitor(ErrorVisitor): |
| 148 | + def __init__( |
| 149 | + self, |
| 150 | + hook_name: str, |
| 151 | + func_name: str, |
| 152 | + ignore_names: List[str], |
| 153 | + dep_names: List[str], |
| 154 | + ) -> None: |
| 155 | + super().__init__() |
| 156 | + self._hook_name = hook_name |
| 157 | + self._func_name = func_name |
| 158 | + self._ignore_names = ignore_names |
| 159 | + self._dep_names = dep_names |
| 160 | + self.used_deps: Set[str] = set() |
| 161 | + |
| 162 | + def visit_Name(self, node: ast.Name) -> None: |
| 163 | + node_id = node.id |
| 164 | + if node_id not in self._ignore_names: |
| 165 | + if node_id in self._dep_names: |
| 166 | + self.used_deps.add(node_id) |
| 167 | + else: |
| 168 | + self._save_error( |
| 169 | + 203, |
| 170 | + node, |
| 171 | + ( |
| 172 | + f"dependency {node_id!r} of function {self._func_name!r} " |
| 173 | + f"is not specified in declaration of {self._hook_name!r}" |
| 174 | + ), |
| 175 | + ) |
| 176 | + |
| 177 | + |
| 178 | +def _param_names_of_function_def(func: Union[ast.FunctionDef, ast.Lambda]) -> List[str]: |
| 179 | + names: List[str] = [] |
| 180 | + names.extend(a.arg for a in func.args.args) |
| 181 | + names.extend(kw.arg for kw in func.args.kwonlyargs) |
| 182 | + if func.args.vararg is not None: |
| 183 | + names.append(func.args.vararg.arg) |
| 184 | + if func.args.kwarg is not None: |
| 185 | + names.append(func.args.kwarg.arg) |
| 186 | + return names |
0 commit comments