@@ -5,178 +5,145 @@ import { allValid, ruleTester } from "../../../../../test";
5
5
import rule , { RULE_NAME } from "./prefer-use-state-lazy-initialization" ;
6
6
7
7
ruleTester . run ( RULE_NAME , rule , {
8
- invalid : ( [
9
- [ "getValue()" , T . CallExpression ] ,
10
- [ "getValue(1, 2, 3)" , T . CallExpression ] ,
11
- [ "new Foo()" , T . NewExpression ] ,
12
- ] satisfies [ string , T ] [ ] ) . flatMap ( ( [ expression , type ] ) => [
8
+ invalid : [
13
9
{
14
- code : `import { useState } from "react"; useState(1 || ${ expression } )` ,
10
+ code : `import { useState } from "react"; useState(1 || getValue() )` ,
15
11
errors : [
16
12
{
17
- type : T . LogicalExpression ,
13
+ type : T . CallExpression ,
18
14
messageId : "preferUseStateLazyInitialization" ,
19
15
} ,
20
16
] ,
21
17
} ,
22
18
{
23
- code : `import { useState } from "react"; useState(2 < ${ expression } )` ,
19
+ code : `import { useState } from "react"; useState(2 < getValue() )` ,
24
20
errors : [
25
21
{
26
- type : T . BinaryExpression ,
22
+ type : T . CallExpression ,
27
23
messageId : "preferUseStateLazyInitialization" ,
28
24
} ,
29
25
] ,
30
26
} ,
31
27
{
32
- code : `import { useState } from "react"; useState(${ expression } )` ,
28
+ code : `import { useState } from "react"; useState(1 < 2 ? getValue() : 4 )` ,
33
29
errors : [
34
30
{
35
- type,
31
+ type : T . CallExpression ,
36
32
messageId : "preferUseStateLazyInitialization" ,
37
33
} ,
38
34
] ,
39
35
} ,
40
36
{
41
- code : `import { useState } from "react"; useState(a ? b : ${ expression } )` ,
37
+ code : `import { useState } from "react"; useState(a ? b : getValue() )` ,
42
38
errors : [
43
39
{
44
- type : T . ConditionalExpression ,
40
+ type : T . CallExpression ,
45
41
messageId : "preferUseStateLazyInitialization" ,
46
42
} ,
47
43
] ,
48
44
} ,
49
45
{
50
- code : `import { useState } from "react"; useState(${ expression } ? b : c)` ,
46
+ code : `import { useState } from "react"; useState(getValue() ? b : c)` ,
51
47
errors : [
52
48
{
53
- type : T . ConditionalExpression ,
49
+ type : T . CallExpression ,
54
50
messageId : "preferUseStateLazyInitialization" ,
55
51
} ,
56
52
] ,
57
53
} ,
58
54
{
59
- code : `import { useState } from "react"; useState(a ? (b ? ${ expression } : b2) : c)` ,
55
+ code : `import { useState } from "react"; useState(a ? (b ? getValue() : b2) : c)` ,
60
56
errors : [
61
57
{
62
- type : T . ConditionalExpression ,
58
+ type : T . CallExpression ,
63
59
messageId : "preferUseStateLazyInitialization" ,
64
60
} ,
65
61
] ,
66
62
} ,
67
63
{
68
- code : `import { useState } from "react"; useState(${ expression } && b)` ,
64
+ code : `import { useState } from "react"; useState(getValue() && b)` ,
69
65
errors : [
70
66
{
71
- type : T . LogicalExpression ,
67
+ type : T . CallExpression ,
72
68
messageId : "preferUseStateLazyInitialization" ,
73
69
} ,
74
70
] ,
75
71
} ,
76
72
{
77
- code : `import { useState } from "react"; useState(a && ${ expression } )` ,
73
+ code : `import { useState } from "react"; useState(a() && new Foo() )` ,
78
74
errors : [
79
75
{
80
- type : T . LogicalExpression ,
76
+ type : T . CallExpression ,
81
77
messageId : "preferUseStateLazyInitialization" ,
82
78
} ,
83
- ] ,
84
- } ,
85
- {
86
- code : `import { useState } from "react"; useState(${ expression } && b())` ,
87
- errors : [
88
- {
89
- type : T . LogicalExpression ,
90
- messageId : "preferUseStateLazyInitialization" ,
91
- } ,
92
- ] ,
93
- } ,
94
- {
95
- code : `import { useState } from "react"; useState(a() && ${ expression } )` ,
96
- errors : [
97
- {
98
- type : T . LogicalExpression ,
99
- messageId : "preferUseStateLazyInitialization" ,
100
- } ,
101
- ] ,
102
- } ,
103
- {
104
- code : `import { useState } from "react"; useState(+${ expression } )` ,
105
- errors : [
106
79
{
107
- type : T . UnaryExpression ,
80
+ type : T . NewExpression ,
108
81
messageId : "preferUseStateLazyInitialization" ,
109
82
} ,
110
83
] ,
111
84
} ,
112
85
{
113
- code : `import { useState } from "react"; useState(- ${ expression } )` ,
86
+ code : `import { useState } from "react"; useState(+getValue() )` ,
114
87
errors : [
115
88
{
116
- type : T . UnaryExpression ,
89
+ type : T . CallExpression ,
117
90
messageId : "preferUseStateLazyInitialization" ,
118
91
} ,
119
92
] ,
120
93
} ,
121
94
{
122
- code : `import { useState } from "react"; useState(~ ${ expression } )` ,
95
+ code : `import { useState } from "react"; useState(getValue() + 1 )` ,
123
96
errors : [
124
97
{
125
- type : T . UnaryExpression ,
98
+ type : T . CallExpression ,
126
99
messageId : "preferUseStateLazyInitialization" ,
127
100
} ,
128
101
] ,
129
102
} ,
130
103
{
131
- code : `import { useState } from "react"; useState(! ${ expression } )` ,
104
+ code : `import { useState } from "react"; useState([getValue()] )` ,
132
105
errors : [
133
106
{
134
- type : T . UnaryExpression ,
107
+ type : T . CallExpression ,
135
108
messageId : "preferUseStateLazyInitialization" ,
136
109
} ,
137
110
] ,
138
111
} ,
139
112
{
140
- code : `import { useState } from "react"; useState(${ expression } + 1 )` ,
113
+ code : `import { useState } from "react"; useState({ a: getValue() } )` ,
141
114
errors : [
142
115
{
143
- type : T . BinaryExpression ,
116
+ type : T . CallExpression ,
144
117
messageId : "preferUseStateLazyInitialization" ,
145
118
} ,
146
119
] ,
147
120
} ,
148
121
{
149
- code : `import { useState } from "react"; useState(${ expression } - 1)` ,
150
- errors : [
151
- {
152
- type : T . BinaryExpression ,
153
- messageId : "preferUseStateLazyInitialization" ,
154
- } ,
155
- ] ,
156
- } ,
157
- {
158
- code : `import { useState } from "react"; useState([${ expression } ])` ,
122
+ code : tsx `
123
+ import { useState, use } from 'react';
124
+
125
+ function Component({data}) {
126
+ const [data, setData] = useState(data ? use(data) : getValue());
127
+ return null;
128
+ }
129
+ ` ,
159
130
errors : [
160
131
{
161
- type : T . ArrayExpression ,
132
+ type : T . CallExpression ,
162
133
messageId : "preferUseStateLazyInitialization" ,
163
134
} ,
164
135
] ,
165
- } ,
166
- {
167
- code : `import { useState } from "react"; useState({ a: ${ expression } })` ,
168
- errors : [
169
- {
170
- type : T . ObjectExpression ,
171
- messageId : "preferUseStateLazyInitialization" ,
136
+ settings : {
137
+ "react-x" : {
138
+ version : "19.0.0" ,
172
139
} ,
173
- ] ,
140
+ } ,
174
141
} ,
175
142
{
176
- code : tsx `useLocalStorageState(1 || ${ expression } )` ,
143
+ code : tsx `useLocalStorageState(1 || getValue() )` ,
177
144
errors : [
178
145
{
179
- type : T . LogicalExpression ,
146
+ type : T . CallExpression ,
180
147
messageId : "preferUseStateLazyInitialization" ,
181
148
} ,
182
149
] ,
@@ -188,7 +155,7 @@ ruleTester.run(RULE_NAME, rule, {
188
155
} ,
189
156
} ,
190
157
} ,
191
- ] ) ,
158
+ ] ,
192
159
valid : [
193
160
...allValid ,
194
161
"useState()" ,
@@ -260,7 +227,11 @@ ruleTester.run(RULE_NAME, rule, {
260
227
'const { useState } = require("react"); useState(1 < 2 ? 3 : 4)' ,
261
228
'const { useState } = require("react"); useState(1 == 2 ? 3 : 4)' ,
262
229
'const { useState } = require("react"); useState(1 === 2 ? 3 : 4)' ,
230
+ "const [id, setId] = useState(useId());" ,
263
231
"const [state, setState] = useState(use(promise));" ,
232
+ "const [serverData, setLikes] = useState(use(getLikes()));" ,
233
+ "const [data, setData] = useState(use(getData()) || []);" ,
234
+ "const [character, setCharacter] = useState(use(props.character) ?? undefined);" ,
264
235
{
265
236
code : tsx `
266
237
import { useState, use } from 'react';
@@ -278,55 +249,11 @@ ruleTester.run(RULE_NAME, rule, {
278
249
} ,
279
250
} ,
280
251
{
281
- code : tsx `
282
- import { useState, use } from 'react';
283
-
284
- const promise = Promise.resolve();
285
-
286
- function App() {
287
- const [state, setState] = useState(use(promise));
288
-
289
- return null;
290
- }
291
-
292
- export default App;
293
- ` ,
294
- settings : {
295
- "react-x" : {
296
- version : "19.0.0" ,
297
- } ,
298
- } ,
299
- } ,
300
- {
301
- code : "useLocalStorageState()" ,
252
+ code : "useLocalStorage(() => JSON.parse('{}'))" ,
302
253
settings : {
303
254
"react-x" : {
304
255
additionalHooks : {
305
- useState : [ "useLocalStorageState" ] ,
306
- } ,
307
- } ,
308
- } ,
309
- } ,
310
- {
311
- code : tsx `
312
- import { useState } from 'react';
313
-
314
- function getValue() {
315
- return 0;
316
- }
317
-
318
- function App() {
319
- const [count, setCount] = useState(() => getValue());
320
-
321
- return null;
322
- }
323
-
324
- export default App;
325
- ` ,
326
- settings : {
327
- "react-x" : {
328
- additionalHooks : {
329
- useState : [ "useLocalStorageState" ] ,
256
+ useState : [ "useLocalStorage" ] ,
330
257
} ,
331
258
} ,
332
259
} ,
0 commit comments