Skip to content

Commit

Permalink
Allow { csfData as default } exports
Browse files Browse the repository at this point in the history
This ensures that if CSF data is exported with the `{ data as default }`
syntax rather than as `export default data` it is still recognized by
Storybook.

This improves interop with automated code-generation performed by the
ReScript compiler.
  • Loading branch information
Kingdutch committed Jun 28, 2022
1 parent ad3e6f0 commit f9ded0a
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 f9ded0a

Please sign in to comment.