Skip to content
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

ConfigFile: Fix export { X } parsing #29344

Merged
merged 10 commits into from
Oct 20, 2024
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
36 changes: 28 additions & 8 deletions code/core/src/csf-tools/ConfigFile.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-underscore-dangle */
import { describe, expect, it } from 'vitest';

import { babelPrint } from '@storybook/core/babel';
Expand Down Expand Up @@ -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(`
Expand All @@ -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(`
Expand All @@ -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(`
Expand All @@ -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(`
Expand All @@ -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(`
Expand All @@ -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(`
Expand All @@ -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(`
Expand All @@ -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(`
Expand Down Expand Up @@ -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);
});
});
});
9 changes: 7 additions & 2 deletions code/core/src/csf-tools/ConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down