Skip to content

Commit

Permalink
fix: add ; in switch
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Nov 26, 2024
1 parent eafe491 commit 31a436b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
16 changes: 15 additions & 1 deletion packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,13 @@ f()
x
}
f()
switch (1) {
case 1:
x
f()
break
}
`),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./f", {"importedNames":["f"]});
Expand Down Expand Up @@ -1302,7 +1309,14 @@ f()
{
x
}
(0,__vite_ssr_import_0__.f)()
(0,__vite_ssr_import_0__.f)();
switch (1) {
case 1:
x;
(0,__vite_ssr_import_0__.f)();
break
}
"
`)
})
5 changes: 4 additions & 1 deletion packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ async function ssrTransformScript(
onStatements(statements) {
// ensure ";" between statements
for (let i = 0; i < statements.length - 1; i++) {
const stmt = statements[i] as Node
const stmt = statements[i]
if (
code[stmt.end - 1] !== ';' &&
stmt.type !== 'FunctionDeclaration' &&
Expand Down Expand Up @@ -507,12 +507,15 @@ function walk(
return this.skip()
}

// for nodes that can contain multiple statements
if (
node.type === 'Program' ||
node.type === 'BlockStatement' ||
node.type === 'StaticBlock'
) {
onStatements(node.body as Node[])
} else if (node.type === 'SwitchCase') {
onStatements(node.consequent as Node[])
}

// track parent stack, skip for "else-if"/"else" branches as acorn nests
Expand Down

0 comments on commit 31a436b

Please sign in to comment.