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( + '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 { + render() { + return
; + } +} + +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`] = ` [ {