Skip to content

fix: more accurately detect $derived migration use cases #13740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nasty-geese-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: more accurately detect `$derived` migration opportunities
23 changes: 18 additions & 5 deletions packages/svelte/src/compiler/migrate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,14 +558,22 @@ const instance_script = {
const declaration = reference.path.find((el) => el.type === 'VariableDeclaration');
const assignment = reference.path.find((el) => el.type === 'AssignmentExpression');
const update = reference.path.find((el) => el.type === 'UpdateExpression');
const labeled = reference.path.find(
(el) => el.type === 'LabeledStatement' && el.label.name === '$'
const labeled = /** @type {LabeledStatement | undefined} */ (
reference.path.find((el) => el.type === 'LabeledStatement' && el.label.name === '$')
);

if (assignment && labeled) {
if (
assignment &&
labeled &&
// ensure that $: foo = bar * 2 is not counted as a reassignment of bar
(labeled.body.type !== 'ExpressionStatement' ||
labeled.body.expression !== assignment ||
(assignment.left.type === 'Identifier' &&
assignment.left.name === binding.node.name))
) {
if (assignment_in_labeled) return false;
assignment_in_labeled = /** @type {AssignmentExpression} */ (assignment);
labeled_statement = /** @type {LabeledStatement} */ (labeled);
labeled_statement = labeled;
}

return (
Expand Down Expand Up @@ -739,7 +747,12 @@ 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')) {

if (
reassigned_bindings.length === 0 &&
!bindings.some((b) => b?.kind === 'store_sub') &&
node.body.expression.left.type !== 'MemberExpression'
) {
let { start, end } = /** @type {{ start: number, end: number }} */ (
node.body.expression.right
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
// no semicolon at the end
$: time_8 = count * 8
$: ({ time_16 } = { time_16: count * 16 })
// preceeding let that doesn't do anything
let time_32;
$: time_32 = count * doubled;
let very_high;
$: very_high = time_32 * count;
</script>

{count} / {doubled} / {quadrupled} / {time_8} / {time_16}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
// no semicolon at the end
let time_8 = $derived(count * 8)
let { time_16 } = $derived({ time_16: count * 16 })
// preceeding let that doesn't do anything
let time_32 = $derived(count * doubled);

let very_high = $derived(time_32 * count);

</script>

{count} / {doubled} / {quadrupled} / {time_8} / {time_16}
3 changes: 3 additions & 0 deletions packages/svelte/tests/migrate/samples/effects/input.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
console.log('bar');
}
$: $count = 1;
$: foo.x = count;
</script>

<button onclick={() => count++}>increment</button>
9 changes: 7 additions & 2 deletions packages/svelte/tests/migrate/samples/effects/output.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
import { run } from 'svelte/legacy';

let count = 0;
let count = $state(0);
run(() => {
console.log(count);
});
Expand All @@ -18,4 +18,9 @@
run(() => {
$count = 1;
});
</script>
run(() => {
foo.x = count;
});
</script>

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