diff --git a/.changeset/lovely-windows-shout.md b/.changeset/lovely-windows-shout.md new file mode 100644 index 000000000000..d0b748a876fa --- /dev/null +++ b/.changeset/lovely-windows-shout.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: generate correct code for simple destructurings 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 6ec789345230..22c4beb08ada 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 @@ -53,23 +53,22 @@ export function transform_body(instance_body, runner, transform) { transform(b.var(s.node.id, s.node.init)) ); - if (visited.declarations.length === 1) { - return b.thunk( - b.assignment('=', visited.declarations[0].id, visited.declarations[0].init ?? b.void0), - s.has_await - ); + const statements = visited.declarations.map((node) => { + if (node.id.type === 'Identifier' && node.id.name.startsWith('$$d')) { + // 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)); + }); + + if (statements.length === 1) { + const statement = /** @type {ESTree.ExpressionStatement} */ (statements[0]); + return b.thunk(statement.expression, s.has_await); } - // if we have multiple declarations, it indicates destructuring - return b.thunk( - b.block([ - b.var(visited.declarations[0].id, visited.declarations[0].init), - ...visited.declarations - .slice(1) - .map((d) => b.stmt(b.assignment('=', d.id, d.init ?? b.void0))) - ]), - s.has_await - ); + return b.thunk(b.block(statements), s.has_await); } if (s.node.type === 'ClassDeclaration') { diff --git a/packages/svelte/tests/runtime-runes/samples/async-derived-destructured/Child.svelte b/packages/svelte/tests/runtime-runes/samples/async-derived-destructured/Child.svelte index 39112b12a782..fdf7184e3c87 100644 --- a/packages/svelte/tests/runtime-runes/samples/async-derived-destructured/Child.svelte +++ b/packages/svelte/tests/runtime-runes/samples/async-derived-destructured/Child.svelte @@ -1,13 +1,17 @@

{count} ** 2 = {squared}

{count} ** 3 = {cubed}

+

{typeof toFixed} {typeof toString}

diff --git a/packages/svelte/tests/runtime-runes/samples/async-derived-destructured/_config.js b/packages/svelte/tests/runtime-runes/samples/async-derived-destructured/_config.js index d444e8e1d956..bfd582ea9719 100644 --- a/packages/svelte/tests/runtime-runes/samples/async-derived-destructured/_config.js +++ b/packages/svelte/tests/runtime-runes/samples/async-derived-destructured/_config.js @@ -13,6 +13,7 @@ export default test({

1 ** 2 = 1

1 ** 3 = 1

+

function function

` ); @@ -25,6 +26,7 @@ export default test({

2 ** 2 = 4

2 ** 3 = 8

+

function function

` ); }