fix: legacy reactive dependencies tweak (#10128)

take into account member expressions when determining legacy reactive dependencies
fixes #9954
pull/10132/head
Simon H 12 months ago committed by GitHub
parent 901cfc9f15
commit aa5a62396a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: take into account member expressions when determining legacy reactive dependencies

@ -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`?
@ -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);
}
}
}

@ -0,0 +1,5 @@
import { test } from '../../test';
export default test({
html: `1 1`
});

@ -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}
Loading…
Cancel
Save