Skip to content

Commit c2566ce

Browse files
committed
better fix
1 parent dc8ab17 commit c2566ce

File tree

3 files changed

+30
-36
lines changed

3 files changed

+30
-36
lines changed

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -549,27 +549,28 @@ const instance_script = {
549549
let labeled_statement;
550550

551551
// Analyze declaration bindings to see if they're exclusively updated within a single reactive statement
552-
const possible_derived = bindings.every(
553-
(binding) =>
554-
binding.initial !== null &&
555-
binding.references.every((reference) => {
556-
const declaration = reference.path.find((el) => el.type === 'VariableDeclaration');
557-
const assignment = reference.path.find((el) => el.type === 'AssignmentExpression');
558-
const update = reference.path.find((el) => el.type === 'UpdateExpression');
559-
const labeled = reference.path.find(
560-
(el) => el.type === 'LabeledStatement' && el.label.name === '$'
561-
);
552+
const possible_derived = bindings.every((binding) =>
553+
binding.references.every((reference) => {
554+
const declaration = reference.path.find((el) => el.type === 'VariableDeclaration');
555+
const assignment = reference.path.find((el) => el.type === 'AssignmentExpression');
556+
const update = reference.path.find((el) => el.type === 'UpdateExpression');
557+
const labeled = reference.path.find(
558+
(el) => el.type === 'LabeledStatement' && el.label.name === '$'
559+
);
562560

563-
if (assignment && labeled) {
564-
if (assignment_in_labeled) return false;
565-
assignment_in_labeled = /** @type {AssignmentExpression} */ (assignment);
566-
labeled_statement = /** @type {LabeledStatement} */ (labeled);
567-
}
561+
if (assignment && labeled) {
562+
if (assignment_in_labeled) return false;
563+
assignment_in_labeled = /** @type {AssignmentExpression} */ (assignment);
564+
labeled_statement = /** @type {LabeledStatement} */ (labeled);
565+
}
568566

569-
return (
570-
!update && (declaration || (labeled && assignment) || (!labeled && !assignment))
571-
);
572-
})
567+
return (
568+
!update &&
569+
((declaration && binding.initial) ||
570+
(labeled && assignment) ||
571+
(!labeled && !assignment))
572+
);
573+
})
573574
);
574575

575576
const labeled_has_single_assignment =

packages/svelte/tests/migrate/samples/reactive-statements-reorder-with-comments/output.svelte

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
77
88
// triple
9-
let triple = $state();
9+
// update triple
10+
let triple = $derived(count * 3)
11+
// trailing comment
12+
// in triple;
1013
1114
function increment() {
1215
count += 1;
@@ -16,12 +19,7 @@
1619
run(() => {
1720
console.log({ count, double });
1821
});
19-
run(() => {
20-
// update triple
21-
triple = count * 3;
22-
// trailing comment
23-
// in triple
24-
});
22+
2523
</script>
2624

2725
<button onclick={increment}>

packages/svelte/tests/migrate/samples/single-assignment-labeled/output.svelte

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
import { run } from 'svelte/legacy';
33
44
let count = $state(0);
5-
let double = $state();
6-
run(() => {
7-
double = count * 2;
8-
});
5+
let double = $derived(count * 2);
6+
97
108
let quadruple = $state();
119
run(() => {
@@ -43,13 +41,10 @@
4341
evenmore_doubled = evenmore * 2;
4442
});
4543
46-
let almost_infinity = $state();
47-
run(() => {
48-
almost_infinity = count * 128;
49-
});
44+
let almost_infinity = $derived(count * 128);
45+
5046
51-
let should_be_state = $state();
52-
let should_be_state = $state();
47+
let should_be_state = $state(42);
5348
5449
let should_be_state_too = $state(42);
5550

0 commit comments

Comments
 (0)