Skip to content

Commit

Permalink
Merge pull request #27418 from storybookjs/valentin/fix-aliased-named…
Browse files Browse the repository at this point in the history
…-export-analysis

CSF-Tools: Fix export specifier bug
  • Loading branch information
valentinpalkovic authored May 29, 2024
2 parents cf0af1a + dcf4a25 commit 3ad2f5f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
13 changes: 12 additions & 1 deletion code/lib/csf-tools/src/ConfigFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ describe('ConfigFile', () => {
)
).toEqual([{ directory: '../src', titlePrefix: 'Demo' }]);
});
it('export specfier', () => {
it('export specifier', () => {
expect(
getField(
['foo'],
Expand All @@ -227,6 +227,17 @@ describe('ConfigFile', () => {
)
).toEqual('bar');
});
it('export aliased specifier', () => {
expect(
getField(
['fooAlias'],
dedent`
const foo = 'bar';
export { foo as fooAlias };
`
)
).toEqual('bar');
});
});
});

Expand Down
9 changes: 7 additions & 2 deletions code/lib/csf-tools/src/ConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,14 @@ export class ConfigFile {
} else if (node.specifiers) {
// export { X };
node.specifiers.forEach((spec) => {
if (t.isExportSpecifier(spec) && t.isIdentifier(spec.exported)) {
if (
t.isExportSpecifier(spec) &&
t.isIdentifier(spec.local) &&
t.isIdentifier(spec.exported)
) {
const { name: localName } = spec.local;
const { name: exportName } = spec.exported;
const decl = _findVarDeclarator(exportName, parent as t.Program) as any;
const decl = _findVarDeclarator(localName, parent as t.Program) as any;
self._exports[exportName] = decl.init;
self._exportDecls[exportName] = decl;
}
Expand Down

0 comments on commit 3ad2f5f

Please sign in to comment.