diff --git a/.changeset/ninety-dots-train.md b/.changeset/ninety-dots-train.md new file mode 100644 index 0000000000..2afde212b1 --- /dev/null +++ b/.changeset/ninety-dots-train.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +fix: detect style shorthands as stateful variables in legacy mode diff --git a/packages/svelte/src/compiler/phases/2-analyze/index.js b/packages/svelte/src/compiler/phases/2-analyze/index.js index 7a55c575ff..64d35f3989 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/index.js +++ b/packages/svelte/src/compiler/phases/2-analyze/index.js @@ -870,6 +870,16 @@ const legacy_scope_tweaker = { } } } + }, + StyleDirective(node, { state }) { + // the case for node.value different from true is already covered by the Identifier visitor + if (node.value === true) { + // get the binding for node.name and change the binding to state + let binding = state.scope.get(node.name); + if (binding?.mutated && binding.kind === 'normal') { + binding.kind = 'state'; + } + } } }; diff --git a/packages/svelte/tests/runtime-legacy/samples/inline-style-directive-shorthand-declaration-only/_config.js b/packages/svelte/tests/runtime-legacy/samples/inline-style-directive-shorthand-declaration-only/_config.js new file mode 100644 index 0000000000..dfcd5a31fe --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/inline-style-directive-shorthand-declaration-only/_config.js @@ -0,0 +1,33 @@ +import { test } from '../../test'; + +export default test({ + html: ` +
+ + + `, + + async test({ assert, target, window }) { + const [p1, p2] = target.querySelectorAll('p'); + + assert.equal(window.getComputedStyle(p1).color, 'red'); + assert.equal(window.getComputedStyle(p2).color, 'red'); + + const btn = target.querySelector('button'); + console.log(btn); + btn?.click(); + await Promise.resolve(); + + assert.htmlEqual( + target.innerHTML, + ` + + + + ` + ); + + assert.equal(window.getComputedStyle(p1).color, 'green'); + assert.equal(window.getComputedStyle(p2).color, 'green'); + } +}); diff --git a/packages/svelte/tests/runtime-legacy/samples/inline-style-directive-shorthand-declaration-only/main.svelte b/packages/svelte/tests/runtime-legacy/samples/inline-style-directive-shorthand-declaration-only/main.svelte new file mode 100644 index 0000000000..9213a5bb2e --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/inline-style-directive-shorthand-declaration-only/main.svelte @@ -0,0 +1,15 @@ + + + + +{#each [1] as _} + +{/each} + + \ No newline at end of file