diff --git a/.changeset/brave-candles-serve.md b/.changeset/brave-candles-serve.md new file mode 100644 index 0000000000..f758e3cc76 --- /dev/null +++ b/.changeset/brave-candles-serve.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: correctly migrate sequence expressions diff --git a/packages/svelte/src/compiler/migrate/index.js b/packages/svelte/src/compiler/migrate/index.js index c14b577319..e4c759083d 100644 --- a/packages/svelte/src/compiler/migrate/index.js +++ b/packages/svelte/src/compiler/migrate/index.js @@ -368,8 +368,15 @@ const instance_script = { // state if (declarator.init) { - state.str.prependLeft(/** @type {number} */ (declarator.init.start), '$state('); - state.str.appendRight(/** @type {number} */ (declarator.init.end), ')'); + let { start, end } = /** @type {{ start: number, end: number }} */ (declarator.init); + + if (declarator.init.type === 'SequenceExpression') { + while (state.str.original[start] !== '(') start -= 1; + while (state.str.original[end - 1] !== ')') end += 1; + } + + state.str.prependLeft(start, '$state('); + state.str.appendRight(end, ')'); } else { state.str.prependLeft( /** @type {number} */ (declarator.id.typeAnnotation?.end ?? declarator.id.end), @@ -416,25 +423,30 @@ const instance_script = { const bindings = ids.map((id) => state.scope.get(id.name)); const reassigned_bindings = bindings.filter((b) => b?.reassigned); if (reassigned_bindings.length === 0 && !bindings.some((b) => b?.kind === 'store_sub')) { + let { start, end } = /** @type {{ start: number, end: number }} */ ( + node.body.expression.right + ); + // $derived state.str.update( /** @type {number} */ (node.start), /** @type {number} */ (node.body.expression.start), 'let ' ); - state.str.prependRight( - /** @type {number} */ (node.body.expression.right.start), - '$derived(' - ); - if (node.body.expression.right.end !== node.end) { - state.str.update( - /** @type {number} */ (node.body.expression.right.end), - /** @type {number} */ (node.end), - ');' - ); - } else { - state.str.appendLeft(/** @type {number} */ (node.end), ');'); + + if (node.body.expression.right.type === 'SequenceExpression') { + while (state.str.original[start] !== '(') start -= 1; + while (state.str.original[end - 1] !== ')') end += 1; } + + state.str.prependRight(start, `$derived(`); + + // in a case like `$: ({ a } = b())`, there's already a trailing parenthesis. + // otherwise, we need to add one + if (state.str.original[/** @type {number} */ (node.body.start)] !== '(') { + state.str.appendLeft(end, `)`); + } + return; } else { for (const binding of reassigned_bindings) { diff --git a/packages/svelte/tests/migrate/samples/derivations-no-colon/input.svelte b/packages/svelte/tests/migrate/samples/derivations-no-colon/input.svelte deleted file mode 100644 index 7df9187a02..0000000000 --- a/packages/svelte/tests/migrate/samples/derivations-no-colon/input.svelte +++ /dev/null @@ -1,7 +0,0 @@ - - -{count} / {doubled} / {quadrupled} diff --git a/packages/svelte/tests/migrate/samples/derivations-no-colon/output.svelte b/packages/svelte/tests/migrate/samples/derivations-no-colon/output.svelte deleted file mode 100644 index f6af6dd450..0000000000 --- a/packages/svelte/tests/migrate/samples/derivations-no-colon/output.svelte +++ /dev/null @@ -1,7 +0,0 @@ - - -{count} / {doubled} / {quadrupled} \ No newline at end of file diff --git a/packages/svelte/tests/migrate/samples/derivations/input.svelte b/packages/svelte/tests/migrate/samples/derivations/input.svelte index 9fde4a4359..42b47b8272 100644 --- a/packages/svelte/tests/migrate/samples/derivations/input.svelte +++ b/packages/svelte/tests/migrate/samples/derivations/input.svelte @@ -1,7 +1,11 @@ -{count} / {doubled} / {quadrupled} +{count} / {doubled} / {quadrupled} / {time_8} / {time_16} diff --git a/packages/svelte/tests/migrate/samples/derivations/output.svelte b/packages/svelte/tests/migrate/samples/derivations/output.svelte index f6af6dd450..e797a4da1e 100644 --- a/packages/svelte/tests/migrate/samples/derivations/output.svelte +++ b/packages/svelte/tests/migrate/samples/derivations/output.svelte @@ -1,7 +1,11 @@ -{count} / {doubled} / {quadrupled} \ No newline at end of file +{count} / {doubled} / {quadrupled} / {time_8} / {time_16} diff --git a/packages/svelte/tests/migrate/samples/state-and-derivations-sequence/input.svelte b/packages/svelte/tests/migrate/samples/state-and-derivations-sequence/input.svelte new file mode 100644 index 0000000000..487f72429c --- /dev/null +++ b/packages/svelte/tests/migrate/samples/state-and-derivations-sequence/input.svelte @@ -0,0 +1,14 @@ + + + + + +{count} / {doubled} / {quadrupled} / {time_8} / {time_16} diff --git a/packages/svelte/tests/migrate/samples/state-and-derivations-sequence/output.svelte b/packages/svelte/tests/migrate/samples/state-and-derivations-sequence/output.svelte new file mode 100644 index 0000000000..bd9fe82142 --- /dev/null +++ b/packages/svelte/tests/migrate/samples/state-and-derivations-sequence/output.svelte @@ -0,0 +1,14 @@ + + + + + +{count} / {doubled} / {quadrupled} / {time_8} / {time_16}