Skip to content

Commit 5485873

Browse files
authored
Merge pull request #321 from storybookjs/jeppe/fix-raw-code-injection
Fix raw code not being injected with Svelte v5.35.1+
2 parents 4053be4 + de02add commit 5485873

File tree

13 files changed

+373
-372
lines changed

13 files changed

+373
-372
lines changed

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@
4949
"release": "pnpm run build && auto shipit",
5050
"start": "pnpm run build && concurrently \"pnpm run build --watch\" \"pnpm run storybook --quiet\"",
5151
"storybook": "storybook dev --port 6006 --no-open",
52-
"test": "vitest run",
53-
"test:watch": "vitest watch"
52+
"test": "vitest"
5453
},
5554
"dependencies": {
5655
"@storybook/csf": "^0.1.13",
@@ -66,8 +65,8 @@
6665
"@chromatic-com/storybook": "^4.0.0-next.6",
6766
"@eslint/compat": "^1.2.8",
6867
"@eslint/js": "^9.25.0",
69-
"@storybook/addon-vitest": "^9.0.0",
7068
"@storybook/addon-docs": "^9.0.0",
69+
"@storybook/addon-vitest": "^9.0.0",
7170
"@storybook/svelte-vite": "^9.0.0",
7271
"@sveltejs/package": "^2.3.7",
7372
"@sveltejs/vite-plugin-svelte": "^5.0.3",
@@ -90,7 +89,7 @@
9089
"rimraf": "^5.0.7",
9190
"rollup": "^4.25.0",
9291
"storybook": "^9.0.0",
93-
"svelte": "^5.28.2",
92+
"svelte": "5.35.1",
9493
"svelte-check": "^4.1.7",
9594
"tslib": "^2.6.3",
9695
"type-fest": "^4.20.1",

pnpm-lock.yaml

Lines changed: 340 additions & 340 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/compiler/pre-transform/codemods/component-meta-to-define-meta.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function transformComponentMetaToDefineMeta(params: Params): ESTreeAST.Va
2727
const { component, comment } = params;
2828
const { attributes, start, end } = component;
2929

30-
let properties: ESTreeAST.ObjectExpression['properties'] = [];
30+
const properties: ESTreeAST.ObjectExpression['properties'] = [];
3131

3232
for (const attribute of attributes) {
3333
if (attribute.type === 'Attribute') {
@@ -150,7 +150,7 @@ function transformTags(tags: SvelteAST.Attribute): void {
150150

151151
// tags.value is SvelteAST.ExpressionTag
152152
if (!Array.isArray(tags.value)) {
153-
if (typeof tags.value.expression.value !== 'string') {
153+
if (typeof (tags.value.expression as any).value !== 'string') {
154154
// NOTE: The error on invalid type (not a string) is likely visible
155155
// 1. via TypeScript
156156
// 2. and thrown by storybook internal, right?

src/compiler/pre-transform/codemods/export-const-to-define-meta.test.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ describe(transformExportMetaToDefineMeta.name, () => {
3131
} satisfies Meta<Button>;
3232
</script>
3333
`;
34-
const node = await parseAndExtractSvelteNode<ESTreeAST.ExportNamedDeclaration>(
35-
code,
36-
'ExportNamedDeclaration'
37-
);
34+
const node = await parseAndExtractSvelteNode<any>(code, 'ExportNamedDeclaration');
3835

3936
expect(print(transformExportMetaToDefineMeta(node))).toMatchInlineSnapshot(`
4037
"const { Story } = defineMeta({
@@ -66,10 +63,7 @@ describe(transformExportMetaToDefineMeta.name, () => {
6663
};
6764
</script>
6865
`;
69-
const node = await parseAndExtractSvelteNode<ESTreeAST.ExportNamedDeclaration>(
70-
code,
71-
'ExportNamedDeclaration'
72-
);
66+
const node = await parseAndExtractSvelteNode<any>(code, 'ExportNamedDeclaration');
7367

7468
expect(print(transformExportMetaToDefineMeta(node))).toMatchInlineSnapshot(`
7569
"/**

src/compiler/pre-transform/codemods/import-declaration.test.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ describe(transformImportDeclaration.name, () => {
1313
import { Story, Template } from "@storybook/addon-svelte-csf";
1414
</script>
1515
`;
16-
const node = await parseAndExtractSvelteNode<ESTreeAST.ImportDeclaration>(
17-
code,
18-
'ImportDeclaration'
19-
);
16+
const node = await parseAndExtractSvelteNode<any>(code, 'ImportDeclaration');
2017

2118
expect(print(transformImportDeclaration({ node }))).toMatchInlineSnapshot(
2219
`"import { defineMeta } from "@storybook/addon-svelte-csf";"`
@@ -29,10 +26,7 @@ describe(transformImportDeclaration.name, () => {
2926
import { Story, Template, defineMeta } from "@storybook/addon-svelte-csf";
3027
</script>
3128
`;
32-
const node = await parseAndExtractSvelteNode<ESTreeAST.ImportDeclaration>(
33-
code,
34-
'ImportDeclaration'
35-
);
29+
const node = await parseAndExtractSvelteNode<any>(code, 'ImportDeclaration');
3630

3731
expect(print(transformImportDeclaration({ node }))).toMatchInlineSnapshot(
3832
`"import { defineMeta } from "@storybook/addon-svelte-csf";"`

src/compiler/pre-transform/index.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export async function codemodLegacyNodes(params: Params): Promise<SvelteAST.Root
9696
return transformed;
9797
}
9898

99-
return node;
99+
return node as any;
100100
},
101101

102102
ExportNamedDeclaration(node, context) {
@@ -235,7 +235,7 @@ export async function codemodLegacyNodes(params: Params): Promise<SvelteAST.Root
235235

236236
state.currentScript = scriptContext === 'module' ? 'module' : 'instance';
237237

238-
content = visit(content, state) as ESTreeAST.Program;
238+
content = visit(content, state) as any;
239239

240240
return { ...rest, content, context: scriptContext };
241241
},
@@ -265,24 +265,28 @@ export async function codemodLegacyNodes(params: Params): Promise<SvelteAST.Root
265265
if (declaration.type === 'VariableDeclaration') {
266266
const { init } = declaration.declarations[0];
267267

268-
if (init?.type === 'CallExpression' && init.callee.name === 'defineMeta') {
268+
if (
269+
init?.type === 'CallExpression' &&
270+
'name' in init.callee &&
271+
init.callee.name === 'defineMeta'
272+
) {
269273
continue;
270274
}
271275
}
272276

273277
instanceBody.push(declaration);
274278
}
275279

276-
node.body = instanceBody;
280+
node.body = instanceBody as any;
277281
}
278282

279283
if (state.currentScript === 'module') {
280284
if (state.storiesComponentImportDeclaration) {
281-
node.body.unshift(state.storiesComponentImportDeclaration);
285+
node.body.unshift(state.storiesComponentImportDeclaration as any);
282286
}
283287

284288
if (state.pkgImportDeclaration) {
285-
node.body.unshift(state.pkgImportDeclaration);
289+
node.body.unshift(state.pkgImportDeclaration as any);
286290
}
287291

288292
if (state.defineMetaFromExportConstMeta) {

src/indexer/parser.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ export async function parseForIndexer(
140140
if (specifier.type !== 'ImportSpecifier') {
141141
throw new DefaultOrNamespaceImportUsedError(filename);
142142
}
143+
if (!('name' in specifier.imported)) {
144+
return;
145+
}
143146

144147
if (specifier.imported.name === 'defineMeta') {
145148
state.defineMetaImport = specifier;

src/parser/extract/compiled/stories.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export async function extractStoriesNodesFromExportDefaultFn(params: Params) {
2323

2424
if (node.callee.type === 'Identifier' && node.callee.name === storyIdentifier.name) {
2525
state.push(node);
26+
} else {
27+
context.next();
2628
}
2729
},
2830
};

src/parser/extract/svelte/module-nodes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
MissingModuleTagError,
1010
NoStoryComponentDestructuredError,
1111
} from '$lib/utils/error/parser/extract/svelte.js';
12+
import type { Identifier } from 'estree';
1213

1314
const AST_NODES_NAMES = {
1415
defineMeta: 'defineMeta',
@@ -69,7 +70,7 @@ export async function extractModuleNodes(options: Params): Promise<Result> {
6970
},
7071

7172
ImportSpecifier(node) {
72-
if (node.imported.name === AST_NODES_NAMES.defineMeta) {
73+
if ('name' in node.imported && node.imported.name === AST_NODES_NAMES.defineMeta) {
7374
state.defineMetaImport = node;
7475
}
7576
},

src/parser/extract/svelte/snippet-block.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function findStoryAttributeTemplateSnippetBlock(options: {
4242
}
4343

4444
return findSnippetBlockByName({
45-
name: template.value.expression.name,
45+
name: (template.value.expression as any).name,
4646
nodes: nodes,
4747
});
4848
}

0 commit comments

Comments
 (0)