Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove reliance on CamelCase component names #9

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions flake8_idom_hooks/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ast
from contextlib import contextmanager
from typing import List, Tuple, Iterator, Any
from typing import Any, Iterator, List, Tuple


@contextmanager
Expand Down Expand Up @@ -28,11 +28,16 @@ def is_hook_def(node: ast.FunctionDef) -> bool:


def is_component_def(node: ast.FunctionDef) -> bool:
return is_component_function_name(node.name)
return any(
is_idom_component_decorator(decorator) for decorator in node.decorator_list
)


def is_component_function_name(name: str) -> bool:
return name[0].upper() == name[0] and "_" not in name
def is_idom_component_decorator(node: Any) -> bool:
return getattr(node, "id", None) == "component" or (
getattr(getattr(node, "value", None), "id", None) == "idom"
and getattr(node, "attr", None) == "component"
)


def is_hook_function_name(name: str) -> bool:
Expand Down
29 changes: 29 additions & 0 deletions tests/hook_usage_test_cases.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import idom
from idom import component


@component
def HookInIf():
if True:
# error: ROH102 hook 'use_state' used inside if statement
use_state


@component
def HookInElif():
if False:
pass
Expand All @@ -12,6 +18,7 @@ def HookInElif():
use_state


@component
def HookInElse():
if False:
pass
Expand All @@ -20,6 +27,7 @@ def HookInElse():
use_state


@component
def HookInIfExp():
(
# error: ROH102 hook 'use_state' used inside inline if expression
Expand All @@ -29,6 +37,7 @@ def HookInIfExp():
)


@component
def HookInElseOfIfExp():
(
None
Expand All @@ -39,6 +48,7 @@ def HookInElseOfIfExp():
)


@component
def HookInTry():
try:
# error: ROH102 hook 'use_state' used inside try statement
Expand All @@ -47,6 +57,7 @@ def HookInTry():
pass


@component
def HookInExcept():
try:
raise ValueError()
Expand All @@ -55,6 +66,7 @@ def HookInExcept():
use_state


@component
def HookInFinally():
try:
pass
Expand All @@ -63,12 +75,14 @@ def HookInFinally():
use_state


@component
def HookInForLoop():
for i in range(3):
# error: ROH102 hook 'use_state' used inside for loop
use_state


@component
def HookInWhileLoop():
while True:
# error: ROH102 hook 'use_state' used inside while loop
Expand All @@ -82,18 +96,27 @@ def use_state():


def generic_function():

# error: ROH101 hook 'use_state' used outside component or hook definition
use_state


@component
def use_state():
use_other


@component
def Component():
use_state


@idom.component
def IdomLongImportComponent():
use_state


@component
def use_custom_hook():
use_state

Expand All @@ -110,6 +133,7 @@ def not_hook_or_component():
use_state


@component
def CheckEffects():
x = 1
y = 2
Expand Down Expand Up @@ -205,25 +229,30 @@ def impropper_usage_of_effect_as_decorator():
)


@component
def make_component():
# nested component definitions are ok.
@component
def NestedComponent():
use_state


some_global_variable


@component
def Component():
# referencing a global variable is OK
use_effect(lambda: some_global_variable, [])


if True:

@component
def Component():
# this is ok since the conditional is outside the component
use_state

@component
def use_other():
use_state