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

Commit f16aeaa

Browse files
committed
only check hook usage inside calls
1 parent b646678 commit f16aeaa

File tree

5 files changed

+51
-25
lines changed

5 files changed

+51
-25
lines changed

Diff for: flake8_idom_hooks/rules_of_hooks.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@ def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
4545
with set_current(self, function=node):
4646
self.generic_visit(node)
4747

48+
def visit_Call(self, node: ast.Call) -> None:
49+
with set_current(self, call=node):
50+
self.generic_visit(node)
51+
4852
def _visit_hook_usage(self, node: ast.Name | ast.Attribute) -> None:
49-
self._check_if_propper_hook_usage(node)
53+
if self._current_call:
54+
self._check_if_propper_hook_usage(node)
5055

5156
visit_Attribute = _visit_hook_usage
5257
visit_Name = _visit_hook_usage

Diff for: tests/cases/custom_component_decorator_pattern.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
def check_normal_pattern():
33
if True:
44
# error: ROH102 hook 'use_state' used inside if statement
5-
use_state
5+
use_state()
66

77

88
@custom_component
99
def check_custom_pattern():
1010
if True:
1111
# error: ROH102 hook 'use_state' used inside if statement
12-
use_state
12+
use_state()

Diff for: tests/cases/custom_hook_function_pattern.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
def check():
33
if True:
44
# this get's ignored because of custom pattern
5-
use_ignore_this
5+
use_ignore_this()
66
# error: ROH102 hook 'use_state' used inside if statement
7-
use_state
7+
use_state()

Diff for: tests/cases/hook_usage.py

+40-19
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,29 @@
22
from idom import component
33

44

5+
@component
6+
def HookInIfNoCall():
7+
if True:
8+
# Ok, hook was not called
9+
use_state
10+
11+
512
@component
613
def HookInIf():
714
if True:
815
# error: ROH102 hook 'use_state' used inside if statement
9-
use_state
16+
use_state()
17+
18+
19+
@component
20+
def HookInIfInExpression():
21+
if True:
22+
(
23+
None
24+
or
25+
# error: ROH102 hook 'use_state' used inside if statement
26+
use_state
27+
)()
1028

1129

1230
@component
@@ -15,7 +33,7 @@ def HookInElif():
1533
pass
1634
elif True:
1735
# error: ROH102 hook 'use_state' used inside if statement
18-
use_state
36+
use_state()
1937

2038

2139
@component
@@ -24,14 +42,14 @@ def HookInElse():
2442
pass
2543
else:
2644
# error: ROH102 hook 'use_state' used inside if statement
27-
use_state
45+
use_state()
2846

2947

3048
@component
3149
def HookInIfExp():
3250
(
3351
# error: ROH102 hook 'use_state' used inside inline if expression
34-
use_state
52+
use_state()
3553
if True
3654
else None
3755
)
@@ -44,15 +62,15 @@ def HookInElseOfIfExp():
4462
if True
4563
else
4664
# error: ROH102 hook 'use_state' used inside inline if expression
47-
use_state
65+
use_state()
4866
)
4967

5068

5169
@component
5270
def HookInTry():
5371
try:
5472
# error: ROH102 hook 'use_state' used inside try statement
55-
use_state
73+
use_state()
5674
except:
5775
pass
5876

@@ -63,7 +81,7 @@ def HookInExcept():
6381
raise ValueError()
6482
except:
6583
# error: ROH102 hook 'use_state' used inside try statement
66-
use_state
84+
use_state()
6785

6886

6987
@component
@@ -72,21 +90,21 @@ def HookInFinally():
7290
pass
7391
finally:
7492
# error: ROH102 hook 'use_state' used inside try statement
75-
use_state
93+
use_state()
7694

7795

7896
@component
7997
def HookInForLoop():
8098
for i in range(3):
8199
# error: ROH102 hook 'use_state' used inside for loop
82-
use_state
100+
use_state()
83101

84102

85103
@component
86104
def HookInWhileLoop():
87105
while True:
88106
# error: ROH102 hook 'use_state' used inside while loop
89-
use_state
107+
use_state()
90108

91109

92110
def outer_function():
@@ -97,47 +115,50 @@ def use_state():
97115

98116
def generic_function():
99117
# error: ROH101 hook 'use_state' used outside component or hook definition
100-
use_state
118+
use_state()
101119

102120

103121
@component
104122
def use_state():
105-
use_other
123+
use_other()
106124

107125

108126
@component
109127
def Component():
110-
use_state
128+
use_state()
111129

112130

113131
@idom.component
114132
def IdomLongImportComponent():
115-
use_state
133+
use_state()
116134

117135

118136
@component
119137
def use_custom_hook():
120-
use_state
138+
use_state()
121139

122140

123141
# ok since 'use_state' is not the last attribute
124142
module.use_state.other
125143

144+
# ok since use state is not called
145+
module.use_effect
146+
126147
# error: ROH101 hook 'use_effect' used outside component or hook definition
127148
module.use_effect()
128149

129150

130151
def not_hook_or_component():
131152
# error: ROH101 hook 'use_state' used outside component or hook definition
132-
use_state
153+
use_state()
133154

134155

135156
@component
136157
def make_component():
137158
# nested component definitions are ok.
138159
@component
139160
def NestedComponent():
140-
use_state
161+
use_state()
141162

142163

143164
some_global_variable
@@ -154,11 +175,11 @@ def Component():
154175
@component
155176
def Component():
156177
# this is ok since the conditional is outside the component
157-
use_state
178+
use_state()
158179

159180
@component
160181
def use_other():
161-
use_state
182+
use_state()
162183

163184

164185
@component

Diff for: tests/cases/no_exhaustive_deps.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# confirm that we're still checking for other errors
22
def generic_function():
33
# error: ROH101 hook 'use_state' used outside component or hook definition
4-
use_state
4+
use_state()
55

66

77
@component

0 commit comments

Comments
 (0)