Skip to content

correctly support ObjectPattern and ArrayPattern in function signatures #830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/*'