Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit feefba9

Browse files
committed
improve component decorator check
1 parent f3340f6 commit feefba9

File tree

2 files changed

+21
-41
lines changed

2 files changed

+21
-41
lines changed

flake8_idom_hooks/utils.py

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import re
12
import ast
23
from contextlib import contextmanager
34
from typing import Any, Iterator, List, Tuple
45

56

7+
_COMPONENT_DECORATOR_NAME_PATTERN = re.compile("^(idom.(\w+\.)*)?component$")
8+
9+
610
@contextmanager
711
def set_current(obj: Any, **attrs: Any) -> Iterator[None]:
812
old_attrs = {k: getattr(obj, f"_current_{k}") for k in attrs}
@@ -28,17 +32,25 @@ def is_hook_def(node: ast.FunctionDef) -> bool:
2832

2933

3034
def is_component_def(node: ast.FunctionDef) -> bool:
31-
return any(
32-
is_idom_component_decorator(decorator) for decorator in node.decorator_list
33-
)
35+
return any(map(_is_component_decorator, node.decorator_list))
3436

3537

36-
def is_idom_component_decorator(node: Any) -> bool:
37-
return getattr(node, "id", None) == "component" or (
38-
getattr(getattr(node, "value", None), "id", None) == "idom"
39-
and getattr(node, "attr", None) == "component"
38+
def is_hook_function_name(name: str) -> bool:
39+
return name.lstrip("_").startswith("use_")
40+
41+
42+
def _is_component_decorator(node: ast.AST) -> bool:
43+
return (
44+
_COMPONENT_DECORATOR_NAME_PATTERN.match(
45+
".".join(reversed(list(_get_dotted_name(node))))
46+
)
47+
is not None
4048
)
4149

4250

43-
def is_hook_function_name(name: str) -> bool:
44-
return name.lstrip("_").startswith("use_")
51+
def _get_dotted_name(node: ast.AST) -> Iterator[str]:
52+
while isinstance(node, ast.Attribute):
53+
yield node.attr
54+
node = node.value
55+
if isinstance(node, ast.Name):
56+
yield node.id

tox.ini

-32
This file was deleted.

0 commit comments

Comments
 (0)