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

Commit 155c15a

Browse files
committed
improve component decorator check
1 parent 3fbdf28 commit 155c15a

File tree

3 files changed

+28
-41
lines changed

3 files changed

+28
-41
lines changed

flake8_idom_hooks/utils.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import ast
2+
import re
23
from contextlib import contextmanager
34
from typing import Any, Iterator, List, Tuple
45

6+
_COMPONENT_DECORATOR_NAME_PATTERN = re.compile(r"^(idom.(\w+\.)*)?component$")
7+
58

69
@contextmanager
710
def set_current(obj: Any, **attrs: Any) -> Iterator[None]:
@@ -28,17 +31,25 @@ def is_hook_def(node: ast.FunctionDef) -> bool:
2831

2932

3033
def is_component_def(node: ast.FunctionDef) -> bool:
31-
return any(
32-
is_idom_component_decorator(decorator) for decorator in node.decorator_list
33-
)
34+
return any(map(_is_component_decorator, node.decorator_list))
35+
36+
37+
def is_hook_function_name(name: str) -> bool:
38+
return name.lstrip("_").startswith("use_")
3439

3540

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"
41+
def _is_component_decorator(node: ast.AST) -> bool:
42+
return (
43+
_COMPONENT_DECORATOR_NAME_PATTERN.match(
44+
".".join(reversed(list(_get_dotted_name(node))))
45+
)
46+
is not None
4047
)
4148

4249

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

noxfile.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
REQUIREMENTS_DIR = ROOT / "requirements"
77

88

9+
@session
10+
def format(session: Session) -> None:
11+
install_requirements(session, "style")
12+
session.run("black", ".")
13+
session.run("isort", ".")
14+
15+
916
@session
1017
def test(session: Session) -> None:
1118
session.notify("test_style")
@@ -17,6 +24,7 @@ def test(session: Session) -> None:
1724
@session
1825
def test_style(session: Session) -> None:
1926
install_requirements(session, "style")
27+
session.run("black", "--check", ".")
2028
session.run("isort", "--check", ".")
2129
session.run("flake8", ".")
2230

tox.ini

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)