@@ -5,15 +5,16 @@ import type DocumentationMock from '../../__mocks__/Documentation';
5
5
import type { NodePath } from '@babel/traverse' ;
6
6
import type {
7
7
ArrowFunctionExpression ,
8
+ CallExpression ,
9
+ ClassDeclaration ,
8
10
ClassExpression ,
9
11
ExportDefaultDeclaration ,
10
- ExportNamedDeclaration ,
11
- ExpressionStatement ,
12
+ FunctionDeclaration ,
12
13
FunctionExpression ,
13
- Node ,
14
14
ObjectExpression ,
15
15
VariableDeclaration ,
16
16
} from '@babel/types' ;
17
+ import type { ComponentNode } from '../../resolver' ;
17
18
18
19
jest . mock ( '../../Documentation' ) ;
19
20
@@ -26,7 +27,7 @@ describe('componentDocblockHandler', () => {
26
27
27
28
function test (
28
29
definitionSrc : string ,
29
- parseFunc : ( src : string ) => NodePath < Node | null | undefined > ,
30
+ parseFunc : ( src : string ) => NodePath < ComponentNode > ,
30
31
) {
31
32
it ( 'finds docblocks for component definitions' , ( ) => {
32
33
const definition = parseFunc ( `
@@ -36,7 +37,7 @@ describe('componentDocblockHandler', () => {
36
37
* Component description
37
38
*/
38
39
${ definitionSrc }
39
- ` ) as NodePath ;
40
+ ` ) ;
40
41
41
42
componentDocblockHandler ( documentation , definition ) ;
42
43
expect ( documentation . description ) . toBe ( 'Component description' ) ;
@@ -50,15 +51,15 @@ describe('componentDocblockHandler', () => {
50
51
* This is not a docblock',
51
52
*/
52
53
${ definitionSrc }
53
- ` ) as NodePath ;
54
+ ` ) ;
54
55
55
56
componentDocblockHandler ( documentation , definition ) ;
56
57
expect ( documentation . description ) . toBe ( '' ) ;
57
58
58
59
definition = parseFunc ( `
59
60
// Inline comment'
60
61
${ definitionSrc }
61
- ` ) as NodePath ;
62
+ ` ) ;
62
63
63
64
componentDocblockHandler ( documentation , definition ) ;
64
65
expect ( documentation . description ) . toBe ( '' ) ;
@@ -73,7 +74,7 @@ describe('componentDocblockHandler', () => {
73
74
*/
74
75
var something_else = "foo";
75
76
${ definitionSrc }
76
- ` ) as NodePath ;
77
+ ` ) ;
77
78
78
79
componentDocblockHandler ( documentation , definition ) ;
79
80
expect ( documentation . description ) . toBe ( '' ) ;
@@ -86,7 +87,7 @@ describe('componentDocblockHandler', () => {
86
87
*/
87
88
function testDecorators (
88
89
classSrc : string ,
89
- parseFunc : ( src : string ) => NodePath < Node | null | undefined > ,
90
+ parseFunc : ( src : string ) => NodePath < ComponentNode > ,
90
91
exportSrc = '' ,
91
92
) {
92
93
describe ( 'decorators' , ( ) => {
@@ -100,7 +101,7 @@ describe('componentDocblockHandler', () => {
100
101
@Decorator1
101
102
@Decorator2
102
103
${ classSrc }
103
- ` ) as NodePath ;
104
+ ` ) ;
104
105
105
106
componentDocblockHandler ( documentation , definition ) ;
106
107
expect ( documentation . description ) . toBe ( 'Component description' ) ;
@@ -120,7 +121,7 @@ describe('componentDocblockHandler', () => {
120
121
* Component description
121
122
*/
122
123
${ classSrc }
123
- ` ) as NodePath ;
124
+ ` ) ;
124
125
125
126
componentDocblockHandler ( documentation , definition ) ;
126
127
expect ( documentation . description ) . toBe ( 'Component description' ) ;
@@ -158,12 +159,12 @@ describe('componentDocblockHandler', () => {
158
159
describe ( 'imports' , ( ) => {
159
160
it ( 'can use a custom importer to resolve docblocks on imported components' , ( ) => {
160
161
const program = parse
161
- . statementLast < ExportDefaultDeclaration > (
162
+ . statementLast (
162
163
`import ${ importDef } from 'test1';
163
164
export default ${ importName } ;` ,
164
165
mockImporter ,
165
166
)
166
- . get ( 'declaration' ) ;
167
+ . get ( 'declaration' ) as NodePath < ComponentNode > ;
167
168
168
169
componentDocblockHandler ( documentation , program ) ;
169
170
expect ( documentation . description ) . toBe ( 'Component description' ) ;
@@ -172,12 +173,12 @@ describe('componentDocblockHandler', () => {
172
173
173
174
it ( 'traverses multiple imports' , ( ) => {
174
175
const program = parse
175
- . statementLast < ExportDefaultDeclaration > (
176
+ . statementLast (
176
177
`import ${ importDef } from 'test2';
177
178
export default ${ importName } ;` ,
178
179
mockImporter ,
179
180
)
180
- . get ( 'declaration' ) ;
181
+ . get ( 'declaration' ) as NodePath < ComponentNode > ;
181
182
182
183
componentDocblockHandler ( documentation , program ) ;
183
184
expect ( documentation . description ) . toBe ( 'Component description' ) ;
@@ -221,32 +222,40 @@ describe('componentDocblockHandler', () => {
221
222
testImports ( 'export var Component = () => {}' , 'Component' ) ;
222
223
} ) ;
223
224
224
- describe ( 'ES6 default exports ' , ( ) => {
225
+ describe ( 'ESM default export ' , ( ) => {
225
226
describe ( 'Default React.createClass export' , ( ) => {
226
227
test ( 'export default React.createClass({});' , src =>
227
228
parse
228
- . statementLast < ExportDefaultDeclaration > ( src )
229
+ . statementLast ( src )
229
230
. get ( 'declaration.arguments.0' ) as NodePath < ObjectExpression > ) ;
230
231
} ) ;
231
232
232
233
describe ( 'Default class declaration export' , ( ) => {
233
234
test ( 'export default class Component {}' , src =>
234
- parse . statementLast < ExportDefaultDeclaration > ( src ) . get ( 'declaration' ) ) ;
235
+ parse
236
+ . statementLast ( src )
237
+ . get ( 'declaration' ) as NodePath < ClassDeclaration > ) ;
235
238
testDecorators (
236
239
'class Component {}' ,
237
240
src =>
238
- parse . statementLast < ExportDefaultDeclaration > ( src ) . get ( 'declaration' ) ,
241
+ parse
242
+ . statementLast ( src )
243
+ . get ( 'declaration' ) as NodePath < ClassDeclaration > ,
239
244
'export default' ,
240
245
) ;
241
246
} ) ;
242
247
243
248
describe ( 'Default class expression export' , ( ) => {
244
249
test ( 'export default class {}' , src =>
245
- parse . statementLast < ExportDefaultDeclaration > ( src ) . get ( 'declaration' ) ) ;
250
+ parse
251
+ . statementLast ( src )
252
+ . get ( 'declaration' ) as NodePath < ClassExpression > ) ;
246
253
testDecorators (
247
254
'class {}' ,
248
255
src =>
249
- parse . statementLast < ExportDefaultDeclaration > ( src ) . get ( 'declaration' ) ,
256
+ parse
257
+ . statementLast ( src )
258
+ . get ( 'declaration' ) as NodePath < ClassExpression > ,
250
259
'export default' ,
251
260
) ;
252
261
} ) ;
@@ -255,61 +264,71 @@ describe('componentDocblockHandler', () => {
255
264
describe ( 'named function' , ( ) => {
256
265
test ( 'export default function Component() {}' , src =>
257
266
parse
258
- . statementLast < ExportDefaultDeclaration > ( src )
259
- . get ( 'declaration' ) ) ;
267
+ . statementLast ( src )
268
+ . get ( 'declaration' ) as NodePath < FunctionDeclaration > ) ;
260
269
} ) ;
261
270
262
271
describe ( 'anonymous function' , ( ) => {
263
272
test ( 'export default function() {}' , src =>
264
273
parse
265
- . statementLast < ExportDefaultDeclaration > ( src )
266
- . get ( 'declaration' ) ) ;
274
+ . statementLast ( src )
275
+ . get ( 'declaration' ) as NodePath < FunctionDeclaration > ) ;
267
276
} ) ;
268
277
269
278
describe ( 'arrow function' , ( ) => {
270
279
test ( 'export default () => {}' , src =>
271
280
parse
272
281
. statementLast < ExportDefaultDeclaration > ( src )
273
- . get ( 'declaration' ) ) ;
282
+ . get ( 'declaration' ) as NodePath < ArrowFunctionExpression > ) ;
274
283
} ) ;
275
284
} ) ;
276
285
} ) ;
277
286
278
- describe ( 'ES6 named exports ' , ( ) => {
287
+ describe ( 'ESM named export ' , ( ) => {
279
288
describe ( 'Named React.createClass export' , ( ) => {
280
289
test ( 'export var Component = React.createClass({});' , src =>
281
290
parse
282
- . statementLast < ExportNamedDeclaration > ( src )
291
+ . statementLast ( src )
283
292
. get (
284
293
'declaration.declarations.0.init.arguments.0' ,
285
294
) as NodePath < ObjectExpression > ) ;
286
295
} ) ;
287
296
288
297
describe ( 'Named class declaration export' , ( ) => {
289
298
test ( 'export class Component {}' , src =>
290
- parse . statementLast < ExportNamedDeclaration > ( src ) . get ( 'declaration' ) ) ;
299
+ parse
300
+ . statementLast ( src )
301
+ . get ( 'declaration' ) as NodePath < ClassDeclaration > ) ;
291
302
testDecorators (
292
303
'class Component {}' ,
293
304
src =>
294
- parse . statementLast < ExportNamedDeclaration > ( src ) . get ( 'declaration' ) ,
305
+ parse
306
+ . statementLast ( src )
307
+ . get ( 'declaration' ) as NodePath < ClassDeclaration > ,
295
308
'export' ,
296
309
) ;
297
310
} ) ;
298
311
299
312
describe ( 'Named stateless function' , ( ) => {
300
313
describe ( 'named function' , ( ) => {
301
314
test ( 'export function Component() {}' , src =>
302
- parse . statementLast < ExportNamedDeclaration > ( src ) . get ( 'declaration' ) ) ;
315
+ parse
316
+ . statementLast ( src )
317
+ . get ( 'declaration' ) as NodePath < FunctionDeclaration > ) ;
303
318
} ) ;
304
319
305
320
describe ( 'anonymous function' , ( ) => {
306
321
test ( 'export var Component = function() {}' , src =>
307
- parse . statementLast < ExportNamedDeclaration > ( src ) . get ( 'declaration' ) ) ;
322
+ parse
323
+ . statementLast ( src )
324
+ . get ( 'declaration' ) as NodePath < FunctionExpression > ) ;
308
325
} ) ;
309
326
310
327
describe ( 'arrow function' , ( ) => {
311
328
test ( 'export var Component = () => {}' , src =>
312
- parse . statementLast < ExportNamedDeclaration > ( src ) . get ( 'declaration' ) ) ;
329
+ parse
330
+ . statementLast ( src )
331
+ . get ( 'declaration' ) as NodePath < ArrowFunctionExpression > ) ;
313
332
} ) ;
314
333
} ) ;
315
334
} ) ;
@@ -321,7 +340,7 @@ describe('componentDocblockHandler', () => {
321
340
test ( `
322
341
React.forwardRef((props, ref) => {});
323
342
import React from "react";` , src =>
324
- parse . statement < ExpressionStatement > ( src , - 2 ) . get ( 'expression' ) ) ;
343
+ parse . statement ( src , - 2 ) . get ( 'expression' ) as NodePath < CallExpression > ) ;
325
344
326
345
testImports (
327
346
`import React from 'react';
@@ -336,7 +355,7 @@ describe('componentDocblockHandler', () => {
336
355
React.memo(React.forwardRef((props, ref) => {}));
337
356
import React from "react";
338
357
` , src =>
339
- parse . statement < ExpressionStatement > ( src , - 2 ) . get ( 'expression' ) ) ;
358
+ parse . statement ( src , - 2 ) . get ( 'expression' ) as NodePath < CallExpression > ) ;
340
359
341
360
testImports (
342
361
`
@@ -354,7 +373,7 @@ describe('componentDocblockHandler', () => {
354
373
React.forwardRef(Component);
355
374
import React from "react";
356
375
` , src =>
357
- parse . statement < ExpressionStatement > ( src , - 2 ) . get ( 'expression' ) ) ;
376
+ parse . statement ( src , - 2 ) . get ( 'expression' ) as NodePath < CallExpression > ) ;
358
377
359
378
testImports (
360
379
`
0 commit comments