From b1a21c34c314e17ff7cece49a3d0eab442812ba7 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder <231804+danez@users.noreply.github.com> Date: Fri, 12 Jan 2024 01:18:37 +0100 Subject: [PATCH] Fix handling of exports with namespace specifiers --- .changeset/itchy-planets-heal.md | 5 +++++ .../__tests__/resolveExportDeclaration-test.ts | 9 +++++++++ .../src/utils/resolveExportDeclaration.ts | 10 +++++----- .../__fixtures__/namespace-export.tsx | 13 +++++++++++++ .../__snapshots__/integration-test.ts.snap | 18 ++++++++++++++++++ 5 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 .changeset/itchy-planets-heal.md create mode 100644 packages/react-docgen/tests/integration/__fixtures__/namespace-export.tsx diff --git a/.changeset/itchy-planets-heal.md b/.changeset/itchy-planets-heal.md new file mode 100644 index 00000000000..2cbf18d3b0d --- /dev/null +++ b/.changeset/itchy-planets-heal.md @@ -0,0 +1,5 @@ +--- +'react-docgen': patch +--- + +Do not throw error when using namespace specifiers in export statements diff --git a/packages/react-docgen/src/utils/__tests__/resolveExportDeclaration-test.ts b/packages/react-docgen/src/utils/__tests__/resolveExportDeclaration-test.ts index 610ee98074b..4cb3a7bd212 100644 --- a/packages/react-docgen/src/utils/__tests__/resolveExportDeclaration-test.ts +++ b/packages/react-docgen/src/utils/__tests__/resolveExportDeclaration-test.ts @@ -73,4 +73,13 @@ describe('resolveExportDeclaration', () => { specifiers[2].get('local'), ]); }); + + test('resolves named exports from with namespace', () => { + const exp = parse.statement<ExportNamedDeclaration>( + 'export * as foo from "";', + ); + const resolved = resolveExportDeclaration(exp); + + expect(resolved).toEqual([]); + }); }); diff --git a/packages/react-docgen/src/utils/resolveExportDeclaration.ts b/packages/react-docgen/src/utils/resolveExportDeclaration.ts index 539c2814e87..5081ab23ef9 100644 --- a/packages/react-docgen/src/utils/resolveExportDeclaration.ts +++ b/packages/react-docgen/src/utils/resolveExportDeclaration.ts @@ -24,11 +24,11 @@ export default function resolveExportDeclaration( definitions.push(declaration); } } else if (path.has('specifiers')) { - path - .get('specifiers') - .forEach((specifier) => - definitions.push(specifier.get('local') as NodePath), - ); + path.get('specifiers').forEach((specifier) => { + if (specifier.isExportSpecifier()) { + definitions.push(specifier.get('local')); + } + }); } } diff --git a/packages/react-docgen/tests/integration/__fixtures__/namespace-export.tsx b/packages/react-docgen/tests/integration/__fixtures__/namespace-export.tsx new file mode 100644 index 00000000000..6757aeb2ac5 --- /dev/null +++ b/packages/react-docgen/tests/integration/__fixtures__/namespace-export.tsx @@ -0,0 +1,13 @@ +import * as React from 'react'; + +interface IProps { + value: string; +} + +export default class extends React.Component<IProps> { + render() { + return <div/>; + } +} + +export * as namespace from "./support/other-exports.js"; diff --git a/packages/react-docgen/tests/integration/__snapshots__/integration-test.ts.snap b/packages/react-docgen/tests/integration/__snapshots__/integration-test.ts.snap index 5b01f2f2e4c..8a3769dcb4a 100644 --- a/packages/react-docgen/tests/integration/__snapshots__/integration-test.ts.snap +++ b/packages/react-docgen/tests/integration/__snapshots__/integration-test.ts.snap @@ -2120,6 +2120,24 @@ exports[`integration > fixtures > processes component "flow-spread-import-type.j ] `; +exports[`integration > fixtures > processes component "namespace-export.tsx" without errors 1`] = ` +[ + { + "description": "", + "methods": [], + "props": { + "value": { + "description": "", + "required": true, + "tsType": { + "name": "string", + }, + }, + }, + }, +] +`; + exports[`integration > fixtures > processes component "test-all-imports.tsx" without errors 1`] = ` [ {