From c3c16e30ad4cffa7304543ff4bef9b4f13a05683 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder <231804+danez@users.noreply.github.com> Date: Mon, 14 Aug 2023 14:27:33 +0200 Subject: [PATCH] correctly support ObjectPattern and ArrayPattern in function signatures --- .changeset/hungry-sheep-laugh.md | 5 ++ .../__snapshots__/getTSType-test.ts.snap | 46 +++++++++++++++++++ .../src/utils/__tests__/getTSType-test.ts | 8 ++++ packages/react-docgen/src/utils/getTSType.ts | 5 +- pnpm-workspace.yaml | 2 +- 5 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 .changeset/hungry-sheep-laugh.md diff --git a/.changeset/hungry-sheep-laugh.md b/.changeset/hungry-sheep-laugh.md new file mode 100644 index 00000000000..18bb99b869e --- /dev/null +++ b/.changeset/hungry-sheep-laugh.md @@ -0,0 +1,5 @@ +--- +'react-docgen': patch +--- + +Fixed error with object and array patterns in function signatures. diff --git a/packages/react-docgen/src/utils/__tests__/__snapshots__/getTSType-test.ts.snap b/packages/react-docgen/src/utils/__tests__/__snapshots__/getTSType-test.ts.snap index ad42323142a..22e588f60e3 100644 --- a/packages/react-docgen/src/utils/__tests__/__snapshots__/getTSType-test.ts.snap +++ b/packages/react-docgen/src/utils/__tests__/__snapshots__/getTSType-test.ts.snap @@ -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) => 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", + }, + }, + ], + "return": { + "name": "boolean", + }, + }, + "type": "function", +} +`; + exports[`getTSType > detects function type with subtype 1`] = ` { "elements": [ diff --git a/packages/react-docgen/src/utils/__tests__/getTSType-test.ts b/packages/react-docgen/src/utils/__tests__/getTSType-test.ts index e4c6b9d15a5..0413d2b8503 100644 --- a/packages/react-docgen/src/utils/__tests__/getTSType-test.ts +++ b/packages/react-docgen/src/utils/__tests__/getTSType-test.ts @@ -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) => boolean;', + ); + + expect(getTSType(typePath)).toMatchSnapshot(); + }); + test('detects callable signature type', () => { const typePath = typeAlias( 'let x: { (str: string): string, token: string };', diff --git a/packages/react-docgen/src/utils/getTSType.ts b/packages/react-docgen/src/utils/getTSType.ts index c60b4a30361..1c8c9285858 100644 --- a/packages/react-docgen/src/utils/getTSType.ts +++ b/packages/react-docgen/src/utils/getTSType.ts @@ -33,7 +33,6 @@ import type { TSTypeOperator, Identifier, TSTypeParameterDeclaration, - RestElement, TypeScript, TSQualifiedName, TSLiteralType, @@ -357,8 +356,8 @@ function handleTSFunctionType( return; } - } else { - const restArgument = (param as NodePath).get('argument'); + } else if (param.isRestElement()) { + const restArgument = param.get('argument'); if (restArgument.isIdentifier()) { arg.name = restArgument.node.name; diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index c16430c13fc..709b8d59c86 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -1,3 +1,3 @@ packages: - - 'benchmark/' + - 'benchmark' - 'packages/*'