Skip to content

Commit 74728f2

Browse files
committed
Fix meta.story()
1 parent 84a35c7 commit 74728f2

2 files changed

Lines changed: 36 additions & 13 deletions

File tree

code/renderers/react/src/componentManifest/generateCodeSnippet.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ test('Default- CSF4', () => {
9797
);
9898
});
9999

100+
test('StoryWithoutArguments - CSF4', () => {
101+
const input = withCSF4(`
102+
export const StoryWithoutArguments = meta.story();
103+
`);
104+
expect(generateExample(input)).toMatchInlineSnapshot(
105+
`"const StoryWithoutArguments = () => <Button>Click me</Button>;"`
106+
);
107+
});
108+
100109
test('Replace children', () => {
101110
const input = withCSF3(dedent`
102111
export const WithEmoji: Story = {

code/renderers/react/src/componentManifest/generateCodeSnippet.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,21 @@ export function getCodeSnippet(
5858

5959
if (storyPath === normalizedPath) {
6060
const args = storyPath.get('arguments');
61-
invariant(
62-
args.length === 1,
63-
() => storyPath.buildCodeFrameError('Could not evaluate story expression').message
64-
);
65-
const storyArg = args[0];
66-
invariant(
67-
storyArg.isExpression(),
68-
() => storyPath.buildCodeFrameError('Could not evaluate story expression').message
69-
);
70-
normalizedPath = storyArg;
61+
// Allow meta.story() with zero args (CSF4)
62+
if (args.length === 0) {
63+
// Leave normalizedPath as the CallExpression; we'll treat it as an empty story config later
64+
} else {
65+
invariant(
66+
args.length === 1,
67+
() => storyPath.buildCodeFrameError('Could not evaluate story expression').message
68+
);
69+
const storyArg = args[0];
70+
invariant(
71+
storyArg.isExpression(),
72+
() => storyPath.buildCodeFrameError('Could not evaluate story expression').message
73+
);
74+
normalizedPath = storyArg;
75+
}
7176
}
7277
}
7378

@@ -90,9 +95,18 @@ export function getCodeSnippet(
9095
) {
9196
storyFn = normalizedPath;
9297
} else if (!normalizedPath.isObjectExpression()) {
93-
throw normalizedPath.buildCodeFrameError(
94-
'Expected story to be csf factory, function or an object expression'
95-
);
98+
// Allow CSF4 meta.story() without arguments, which is equivalent to an empty object story config
99+
if (
100+
normalizedPath.isCallExpression() &&
101+
Array.isArray(normalizedPath.node.arguments) &&
102+
normalizedPath.node.arguments.length === 0
103+
) {
104+
// No-op: treat as an object story with no properties
105+
} else {
106+
throw normalizedPath.buildCodeFrameError(
107+
'Expected story to be csf factory, function or an object expression'
108+
);
109+
}
96110
}
97111

98112
const storyProps = normalizedPath.isObjectExpression()

0 commit comments

Comments
 (0)