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

Commit

Permalink
another fix for early return + add early return lineno in msg
Browse files Browse the repository at this point in the history
  • Loading branch information
rmorshea committed Oct 7, 2022
1 parent 81c9ca9 commit 6c5abe7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 3 additions & 2 deletions flake8_idom_hooks/rules_of_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def _visit_loop(self, node: ast.AST) -> None:
visit_While = _visit_loop

def visit_Return(self, node: ast.Return) -> None:
self._current_early_return = node
if self._current_component is self._current_function:
self._current_early_return = node

def _check_if_hook_defined_in_function(self, node: ast.FunctionDef) -> None:
if self._current_function is not None:
Expand Down Expand Up @@ -111,7 +112,7 @@ def _check_if_propper_hook_usage(self, node: ast.Name | ast.Attribute) -> None:
self._context.add_error(
103,
node,
f"hook {name!r} used after an early return",
f"hook {name!r} used after an early return on line {self._current_early_return.lineno}",
)


Expand Down
10 changes: 9 additions & 1 deletion tests/cases/hook_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def use_other():
def example():
if True:
return None
# error: ROH103 hook 'use_state' used after an early return
# error: ROH103 hook 'use_state' used after an early return on line 190
use_state()


Expand All @@ -211,3 +211,11 @@ def some_effect():

# Ok: no early return error
use_state()


@idom.component
def regression_check():
@use_effect
def effect():
# this return caused false passitive early return error in use_effect usage
return cleanup

0 comments on commit 6c5abe7

Please sign in to comment.