Skip to content

Commit

Permalink
Merge pull request #29344 from vctqs1/add-step-to-check-decl
Browse files Browse the repository at this point in the history
ConfigFile: Fix `export { X }` parsing
  • Loading branch information
shilman authored Oct 20, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 9ea2132 + 993d250 commit 3b4318f
Showing 2 changed files with 35 additions and 10 deletions.
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';
@@ -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);
});
});
});
9 changes: 7 additions & 2 deletions code/core/src/csf-tools/ConfigFile.ts
Original file line number Diff line number Diff line change
@@ -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 {

0 comments on commit 3b4318f

Please sign in to comment.