1+ from idom import component
2+
3+
4+ @component
15def HookInIf ():
26 if True :
37 # error: ROH102 hook 'use_state' used inside if statement
48 use_state
59
610
11+ @component
712def HookInElif ():
813 if False :
914 pass
@@ -12,6 +17,7 @@ def HookInElif():
1217 use_state
1318
1419
20+ @component
1521def HookInElse ():
1622 if False :
1723 pass
@@ -20,6 +26,7 @@ def HookInElse():
2026 use_state
2127
2228
29+ @component
2330def HookInIfExp ():
2431 (
2532 # error: ROH102 hook 'use_state' used inside inline if expression
@@ -29,6 +36,7 @@ def HookInIfExp():
2936 )
3037
3138
39+ @component
3240def HookInElseOfIfExp ():
3341 (
3442 None
@@ -39,6 +47,7 @@ def HookInElseOfIfExp():
3947 )
4048
4149
50+ @component
4251def HookInTry ():
4352 try :
4453 # error: ROH102 hook 'use_state' used inside try statement
@@ -47,6 +56,7 @@ def HookInTry():
4756 pass
4857
4958
59+ @component
5060def HookInExcept ():
5161 try :
5262 raise ValueError ()
@@ -55,6 +65,7 @@ def HookInExcept():
5565 use_state
5666
5767
68+ @component
5869def HookInFinally ():
5970 try :
6071 pass
@@ -63,37 +74,45 @@ def HookInFinally():
6374 use_state
6475
6576
77+ @component
6678def HookInForLoop ():
6779 for i in range (3 ):
6880 # error: ROH102 hook 'use_state' used inside for loop
6981 use_state
7082
7183
84+ @component
7285def HookInWhileLoop ():
7386 while True :
7487 # error: ROH102 hook 'use_state' used inside while loop
7588 use_state
7689
7790
91+ @component
7892def outer_function ():
7993 # error: ROH100 hook 'use_state' defined as closure in function 'outer_function'
94+ @component
8095 def use_state ():
8196 ...
8297
8398
99+ @component
84100def generic_function ():
85101 # error: ROH101 hook 'use_state' used outside component or hook definition
86102 use_state
87103
88104
105+ @component
89106def use_state ():
90107 use_other
91108
92109
110+ @component
93111def Component ():
94112 use_state
95113
96114
115+ @component
97116def use_custom_hook ():
98117 use_state
99118
@@ -105,11 +124,13 @@ def use_custom_hook():
105124module .use_effect ()
106125
107126
127+ @component
108128def not_hook_or_component ():
109129 # error: ROH101 hook 'use_state' used outside component or hook definition
110130 use_state
111131
112132
133+ @component
113134def CheckEffects ():
114135 x = 1
115136 y = 2
@@ -166,34 +187,41 @@ def CheckEffects():
166187 )
167188
168189 @use_effect (args = [x ])
190+ @component
169191 def my_effect ():
170192 x
171193
172194 @use_effect (args = [])
195+ @component
173196 def my_effect ():
174197 # error: ROH202 dependency 'x' of function 'my_effect' is not specified in declaration of 'use_effect'
175198 x
176199
177200 @use_effect (args = [])
178201 @some_other_deco_that_adds_args_to_func_somehow
202+ @component
179203 def my_effect (* args , ** kwargs ):
180204 args
181205 kwargs
182206
183207 @module .use_effect (args = [])
208+ @component
184209 def my_effect ():
185210 # error: ROH202 dependency 'x' of function 'my_effect' is not specified in declaration of 'use_effect'
186211 x
187212
188213 @not_a_decorator_we_care_about
214+ @component
189215 def some_func ():
190216 ...
191217
192218 @not_a_decorator_we_care_about ()
219+ @component
193220 def some_func ():
194221 ...
195222
196223 @use_effect
224+ @component
197225 def impropper_usage_of_effect_as_decorator ():
198226 # ignored because bad useage
199227 x
@@ -210,25 +238,30 @@ def impropper_usage_of_effect_as_decorator():
210238 )
211239
212240
241+ @component
213242def make_component ():
214243 # nested component definitions are ok.
244+ @component
215245 def NestedComponent ():
216246 use_state
217247
218248
219249some_global_variable
220250
221251
252+ @component
222253def Component ():
223254 # referencing a global variable is OK
224255 use_effect (lambda : some_global_variable , [])
225256
226257
227258if True :
228259
260+ @component
229261 def Component ():
230262 # this is ok since the conditional is outside the component
231263 use_state
232264
265+ @component
233266 def use_other ():
234267 use_state
0 commit comments