Skip to content

Commit f64d169

Browse files
fix: detect style shorthands as stateful variables in legacy mode (#11421)
Fixes #11417
1 parent 0f4f3d7 commit f64d169

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

.changeset/ninety-dots-train.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: detect style shorthands as stateful variables in legacy mode

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,16 @@ const legacy_scope_tweaker = {
870870
}
871871
}
872872
}
873+
},
874+
StyleDirective(node, { state }) {
875+
// the case for node.value different from true is already covered by the Identifier visitor
876+
if (node.value === true) {
877+
// get the binding for node.name and change the binding to state
878+
let binding = state.scope.get(node.name);
879+
if (binding?.mutated && binding.kind === 'normal') {
880+
binding.kind = 'state';
881+
}
882+
}
873883
}
874884
};
875885

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
html: `
5+
<p style="color: red;"></p>
6+
<p style="color: red;"></p>
7+
<button></button>
8+
`,
9+
10+
async test({ assert, target, window }) {
11+
const [p1, p2] = target.querySelectorAll('p');
12+
13+
assert.equal(window.getComputedStyle(p1).color, 'red');
14+
assert.equal(window.getComputedStyle(p2).color, 'red');
15+
16+
const btn = target.querySelector('button');
17+
console.log(btn);
18+
btn?.click();
19+
await Promise.resolve();
20+
21+
assert.htmlEqual(
22+
target.innerHTML,
23+
`
24+
<p style="color: green;"></p>
25+
<p style="color: green;"></p>
26+
<button></button>
27+
`
28+
);
29+
30+
assert.equal(window.getComputedStyle(p1).color, 'green');
31+
assert.equal(window.getComputedStyle(p2).color, 'green');
32+
}
33+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
let color = "red";
3+
4+
function change(){
5+
color = "green";
6+
}
7+
</script>
8+
9+
<p style:color></p>
10+
11+
{#each [1] as _}
12+
<p style:color></p>
13+
{/each}
14+
15+
<button on:click={change}></button>

0 commit comments

Comments
 (0)