diff --git a/code/core/src/csf-tools/ConfigFile.test.ts b/code/core/src/csf-tools/ConfigFile.test.ts index fe841c49f488..e3409d5eb681 100644 --- a/code/core/src/csf-tools/ConfigFile.test.ts +++ b/code/core/src/csf-tools/ConfigFile.test.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-underscore-dangle */ import { describe, expect, it } from 'vitest'; import { babelPrint } from '@storybook/core/babel'; @@ -1080,7 +1081,6 @@ describe('ConfigFile', () => { const config = loadConfig(source).parse(); config.setImport('path', 'path'); - // eslint-disable-next-line no-underscore-dangle const parsed = babelPrint(config._ast); expect(parsed).toMatchInlineSnapshot(` @@ -1099,7 +1099,6 @@ describe('ConfigFile', () => { const config = loadConfig(source).parse(); config.setImport('path', 'path'); - // eslint-disable-next-line no-underscore-dangle const parsed = babelPrint(config._ast); expect(parsed).toMatchInlineSnapshot(` @@ -1118,7 +1117,6 @@ describe('ConfigFile', () => { const config = loadConfig(source).parse(); config.setImport(['dirname'], 'path'); - // eslint-disable-next-line no-underscore-dangle const parsed = babelPrint(config._ast); expect(parsed).toMatchInlineSnapshot(` @@ -1139,7 +1137,6 @@ describe('ConfigFile', () => { const config = loadConfig(source).parse(); config.setImport(['dirname'], 'path'); - // eslint-disable-next-line no-underscore-dangle const parsed = babelPrint(config._ast); expect(parsed).toMatchInlineSnapshot(` @@ -1161,7 +1158,6 @@ describe('ConfigFile', () => { const config = loadConfig(source).parse(); config.setRequireImport('path', 'path'); - // eslint-disable-next-line no-underscore-dangle const parsed = babelPrint(config._ast); expect(parsed).toMatchInlineSnapshot(` @@ -1181,7 +1177,6 @@ describe('ConfigFile', () => { const config = loadConfig(source).parse(); config.setRequireImport('path', 'path'); - // eslint-disable-next-line no-underscore-dangle const parsed = babelPrint(config._ast); expect(parsed).toMatchInlineSnapshot(` @@ -1200,7 +1195,6 @@ describe('ConfigFile', () => { const config = loadConfig(source).parse(); config.setRequireImport(['dirname'], 'path'); - // eslint-disable-next-line no-underscore-dangle const parsed = babelPrint(config._ast); expect(parsed).toMatchInlineSnapshot(` @@ -1224,7 +1218,6 @@ describe('ConfigFile', () => { const config = loadConfig(source).parse(); config.setRequireImport(['dirname', 'basename'], 'path'); - // eslint-disable-next-line no-underscore-dangle const parsed = babelPrint(config._ast); expect(parsed).toMatchInlineSnapshot(` @@ -1308,4 +1301,31 @@ describe('ConfigFile', () => { ); }); }); + + describe('parse', () => { + it("export { X } with X is import { X } from 'another-file'", () => { + const source = dedent` + import type { StorybookConfig } from '@storybook/react-webpack5'; + import { path } from 'path'; + + export { path }; + + const config: StorybookConfig = { + addons: [ + 'foo', + { name: 'bar', options: {} }, + ], + "otherField": [ + "foo", + { "name": 'bar', options: {} }, + ], + } + export default config; + `; + const config = loadConfig(source).parse(); + + expect(config._exportDecls['path']).toBe(undefined); + expect(config._exports['path']).toBe(undefined); + }); + }); }); diff --git a/code/core/src/csf-tools/ConfigFile.ts b/code/core/src/csf-tools/ConfigFile.ts index 5cb0d28234eb..dc9f973d5ad6 100644 --- a/code/core/src/csf-tools/ConfigFile.ts +++ b/code/core/src/csf-tools/ConfigFile.ts @@ -102,6 +102,7 @@ const _findVarDeclarator = ( ): t.VariableDeclarator | null | undefined => { let declarator: t.VariableDeclarator | null | undefined = null; let declarations: t.VariableDeclarator[] | null = null; + program.body.find((node: t.Node) => { if (t.isVariableDeclaration(node)) { declarations = node.declarations; @@ -248,9 +249,13 @@ export class ConfigFile { ) { const { name: localName } = spec.local; const { name: exportName } = spec.exported; + const decl = _findVarDeclarator(localName, parent as t.Program) as any; - self._exports[exportName] = decl.init; - self._exportDecls[exportName] = decl; + // decl can be empty in case X from `import { X } from ....` because it is not handled in _findVarDeclarator + if (decl) { + self._exports[exportName] = decl.init; + self._exportDecls[exportName] = decl; + } } }); } else {