@@ -25,28 +25,30 @@ describe('Components', () => {
2525 const done = orDone || instructionsOrDone ;
2626 const instructions = orDone ? instructionsOrDone : instructionsOrDone ;
2727
28- const rule = Components . detect ( ( _context , components , util ) => {
29- const instructionResults = [ ] ;
28+ const rule = {
29+ create : Components . detect ( ( _context , components , util ) => {
30+ const instructionResults = [ ] ;
3031
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+ ) ;
4041
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+ } ;
5052
5153 const tests = {
5254 valid : parsers . all ( [ Object . assign ( { } , test , {
@@ -122,10 +124,12 @@ describe('Components', () => {
122124 describe ( 'isReactHookCall' , ( ) => {
123125 it ( 'should not identify hook-like call' , ( ) => {
124126 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+ ` ,
129133 } , {
130134 CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node ) ,
131135 } , ( _components , instructionResults ) => {
@@ -135,10 +139,12 @@ describe('Components', () => {
135139
136140 it ( 'should identify hook call' , ( ) => {
137141 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+ ` ,
142148 } , {
143149 CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node ) ,
144150 } , ( _components , instructionResults ) => {
@@ -148,10 +154,12 @@ describe('Components', () => {
148154
149155 it ( 'should identify aliased hook call' , ( ) => {
150156 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+ ` ,
155163 } , {
156164 CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node ) ,
157165 } , ( _components , instructionResults ) => {
@@ -161,10 +169,12 @@ describe('Components', () => {
161169
162170 it ( 'should identify aliased present named hook call' , ( ) => {
163171 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+ ` ,
168178 } , {
169179 CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node , [ 'useState' ] ) ,
170180 } , ( _components , instructionResults ) => {
@@ -174,13 +184,15 @@ describe('Components', () => {
174184
175185 it ( 'should not identify shadowed hook call' , ( ) => {
176186 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()
181194 }
182- return useState()
183- }` ,
195+ ` ,
184196 } , {
185197 CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node ) ,
186198 } , ( _components , instructionResults ) => {
@@ -190,13 +202,15 @@ describe('Components', () => {
190202
191203 it ( 'should not identify shadowed aliased present named hook call' , ( ) => {
192204 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()
197212 }
198- return useStateAlternative()
199- }` ,
213+ ` ,
200214 } , {
201215 CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node , [ 'useState' ] ) ,
202216 } , ( _components , instructionResults ) => {
@@ -206,10 +220,12 @@ describe('Components', () => {
206220
207221 it ( 'should identify React hook call' , ( ) => {
208222 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+ ` ,
213229 } , {
214230 CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node ) ,
215231 } , ( _components , instructionResults ) => {
@@ -219,10 +235,12 @@ describe('Components', () => {
219235
220236 it ( 'should identify aliased React hook call' , ( ) => {
221237 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+ ` ,
226244 } , {
227245 CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node ) ,
228246 } , ( _components , instructionResults ) => {
@@ -232,13 +250,15 @@ describe('Components', () => {
232250
233251 it ( 'should not identify shadowed React hook call' , ( ) => {
234252 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()
239260 }
240- return React.useState()
241- }` ,
261+ ` ,
242262 } , {
243263 CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node ) ,
244264 } , ( _components , instructionResults ) => {
@@ -248,10 +268,12 @@ describe('Components', () => {
248268
249269 it ( 'should identify present named hook call' , ( ) => {
250270 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+ ` ,
255277 } , {
256278 CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node , [ 'useState' ] ) ,
257279 } , ( _components , instructionResults ) => {
@@ -261,10 +283,12 @@ describe('Components', () => {
261283
262284 it ( 'should identify present named React hook call' , ( ) => {
263285 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+ ` ,
268292 } , {
269293 CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node , [ 'useState' ] ) ,
270294 } , ( _components , instructionResults ) => {
@@ -274,10 +298,12 @@ describe('Components', () => {
274298
275299 it ( 'should not identify missing named hook call' , ( ) => {
276300 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+ ` ,
281307 } , {
282308 CallExpression : ( node , _context , _components , util ) => util . isReactHookCall ( node , [ 'useRef' ] ) ,
283309 } , ( _components , instructionResults ) => {
0 commit comments