Skip to content

Commit c3c16e3

Browse files
committed
correctly support ObjectPattern and ArrayPattern in function signatures
1 parent 76c35e8 commit c3c16e3

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed

.changeset/hungry-sheep-laugh.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-docgen': patch
3+
---
4+
5+
Fixed error with object and array patterns in function signatures.

packages/react-docgen/src/utils/__tests__/__snapshots__/getTSType-test.ts.snap

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,52 @@ exports[`getTSType > detects function signature type with \`this\` parameter 1`]
283283
}
284284
`;
285285
286+
exports[`getTSType > detects function signature type with object and array pattern 1`] = `
287+
{
288+
"name": "signature",
289+
"raw": "({ x }: { x: number }, [ f,s ]: Array<string>) => boolean",
290+
"signature": {
291+
"arguments": [
292+
{
293+
"name": "",
294+
"type": {
295+
"name": "signature",
296+
"raw": "{ x: number }",
297+
"signature": {
298+
"properties": [
299+
{
300+
"key": "x",
301+
"value": {
302+
"name": "number",
303+
"required": true,
304+
},
305+
},
306+
],
307+
},
308+
"type": "object",
309+
},
310+
},
311+
{
312+
"name": "",
313+
"type": {
314+
"elements": [
315+
{
316+
"name": "string",
317+
},
318+
],
319+
"name": "Array",
320+
"raw": "Array<string>",
321+
},
322+
},
323+
],
324+
"return": {
325+
"name": "boolean",
326+
},
327+
},
328+
"type": "function",
329+
}
330+
`;
331+
286332
exports[`getTSType > detects function type with subtype 1`] = `
287333
{
288334
"elements": [

packages/react-docgen/src/utils/__tests__/getTSType-test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,14 @@ describe('getTSType', () => {
296296
expect(getTSType(typePath)).toMatchSnapshot();
297297
});
298298

299+
test('detects function signature type with object and array pattern', () => {
300+
const typePath = typeAlias(
301+
'let x: ({ x }: { x: number }, [ f,s ]: Array<string>) => boolean;',
302+
);
303+
304+
expect(getTSType(typePath)).toMatchSnapshot();
305+
});
306+
299307
test('detects callable signature type', () => {
300308
const typePath = typeAlias(
301309
'let x: { (str: string): string, token: string };',

packages/react-docgen/src/utils/getTSType.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import type {
3333
TSTypeOperator,
3434
Identifier,
3535
TSTypeParameterDeclaration,
36-
RestElement,
3736
TypeScript,
3837
TSQualifiedName,
3938
TSLiteralType,
@@ -357,8 +356,8 @@ function handleTSFunctionType(
357356

358357
return;
359358
}
360-
} else {
361-
const restArgument = (param as NodePath<RestElement>).get('argument');
359+
} else if (param.isRestElement()) {
360+
const restArgument = param.get('argument');
362361

363362
if (restArgument.isIdentifier()) {
364363
arg.name = restArgument.node.name;

pnpm-workspace.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
packages:
2-
- 'benchmark/'
2+
- 'benchmark'
33
- 'packages/*'

0 commit comments

Comments
 (0)