Skip to content

Commit

Permalink
correctly support ObjectPattern and ArrayPattern in function signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Aug 14, 2023
1 parent 76c35e8 commit c3c16e3
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-sheep-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-docgen': patch
---

Fixed error with object and array patterns in function signatures.
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,52 @@ exports[`getTSType > detects function signature type with \`this\` parameter 1`]
}
`;
exports[`getTSType > detects function signature type with object and array pattern 1`] = `
{
"name": "signature",
"raw": "({ x }: { x: number }, [ f,s ]: Array<string>) => boolean",
"signature": {
"arguments": [
{
"name": "",
"type": {
"name": "signature",
"raw": "{ x: number }",
"signature": {
"properties": [
{
"key": "x",
"value": {
"name": "number",
"required": true,
},
},
],
},
"type": "object",
},
},
{
"name": "",
"type": {
"elements": [
{
"name": "string",
},
],
"name": "Array",
"raw": "Array<string>",
},
},
],
"return": {
"name": "boolean",
},
},
"type": "function",
}
`;
exports[`getTSType > detects function type with subtype 1`] = `
{
"elements": [
Expand Down
8 changes: 8 additions & 0 deletions packages/react-docgen/src/utils/__tests__/getTSType-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,14 @@ describe('getTSType', () => {
expect(getTSType(typePath)).toMatchSnapshot();
});

test('detects function signature type with object and array pattern', () => {
const typePath = typeAlias(
'let x: ({ x }: { x: number }, [ f,s ]: Array<string>) => boolean;',
);

expect(getTSType(typePath)).toMatchSnapshot();
});

test('detects callable signature type', () => {
const typePath = typeAlias(
'let x: { (str: string): string, token: string };',
Expand Down
5 changes: 2 additions & 3 deletions packages/react-docgen/src/utils/getTSType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import type {
TSTypeOperator,
Identifier,
TSTypeParameterDeclaration,
RestElement,
TypeScript,
TSQualifiedName,
TSLiteralType,
Expand Down Expand Up @@ -357,8 +356,8 @@ function handleTSFunctionType(

return;
}
} else {
const restArgument = (param as NodePath<RestElement>).get('argument');
} else if (param.isRestElement()) {
const restArgument = param.get('argument');

if (restArgument.isIdentifier()) {
arg.name = restArgument.node.name;
Expand Down
2 changes: 1 addition & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
packages:
- 'benchmark/'
- 'benchmark'
- 'packages/*'

0 comments on commit c3c16e3

Please sign in to comment.