Skip to content

Commit 98fa11f

Browse files
committed
chore: cleanup code
1 parent 6820afe commit 98fa11f

30 files changed

+714
-490
lines changed

src/__tests__/parse-test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import type { ObjectExpression } from '@babel/types';
12
import fs from 'fs';
23
import { directory as tempDirectory } from 'tempy';
34
import { parse as testParse, noopImporter } from '../../tests/utils';
45
import parse, { ERROR_MISSING_DEFINITION } from '../parse';
56

67
describe('parse', () => {
78
it('allows custom component definition resolvers', () => {
8-
const path = testParse.expression('{foo: "bar"}');
9+
const path = testParse.expression<ObjectExpression>('{foo: "bar"}');
910
const resolver = jest.fn(() => [path]);
1011
const handler = jest.fn();
1112
parse('//empty', resolver, [handler], noopImporter);

src/handlers/__tests__/componentDocblockHandler-test.ts

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import type DocumentationMock from '../../__mocks__/Documentation';
55
import type { NodePath } from '@babel/traverse';
66
import type {
77
ArrowFunctionExpression,
8+
CallExpression,
9+
ClassDeclaration,
810
ClassExpression,
911
ExportDefaultDeclaration,
10-
ExportNamedDeclaration,
11-
ExpressionStatement,
12+
FunctionDeclaration,
1213
FunctionExpression,
13-
Node,
1414
ObjectExpression,
1515
VariableDeclaration,
1616
} from '@babel/types';
17+
import type { ComponentNode } from '../../resolver';
1718

1819
jest.mock('../../Documentation');
1920

@@ -26,7 +27,7 @@ describe('componentDocblockHandler', () => {
2627

2728
function test(
2829
definitionSrc: string,
29-
parseFunc: (src: string) => NodePath<Node | null | undefined>,
30+
parseFunc: (src: string) => NodePath<ComponentNode>,
3031
) {
3132
it('finds docblocks for component definitions', () => {
3233
const definition = parseFunc(`
@@ -36,7 +37,7 @@ describe('componentDocblockHandler', () => {
3637
* Component description
3738
*/
3839
${definitionSrc}
39-
`) as NodePath;
40+
`);
4041

4142
componentDocblockHandler(documentation, definition);
4243
expect(documentation.description).toBe('Component description');
@@ -50,15 +51,15 @@ describe('componentDocblockHandler', () => {
5051
* This is not a docblock',
5152
*/
5253
${definitionSrc}
53-
`) as NodePath;
54+
`);
5455

5556
componentDocblockHandler(documentation, definition);
5657
expect(documentation.description).toBe('');
5758

5859
definition = parseFunc(`
5960
// Inline comment'
6061
${definitionSrc}
61-
`) as NodePath;
62+
`);
6263

6364
componentDocblockHandler(documentation, definition);
6465
expect(documentation.description).toBe('');
@@ -73,7 +74,7 @@ describe('componentDocblockHandler', () => {
7374
*/
7475
var something_else = "foo";
7576
${definitionSrc}
76-
`) as NodePath;
77+
`);
7778

7879
componentDocblockHandler(documentation, definition);
7980
expect(documentation.description).toBe('');
@@ -86,7 +87,7 @@ describe('componentDocblockHandler', () => {
8687
*/
8788
function testDecorators(
8889
classSrc: string,
89-
parseFunc: (src: string) => NodePath<Node | null | undefined>,
90+
parseFunc: (src: string) => NodePath<ComponentNode>,
9091
exportSrc = '',
9192
) {
9293
describe('decorators', () => {
@@ -100,7 +101,7 @@ describe('componentDocblockHandler', () => {
100101
@Decorator1
101102
@Decorator2
102103
${classSrc}
103-
`) as NodePath;
104+
`);
104105

105106
componentDocblockHandler(documentation, definition);
106107
expect(documentation.description).toBe('Component description');
@@ -120,7 +121,7 @@ describe('componentDocblockHandler', () => {
120121
* Component description
121122
*/
122123
${classSrc}
123-
`) as NodePath;
124+
`);
124125

125126
componentDocblockHandler(documentation, definition);
126127
expect(documentation.description).toBe('Component description');
@@ -158,12 +159,12 @@ describe('componentDocblockHandler', () => {
158159
describe('imports', () => {
159160
it('can use a custom importer to resolve docblocks on imported components', () => {
160161
const program = parse
161-
.statementLast<ExportDefaultDeclaration>(
162+
.statementLast(
162163
`import ${importDef} from 'test1';
163164
export default ${importName};`,
164165
mockImporter,
165166
)
166-
.get('declaration');
167+
.get('declaration') as NodePath<ComponentNode>;
167168

168169
componentDocblockHandler(documentation, program);
169170
expect(documentation.description).toBe('Component description');
@@ -172,12 +173,12 @@ describe('componentDocblockHandler', () => {
172173

173174
it('traverses multiple imports', () => {
174175
const program = parse
175-
.statementLast<ExportDefaultDeclaration>(
176+
.statementLast(
176177
`import ${importDef} from 'test2';
177178
export default ${importName};`,
178179
mockImporter,
179180
)
180-
.get('declaration');
181+
.get('declaration') as NodePath<ComponentNode>;
181182

182183
componentDocblockHandler(documentation, program);
183184
expect(documentation.description).toBe('Component description');
@@ -221,32 +222,40 @@ describe('componentDocblockHandler', () => {
221222
testImports('export var Component = () => {}', 'Component');
222223
});
223224

224-
describe('ES6 default exports', () => {
225+
describe('ESM default export', () => {
225226
describe('Default React.createClass export', () => {
226227
test('export default React.createClass({});', src =>
227228
parse
228-
.statementLast<ExportDefaultDeclaration>(src)
229+
.statementLast(src)
229230
.get('declaration.arguments.0') as NodePath<ObjectExpression>);
230231
});
231232

232233
describe('Default class declaration export', () => {
233234
test('export default class Component {}', src =>
234-
parse.statementLast<ExportDefaultDeclaration>(src).get('declaration'));
235+
parse
236+
.statementLast(src)
237+
.get('declaration') as NodePath<ClassDeclaration>);
235238
testDecorators(
236239
'class Component {}',
237240
src =>
238-
parse.statementLast<ExportDefaultDeclaration>(src).get('declaration'),
241+
parse
242+
.statementLast(src)
243+
.get('declaration') as NodePath<ClassDeclaration>,
239244
'export default',
240245
);
241246
});
242247

243248
describe('Default class expression export', () => {
244249
test('export default class {}', src =>
245-
parse.statementLast<ExportDefaultDeclaration>(src).get('declaration'));
250+
parse
251+
.statementLast(src)
252+
.get('declaration') as NodePath<ClassExpression>);
246253
testDecorators(
247254
'class {}',
248255
src =>
249-
parse.statementLast<ExportDefaultDeclaration>(src).get('declaration'),
256+
parse
257+
.statementLast(src)
258+
.get('declaration') as NodePath<ClassExpression>,
250259
'export default',
251260
);
252261
});
@@ -255,61 +264,71 @@ describe('componentDocblockHandler', () => {
255264
describe('named function', () => {
256265
test('export default function Component() {}', src =>
257266
parse
258-
.statementLast<ExportDefaultDeclaration>(src)
259-
.get('declaration'));
267+
.statementLast(src)
268+
.get('declaration') as NodePath<FunctionDeclaration>);
260269
});
261270

262271
describe('anonymous function', () => {
263272
test('export default function() {}', src =>
264273
parse
265-
.statementLast<ExportDefaultDeclaration>(src)
266-
.get('declaration'));
274+
.statementLast(src)
275+
.get('declaration') as NodePath<FunctionDeclaration>);
267276
});
268277

269278
describe('arrow function', () => {
270279
test('export default () => {}', src =>
271280
parse
272281
.statementLast<ExportDefaultDeclaration>(src)
273-
.get('declaration'));
282+
.get('declaration') as NodePath<ArrowFunctionExpression>);
274283
});
275284
});
276285
});
277286

278-
describe('ES6 named exports', () => {
287+
describe('ESM named export', () => {
279288
describe('Named React.createClass export', () => {
280289
test('export var Component = React.createClass({});', src =>
281290
parse
282-
.statementLast<ExportNamedDeclaration>(src)
291+
.statementLast(src)
283292
.get(
284293
'declaration.declarations.0.init.arguments.0',
285294
) as NodePath<ObjectExpression>);
286295
});
287296

288297
describe('Named class declaration export', () => {
289298
test('export class Component {}', src =>
290-
parse.statementLast<ExportNamedDeclaration>(src).get('declaration'));
299+
parse
300+
.statementLast(src)
301+
.get('declaration') as NodePath<ClassDeclaration>);
291302
testDecorators(
292303
'class Component {}',
293304
src =>
294-
parse.statementLast<ExportNamedDeclaration>(src).get('declaration'),
305+
parse
306+
.statementLast(src)
307+
.get('declaration') as NodePath<ClassDeclaration>,
295308
'export',
296309
);
297310
});
298311

299312
describe('Named stateless function', () => {
300313
describe('named function', () => {
301314
test('export function Component() {}', src =>
302-
parse.statementLast<ExportNamedDeclaration>(src).get('declaration'));
315+
parse
316+
.statementLast(src)
317+
.get('declaration') as NodePath<FunctionDeclaration>);
303318
});
304319

305320
describe('anonymous function', () => {
306321
test('export var Component = function() {}', src =>
307-
parse.statementLast<ExportNamedDeclaration>(src).get('declaration'));
322+
parse
323+
.statementLast(src)
324+
.get('declaration') as NodePath<FunctionExpression>);
308325
});
309326

310327
describe('arrow function', () => {
311328
test('export var Component = () => {}', src =>
312-
parse.statementLast<ExportNamedDeclaration>(src).get('declaration'));
329+
parse
330+
.statementLast(src)
331+
.get('declaration') as NodePath<ArrowFunctionExpression>);
313332
});
314333
});
315334
});
@@ -321,7 +340,7 @@ describe('componentDocblockHandler', () => {
321340
test(`
322341
React.forwardRef((props, ref) => {});
323342
import React from "react";`, src =>
324-
parse.statement<ExpressionStatement>(src, -2).get('expression'));
343+
parse.statement(src, -2).get('expression') as NodePath<CallExpression>);
325344

326345
testImports(
327346
`import React from 'react';
@@ -336,7 +355,7 @@ describe('componentDocblockHandler', () => {
336355
React.memo(React.forwardRef((props, ref) => {}));
337356
import React from "react";
338357
`, src =>
339-
parse.statement<ExpressionStatement>(src, -2).get('expression'));
358+
parse.statement(src, -2).get('expression') as NodePath<CallExpression>);
340359

341360
testImports(
342361
`
@@ -354,7 +373,7 @@ describe('componentDocblockHandler', () => {
354373
React.forwardRef(Component);
355374
import React from "react";
356375
`, src =>
357-
parse.statement<ExpressionStatement>(src, -2).get('expression'));
376+
parse.statement(src, -2).get('expression') as NodePath<CallExpression>);
358377

359378
testImports(
360379
`

src/handlers/__tests__/componentMethodsJsDocHandler-test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import type { NodePath } from '@babel/traverse';
12
import Documentation from '../../Documentation';
3+
import type { ComponentNode } from '../../resolver';
24
import type DocumentationMock from '../../__mocks__/Documentation';
35
import componentMethodsJsDocHandler from '../componentMethodsJsDocHandler';
46

@@ -25,7 +27,7 @@ describe('componentMethodsJsDocHandler', () => {
2527
},
2628
];
2729
documentation.set('methods', methods);
28-
componentMethodsJsDocHandler(documentation);
30+
componentMethodsJsDocHandler(documentation, {} as NodePath<ComponentNode>);
2931
expect(documentation.get('methods')).toEqual(methods);
3032
});
3133

@@ -47,7 +49,7 @@ describe('componentMethodsJsDocHandler', () => {
4749
],
4850
},
4951
]);
50-
componentMethodsJsDocHandler(documentation);
52+
componentMethodsJsDocHandler(documentation, {} as NodePath<ComponentNode>);
5153
expect(documentation.get('methods')).toMatchSnapshot();
5254
});
5355

@@ -71,7 +73,7 @@ describe('componentMethodsJsDocHandler', () => {
7173
],
7274
},
7375
]);
74-
componentMethodsJsDocHandler(documentation);
76+
componentMethodsJsDocHandler(documentation, {} as NodePath<ComponentNode>);
7577
expect(documentation.get('methods')).toMatchSnapshot();
7678
});
7779

@@ -94,7 +96,7 @@ describe('componentMethodsJsDocHandler', () => {
9496
],
9597
},
9698
]);
97-
componentMethodsJsDocHandler(documentation);
99+
componentMethodsJsDocHandler(documentation, {} as NodePath<ComponentNode>);
98100
expect(documentation.get('methods')).toMatchSnapshot();
99101
});
100102
});

0 commit comments

Comments
 (0)