Skip to content

Commit ab18c98

Browse files
committed
fix #13727
1 parent c36784c commit ab18c98

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

packages/svelte/src/compiler/migrate/index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,14 +558,22 @@ const instance_script = {
558558
const declaration = reference.path.find((el) => el.type === 'VariableDeclaration');
559559
const assignment = reference.path.find((el) => el.type === 'AssignmentExpression');
560560
const update = reference.path.find((el) => el.type === 'UpdateExpression');
561-
const labeled = reference.path.find(
562-
(el) => el.type === 'LabeledStatement' && el.label.name === '$'
561+
const labeled = /** @type {LabeledStatement | undefined} */ (
562+
reference.path.find((el) => el.type === 'LabeledStatement' && el.label.name === '$')
563563
);
564564

565-
if (assignment && labeled) {
565+
if (
566+
assignment &&
567+
labeled &&
568+
// ensure that $: foo = bar * 2 is not counted as a reassignment of bar
569+
(labeled.body.type !== 'ExpressionStatement' ||
570+
labeled.body.expression !== assignment ||
571+
(assignment.left.type === 'Identifier' &&
572+
assignment.left.name === binding.node.name))
573+
) {
566574
if (assignment_in_labeled) return false;
567575
assignment_in_labeled = /** @type {AssignmentExpression} */ (assignment);
568-
labeled_statement = /** @type {LabeledStatement} */ (labeled);
576+
labeled_statement = labeled;
569577
}
570578

571579
return (
@@ -739,6 +747,7 @@ const instance_script = {
739747
);
740748
const bindings = ids.map((id) => state.scope.get(id.name));
741749
const reassigned_bindings = bindings.filter((b) => b?.reassigned);
750+
742751
if (
743752
reassigned_bindings.length === 0 &&
744753
!bindings.some((b) => b?.kind === 'store_sub') &&

packages/svelte/tests/migrate/samples/derivations/output.svelte

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
// no semicolon at the end
77
let time_8 = $derived(count * 8)
88
let { time_16 } = $derived({ time_16: count * 16 })
9+
// preceeding let that doesn't do anything
10+
let time_32 = $derived(count * doubled);
11+
12+
let very_high = $derived(time_32 * count);
13+
914
</script>
1015

1116
{count} / {doubled} / {quadrupled} / {time_8} / {time_16}

packages/svelte/tests/migrate/samples/effects/input.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
console.log('bar');
1111
}
1212
$: $count = 1;
13-
$: $count.x = count;
13+
$: foo.x = count;
1414
</script>
1515

1616
<button onclick={() => count++}>increment</button>

packages/svelte/tests/migrate/samples/effects/output.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
$count = 1;
2020
});
2121
run(() => {
22-
$count.x = count;
22+
foo.x = count;
2323
});
2424
</script>
2525

0 commit comments

Comments
 (0)