Skip to content

fix: legacy reactive dependencies tweak #10128

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 1 commit into from
Jan 9, 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/quiet-crabs-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: take into account member expressions when determining legacy reactive dependencies
21 changes: 18 additions & 3 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,15 @@ const legacy_scope_tweaker = {
node.body.type === 'ExpressionStatement' &&
node.body.expression.type === 'AssignmentExpression'
) {
for (const id of extract_identifiers(node.body.expression.left)) {
let ids = extract_identifiers(node.body.expression.left);
if (node.body.expression.left.type === 'MemberExpression') {
const id = object(node.body.expression.left);
if (id !== null) {
ids = [id];
}
}

for (const id of ids) {
const binding = state.scope.get(id.name);
if (binding?.kind === 'legacy_reactive') {
// TODO does this include `let double; $: double = x * 2`?
Expand All @@ -511,8 +519,15 @@ const legacy_scope_tweaker = {
},
AssignmentExpression(node, { state, next }) {
if (state.reactive_statement && node.operator === '=') {
for (const id of extract_identifiers(node.left)) {
state.reactive_statement.assignments.add(id);
if (node.left.type === 'MemberExpression') {
const id = object(node.left);
if (id !== null) {
state.reactive_statement.assignments.add(id);
}
} else {
for (const id of extract_identifiers(node.left)) {
state.reactive_statement.assignments.add(id);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
html: `1 1`
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
let button = { title: '', label: '' };
let title = '';
let label = '';

title = label = '1'; // to add dependencies/generate update block

$: button.title = title;
$: button.label = label;
</script>

{button.title} {button.label}