@@ -25,28 +25,30 @@ describe('Components', () => {
25
25
const done = orDone || instructionsOrDone ;
26
26
const instructions = orDone ? instructionsOrDone : instructionsOrDone ;
27
27
28
- const rule = Components . detect ( ( _context , components , util ) => {
29
- const instructionResults = [ ] ;
28
+ const rule = {
29
+ create : Components . detect ( ( _context , components , util ) => {
30
+ const instructionResults = [ ] ;
30
31
31
- const augmentedInstructions = fromEntries (
32
- entries ( instructions || { } ) . map ( ( nodeTypeAndHandler ) => {
33
- const nodeType = nodeTypeAndHandler [ 0 ] ;
34
- const handler = nodeTypeAndHandler [ 1 ] ;
35
- return [ nodeType , ( node ) => {
36
- instructionResults . push ( { type : nodeType , result : handler ( node , context , components , util ) } ) ;
37
- } ] ;
38
- } )
39
- ) ;
32
+ const augmentedInstructions = fromEntries (
33
+ entries ( instructions || { } ) . map ( ( nodeTypeAndHandler ) => {
34
+ const nodeType = nodeTypeAndHandler [ 0 ] ;
35
+ const handler = nodeTypeAndHandler [ 1 ] ;
36
+ return [ nodeType , ( node ) => {
37
+ instructionResults . push ( { type : nodeType , result : handler ( node , context , components , util ) } ) ;
38
+ } ] ;
39
+ } )
40
+ ) ;
40
41
41
- return Object . assign ( { } , augmentedInstructions , {
42
- 'Program:exit' ( node ) {
43
- if ( augmentedInstructions [ 'Program:exit' ] ) {
44
- augmentedInstructions [ 'Program:exit' ] ( node , context , components , util ) ;
45
- }
46
- done ( components , instructionResults ) ;
47
- } ,
48
- } ) ;
49
- } ) ;
42
+ return Object . assign ( { } , augmentedInstructions , {
43
+ 'Program:exit' ( node ) {
44
+ if ( augmentedInstructions [ 'Program:exit' ] ) {
45
+ augmentedInstructions [ 'Program:exit' ] ( node , context , components , util ) ;
46
+ }
47
+ done ( components , instructionResults ) ;
48
+ } ,
49
+ } ) ;
50
+ } ) ,
51
+ } ;
50
52
51
53
const tests = {
52
54
valid : parsers . all ( [ Object . assign ( { } , test , {
@@ -122,10 +124,12 @@ describe('Components', () => {
122
124
describe ( 'isReactHookCall' , ( ) => {
123
125
it ( 'should not identify hook-like call' , ( ) => {
124
126
testComponentsDetect ( {
125
- code : `import { useRef } from 'react'
126
- function useColor() {
127
- return useState()
128
- }` ,
127
+ code : `
128
+ import { useRef } from 'react'
129
+ function useColor() {
130
+ return useState()
131
+ }
132
+ ` ,
129
133
} , {
130
134
CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node ) ,
131
135
} , ( _components , instructionResults ) => {
@@ -135,10 +139,12 @@ describe('Components', () => {
135
139
136
140
it ( 'should identify hook call' , ( ) => {
137
141
testComponentsDetect ( {
138
- code : `import { useState } from 'react'
139
- function useColor() {
140
- return useState()
141
- }` ,
142
+ code : `
143
+ import { useState } from 'react'
144
+ function useColor() {
145
+ return useState()
146
+ }
147
+ ` ,
142
148
} , {
143
149
CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node ) ,
144
150
} , ( _components , instructionResults ) => {
@@ -148,10 +154,12 @@ describe('Components', () => {
148
154
149
155
it ( 'should identify aliased hook call' , ( ) => {
150
156
testComponentsDetect ( {
151
- code : `import { useState as useStateAlternative } from 'react'
152
- function useColor() {
153
- return useStateAlternative()
154
- }` ,
157
+ code : `
158
+ import { useState as useStateAlternative } from 'react'
159
+ function useColor() {
160
+ return useStateAlternative()
161
+ }
162
+ ` ,
155
163
} , {
156
164
CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node ) ,
157
165
} , ( _components , instructionResults ) => {
@@ -161,10 +169,12 @@ describe('Components', () => {
161
169
162
170
it ( 'should identify aliased present named hook call' , ( ) => {
163
171
testComponentsDetect ( {
164
- code : `import { useState as useStateAlternative } from 'react'
165
- function useColor() {
166
- return useStateAlternative()
167
- }` ,
172
+ code : `
173
+ import { useState as useStateAlternative } from 'react'
174
+ function useColor() {
175
+ return useStateAlternative()
176
+ }
177
+ ` ,
168
178
} , {
169
179
CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node , [ 'useState' ] ) ,
170
180
} , ( _components , instructionResults ) => {
@@ -174,13 +184,15 @@ describe('Components', () => {
174
184
175
185
it ( 'should not identify shadowed hook call' , ( ) => {
176
186
testComponentsDetect ( {
177
- code : `import { useState } from 'react'
178
- function useColor() {
179
- function useState() {
180
- return null
187
+ code : `
188
+ import { useState } from 'react'
189
+ function useColor() {
190
+ function useState() {
191
+ return null
192
+ }
193
+ return useState()
181
194
}
182
- return useState()
183
- }` ,
195
+ ` ,
184
196
} , {
185
197
CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node ) ,
186
198
} , ( _components , instructionResults ) => {
@@ -190,13 +202,15 @@ describe('Components', () => {
190
202
191
203
it ( 'should not identify shadowed aliased present named hook call' , ( ) => {
192
204
testComponentsDetect ( {
193
- code : `import { useState as useStateAlternative } from 'react'
194
- function useColor() {
195
- function useStateAlternative() {
196
- return null
205
+ code : `
206
+ import { useState as useStateAlternative } from 'react'
207
+ function useColor() {
208
+ function useStateAlternative() {
209
+ return null
210
+ }
211
+ return useStateAlternative()
197
212
}
198
- return useStateAlternative()
199
- }` ,
213
+ ` ,
200
214
} , {
201
215
CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node , [ 'useState' ] ) ,
202
216
} , ( _components , instructionResults ) => {
@@ -206,10 +220,12 @@ describe('Components', () => {
206
220
207
221
it ( 'should identify React hook call' , ( ) => {
208
222
testComponentsDetect ( {
209
- code : `import React from 'react'
210
- function useColor() {
211
- return React.useState()
212
- }` ,
223
+ code : `
224
+ import React from 'react'
225
+ function useColor() {
226
+ return React.useState()
227
+ }
228
+ ` ,
213
229
} , {
214
230
CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node ) ,
215
231
} , ( _components , instructionResults ) => {
@@ -219,10 +235,12 @@ describe('Components', () => {
219
235
220
236
it ( 'should identify aliased React hook call' , ( ) => {
221
237
testComponentsDetect ( {
222
- code : `import ReactAlternative from 'react'
223
- function useColor() {
224
- return ReactAlternative.useState()
225
- }` ,
238
+ code : `
239
+ import ReactAlternative from 'react'
240
+ function useColor() {
241
+ return ReactAlternative.useState()
242
+ }
243
+ ` ,
226
244
} , {
227
245
CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node ) ,
228
246
} , ( _components , instructionResults ) => {
@@ -232,13 +250,15 @@ describe('Components', () => {
232
250
233
251
it ( 'should not identify shadowed React hook call' , ( ) => {
234
252
testComponentsDetect ( {
235
- code : `import React from 'react'
236
- function useColor() {
237
- const React = {
238
- useState: () => null
253
+ code : `
254
+ import React from 'react'
255
+ function useColor() {
256
+ const React = {
257
+ useState: () => null
258
+ }
259
+ return React.useState()
239
260
}
240
- return React.useState()
241
- }` ,
261
+ ` ,
242
262
} , {
243
263
CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node ) ,
244
264
} , ( _components , instructionResults ) => {
@@ -248,10 +268,12 @@ describe('Components', () => {
248
268
249
269
it ( 'should identify present named hook call' , ( ) => {
250
270
testComponentsDetect ( {
251
- code : `import { useState } from 'react'
252
- function useColor() {
253
- return useState()
254
- }` ,
271
+ code : `
272
+ import { useState } from 'react'
273
+ function useColor() {
274
+ return useState()
275
+ }
276
+ ` ,
255
277
} , {
256
278
CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node , [ 'useState' ] ) ,
257
279
} , ( _components , instructionResults ) => {
@@ -261,10 +283,12 @@ describe('Components', () => {
261
283
262
284
it ( 'should identify present named React hook call' , ( ) => {
263
285
testComponentsDetect ( {
264
- code : `import React from 'react'
265
- function useColor() {
266
- return React.useState()
267
- }` ,
286
+ code : `
287
+ import React from 'react'
288
+ function useColor() {
289
+ return React.useState()
290
+ }
291
+ ` ,
268
292
} , {
269
293
CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node , [ 'useState' ] ) ,
270
294
} , ( _components , instructionResults ) => {
@@ -274,10 +298,12 @@ describe('Components', () => {
274
298
275
299
it ( 'should not identify missing named hook call' , ( ) => {
276
300
testComponentsDetect ( {
277
- code : `import { useState } from 'react'
278
- function useColor() {
279
- return useState()
280
- }` ,
301
+ code : `
302
+ import { useState } from 'react'
303
+ function useColor() {
304
+ return useState()
305
+ }
306
+ ` ,
281
307
} , {
282
308
CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node , [ 'useRef' ] ) ,
283
309
} , ( _components , instructionResults ) => {
0 commit comments