Skip to content

Commit d824a07

Browse files
authored
fix: Allow user-defined local variable meta in stories (#309)
Resolves #306
1 parent 7f3d46b commit d824a07

File tree

11 files changed

+53
-23
lines changed

11 files changed

+53
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ This story renders:
110110
This is now forwarded to the component
111111
</MyComponent>
112112
-->
113-
<Story name="MyComponent children">
114-
This is now forwarded to the component
115-
</Story>
113+
<Story name="MyComponent children">This is now forwarded to the component</Story>
116114
```
117115

118116
To get the same behavior as previously, a new `asChild` boolean prop has been introduced on the `Story` component. `asChild` is a common prop in UI libraries, where you want the `children` to _be_ the output, instead of just being children of the Component. By adding that you can get the old behavior back, when you need more control over what the story renders:

src/compiler/post-transform/appendix/create-variable-from-runtime-stories-call.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe(createVariableFromRuntimeStoriesCall.name, () => {
2323
).code;
2424

2525
expect(stringified).toMatchInlineSnapshot(
26-
`"const __stories = createRuntimeStories(Example_stories, meta);"`
26+
`"const __stories = createRuntimeStories(Example_stories, $__meta);"`
2727
);
2828
});
2929
});

src/compiler/post-transform/appendix/create-variable-from-runtime-stories-call.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { STORYBOOK_META_IDENTIFIER } from '$lib/constants.js';
12
import { createASTIdentifier, type ESTreeAST } from '$lib/parser/ast.js';
23

34
interface Params {
@@ -34,7 +35,7 @@ export function createVariableFromRuntimeStoriesCall(
3435
type: 'Identifier',
3536
name: storiesFunctionDeclaration.id.name,
3637
},
37-
createASTIdentifier('meta'),
38+
createASTIdentifier(STORYBOOK_META_IDENTIFIER),
3839
],
3940
},
4041
},

src/compiler/post-transform/create-appendix.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { createRuntimeStoriesImport } from './appendix/create-import.js';
66
import { createVariableFromRuntimeStoriesCall } from './appendix/create-variable-from-runtime-stories-call.js';
77
import { createNamedExportStory } from './appendix/create-named-export-story.js';
88

9+
import { STORYBOOK_META_IDENTIFIER } from '$lib/constants.js';
910
import { createASTIdentifier, type ESTreeAST, type SvelteAST } from '$lib/parser/ast.js';
1011
import { getStoriesIdentifiers } from '$lib/parser/analyse/story/attributes/identifiers.js';
1112
import type { CompiledASTNodes } from '$lib/parser/extract/compiled/nodes.js';
@@ -65,7 +66,7 @@ export async function createAppendix(params: Params) {
6566
function createExportDefaultMeta(): ESTreeAST.ExportDefaultDeclaration {
6667
return {
6768
type: 'ExportDefaultDeclaration',
68-
declaration: createASTIdentifier('meta'),
69+
declaration: createASTIdentifier(STORYBOOK_META_IDENTIFIER),
6970
};
7071
}
7172

src/compiler/post-transform/define-meta/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe(transformDefineMeta.name, () => {
5454
});
5555

5656
expect(print(defineMetaVariableDeclaration).code).toMatchInlineSnapshot(
57-
`"const { Story } = defineMeta(meta);"`
57+
`"const { Story } = defineMeta($__meta);"`
5858
);
5959
});
6060
});
@@ -97,7 +97,7 @@ describe(createMetaVariableDeclaration.name, () => {
9797
const metaVariableDeclaration = createMetaVariableDeclaration({ init: metaObjectExpression });
9898

9999
expect(print(metaVariableDeclaration).code).toMatchInlineSnapshot(`
100-
"const meta = {
100+
"const $__meta = {
101101
title: 'Example',
102102
component: Example,
103103
tags: ['autodocs'],

src/compiler/post-transform/define-meta/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type MagicString from 'magic-string';
44
import { replaceDefineMetaArgument } from './replace-argument.js';
55
import { insertDefineMetaParameters } from './insert-parameters.js';
66

7+
import { STORYBOOK_META_IDENTIFIER } from '$lib/constants.js';
78
import { createASTIdentifier, type ESTreeAST } from '$lib/parser/ast.js';
89
import type { CompiledASTNodes } from '$lib/parser/extract/compiled/nodes.js';
910
import type { SvelteASTNodes } from '$lib/parser/extract/svelte/nodes.js';
@@ -47,7 +48,7 @@ export function createMetaVariableDeclaration({
4748
declarations: [
4849
{
4950
type: 'VariableDeclarator',
50-
id: createASTIdentifier('meta'),
51+
id: createASTIdentifier(STORYBOOK_META_IDENTIFIER),
5152
init,
5253
},
5354
],

src/compiler/post-transform/define-meta/replace-argument.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import type { SvelteASTNodes } from '../../../parser/extract/svelte/nodes.js';
44
import { getDefineMetaFirstArgumentObjectExpression } from '../../../parser/extract/svelte/define-meta.js';
55
import { NoDestructuredDefineMetaCallError } from '../../../utils/error/parser/analyse/define-meta.js';
66

7+
import { STORYBOOK_META_IDENTIFIER } from '$lib/constants.js';
8+
79
interface Params {
810
nodes: {
911
compiled: CompiledASTNodes;
@@ -36,7 +38,7 @@ export function replaceDefineMetaArgument(params: Params): ESTreeAST.ObjectExpre
3638
});
3739
}
3840

39-
declaration.init.arguments[0] = createASTIdentifier('meta');
41+
declaration.init.arguments[0] = createASTIdentifier(STORYBOOK_META_IDENTIFIER);
4042
params.nodes.compiled.defineMetaVariableDeclaration.declarations[0] = declaration;
4143

4244
return defineMetaFirstArgumentObjectExpression;

src/compiler/post-transform/index.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe(transformStoriesCode.name, () => {
6464
* * _Italic_,
6565
* * \`Code\`.
6666
*/
67-
const meta = {
67+
const $__meta = {
6868
title: 'Example',
6969
component: Example,
7070
tags: ['autodocs'],
@@ -81,7 +81,7 @@ describe(transformStoriesCode.name, () => {
8181
}
8282
}
8383
};
84-
const { Story } = defineMeta(meta);
84+
const { Story } = defineMeta($__meta);
8585
8686
var root_2 = $.add_locations($.template(\`<p> </p> <p> </p> <br>\`, 1), Example_stories[$.FILENAME], [[41, 2], [42, 2], [42, 44]]);
8787
var root = $.add_locations($.template(\`<!> <!> <!> <!> <!>\`, 1), Example_stories[$.FILENAME], []);
@@ -254,9 +254,9 @@ describe(transformStoriesCode.name, () => {
254254
255255
import { createRuntimeStories } from "@storybook/addon-svelte-csf/internal/create-runtime-stories";
256256
257-
const __stories = createRuntimeStories(Example_stories, meta);
257+
const __stories = createRuntimeStories(Example_stories, $__meta);
258258
259-
export default meta;
259+
export default $__meta;
260260
261261
export const __namedExportsOrder = [
262262
"Default",

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const SVELTE_CSF_TAG_PREFIX = 'svelte-csf';
22
export const SVELTE_CSF_V4_TAG = `${SVELTE_CSF_TAG_PREFIX}-v4`;
33
export const SVELTE_CSF_V5_TAG = `${SVELTE_CSF_TAG_PREFIX}-v5`;
4+
export const STORYBOOK_META_IDENTIFIER = '$__meta';

src/indexer/parser.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,16 +209,8 @@ export async function parseForIndexer(
209209
if (legacyTemplate && !foundMeta && id.type === 'Identifier') {
210210
const { name } = id;
211211

212-
if (name === 'meta') {
212+
if (name === 'meta' && init?.type === 'ObjectExpression') {
213213
foundMeta = true;
214-
215-
if (init?.type !== 'ObjectExpression') {
216-
throw new GetDefineMetaFirstArgumentError({
217-
filename,
218-
defineMetaVariableDeclaration: node,
219-
});
220-
}
221-
222214
visit(init, state);
223215
}
224216
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<script module>
2+
import { defineMeta } from '@storybook/addon-svelte-csf';
3+
import { expect, within } from '@storybook/test';
4+
5+
/**
6+
* Testing if there's no compile error when there's a user defined `meta` identifier.
7+
*/
8+
const { Story } = defineMeta({
9+
parameters: {
10+
controls: { disable: true },
11+
},
12+
});
13+
</script>
14+
15+
<script>
16+
let meta = $state({
17+
framework: 'svelte',
18+
});
19+
let output = $derived(JSON.stringify(meta, undefined, 2));
20+
</script>
21+
22+
<Story
23+
name="LocalMeta"
24+
play={async (context) => {
25+
const { canvasElement } = context;
26+
const canvas = within(canvasElement);
27+
const pre = canvas.getByTestId('output');
28+
29+
expect(pre.textContent).toEqual(output);
30+
}}
31+
>
32+
<p>It works! 🎉</p>
33+
<pre data-testid="output">{output}</pre>
34+
</Story>

0 commit comments

Comments
 (0)