Skip to content

Commit

Permalink
Merge pull request #18588 from Kingdutch/bugfix/support-as-default-cs…
Browse files Browse the repository at this point in the history
…f-export

Index: Support `{ csfData as default }` CSF exports
  • Loading branch information
shilman authored Jul 11, 2022
2 parents e7ad108 + 5e98e9a commit 68eb4fe
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
24 changes: 24 additions & 0 deletions lib/csf-tools/src/CsfFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,30 @@ describe('CsfFile', () => {
__id: foo-bar--a
`);
});

it('as default export', () => {
expect(
parse(
dedent`
const meta = { title: 'foo/bar' };
export const A = () => {};
export {
meta as default,
A
};
`,
true
)
).toMatchInlineSnapshot(`
meta:
title: foo/bar
stories:
- id: foo-bar--a
name: A
parameters:
__id: foo-bar--a
`);
});
});

describe('error handling', () => {
Expand Down
26 changes: 24 additions & 2 deletions lib/csf-tools/src/CsfFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,30 @@ export class CsfFile {
node.specifiers.forEach((specifier) => {
if (t.isExportSpecifier(specifier) && t.isIdentifier(specifier.exported)) {
const { name: exportName } = specifier.exported;
self._storyAnnotations[exportName] = {};
self._stories[exportName] = { id: 'FIXME', name: exportName, parameters: {} };
if (exportName === 'default') {
let metaNode: t.ObjectExpression;
const decl = t.isProgram(parent)
? findVarInitialization(specifier.local.name, parent)
: specifier.local;

if (t.isObjectExpression(decl)) {
// export default { ... };
metaNode = decl;
} else if (
// export default { ... } as Meta<...>
t.isTSAsExpression(decl) &&
t.isObjectExpression(decl.expression)
) {
metaNode = decl.expression;
}

if (!self._meta && metaNode && t.isProgram(parent)) {
self._parseMeta(metaNode, parent);
}
} else {
self._storyAnnotations[exportName] = {};
self._stories[exportName] = { id: 'FIXME', name: exportName, parameters: {} };
}
}
});
}
Expand Down

0 comments on commit 68eb4fe

Please sign in to comment.