diff --git a/.changeset/kind-dolls-cry.md b/.changeset/kind-dolls-cry.md new file mode 100644 index 0000000000..c6653d2f7f --- /dev/null +++ b/.changeset/kind-dolls-cry.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: account for empty statements when visiting in transform async diff --git a/packages/svelte/src/compiler/phases/3-transform/shared/transform-async.js b/packages/svelte/src/compiler/phases/3-transform/shared/transform-async.js index 8d3a6b2769..95b9f585d3 100644 --- a/packages/svelte/src/compiler/phases/3-transform/shared/transform-async.js +++ b/packages/svelte/src/compiler/phases/3-transform/shared/transform-async.js @@ -49,22 +49,25 @@ export function transform_body(instance_body, runner, transform) { if (instance_body.async.length > 0) { const thunks = instance_body.async.map((s) => { if (s.node.type === 'VariableDeclarator') { - const visited = /** @type {ESTree.VariableDeclaration} */ ( + const visited = /** @type {ESTree.VariableDeclaration | ESTree.EmptyStatement} */ ( transform(b.var(s.node.id, s.node.init)) ); - const statements = visited.declarations.map((node) => { - if ( - node.id.type === 'Identifier' && - (node.id.name.startsWith('$$d') || node.id.name.startsWith('$$array')) - ) { - // this is an intermediate declaration created in VariableDeclaration.js; - // subsequent statements depend on it - return b.var(node.id, node.init); - } + const statements = + visited.type === 'VariableDeclaration' + ? visited.declarations.map((node) => { + if ( + node.id.type === 'Identifier' && + (node.id.name.startsWith('$$d') || node.id.name.startsWith('$$array')) + ) { + // this is an intermediate declaration created in VariableDeclaration.js; + // subsequent statements depend on it + return b.var(node.id, node.init); + } - return b.stmt(b.assignment('=', node.id, node.init ?? b.void0)); - }); + return b.stmt(b.assignment('=', node.id, node.init ?? b.void0)); + }) + : []; if (statements.length === 1) { const statement = /** @type {ESTree.ExpressionStatement} */ (statements[0]); diff --git a/packages/svelte/tests/runtime-runes/samples/async-transform-empty-statements/_config.js b/packages/svelte/tests/runtime-runes/samples/async-transform-empty-statements/_config.js new file mode 100644 index 0000000000..2e4a27cf09 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/async-transform-empty-statements/_config.js @@ -0,0 +1,5 @@ +import { test } from '../../test'; + +export default test({ + async test() {} +}); diff --git a/packages/svelte/tests/runtime-runes/samples/async-transform-empty-statements/main.svelte b/packages/svelte/tests/runtime-runes/samples/async-transform-empty-statements/main.svelte new file mode 100644 index 0000000000..03c4ada55a --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/async-transform-empty-statements/main.svelte @@ -0,0 +1,6 @@ + + +{name} \ No newline at end of file