Skip to content

Commit

Permalink
Merge pull request #22190 from storybookjs/shilman/21703-csf-improve-…
Browse files Browse the repository at this point in the history
…error

CSF: Improve error message for bad default export
  • Loading branch information
shilman committed Apr 24, 2023
1 parent 9540dda commit 33101b2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
13 changes: 13 additions & 0 deletions code/lib/csf-tools/src/CsfFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,19 @@ describe('CsfFile', () => {
).toThrow('CSF: missing default export');
});

it('bad meta', () => {
expect(() =>
parse(
dedent`
const foo = bar();
export default foo;
export const A = () => {};
export const B = () => {};
`
)
).toThrow('CSF: default export must be an object');
});

it('no metadata', () => {
expect(() =>
parse(
Expand Down
14 changes: 11 additions & 3 deletions code/lib/csf-tools/src/CsfFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ export interface CsfOptions {
}

export class NoMetaError extends Error {
constructor(ast: t.Node, fileName?: string) {
constructor(message: string, ast: t.Node, fileName?: string) {
super(dedent`
CSF: missing default export ${formatLocation(ast, fileName)}
CSF: ${message} ${formatLocation(ast, fileName)}
More info: https://storybook.js.org/docs/react/writing-stories/introduction#default-export
`);
Expand Down Expand Up @@ -272,6 +272,14 @@ export class CsfFile {
self._metaNode = metaNode;
self._parseMeta(metaNode, parent);
}

if (self._metaStatement && !self._metaNode) {
throw new NoMetaError(
'default export must be an object',
self._metaStatement,
self._fileName
);
}
},
},
ExportNamedDeclaration: {
Expand Down Expand Up @@ -435,7 +443,7 @@ export class CsfFile {
});

if (!self._meta) {
throw new NoMetaError(self._ast, self._fileName);
throw new NoMetaError('missing default export', self._ast, self._fileName);
}

if (!self._meta.title && !self._meta.component) {
Expand Down

0 comments on commit 33101b2

Please sign in to comment.