Skip to content

Commit 5975eb5

Browse files
authored
Merge pull request #3101 from sveltejs/gh-3044
always bail out of hoisting on encountering local state
2 parents fc32147 + fc710a9 commit 5975eb5

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/compiler/compile/Component.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,8 @@ export default class Component {
10401040

10411041
walk(fn_declaration, {
10421042
enter(node, parent) {
1043+
if (!hoistable) return this.skip();
1044+
10431045
if (map.has(node)) {
10441046
scope = map.get(node);
10451047
}
@@ -1048,7 +1050,7 @@ export default class Component {
10481050
const { name } = flatten_reference(node);
10491051
const owner = scope.find_owner(name);
10501052

1051-
if (node.type === 'Identifier' && injected_reactive_declaration_vars.has(name)) {
1053+
if (injected_reactive_declaration_vars.has(name)) {
10521054
hoistable = false;
10531055
} else if (name[0] === '$' && !owner) {
10541056
hoistable = false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default {
2+
html: `
3+
<button>Click me</button>
4+
`,
5+
6+
async test({ assert, target, window }) {
7+
const event = new window.MouseEvent('click');
8+
const button = target.querySelector('button');
9+
10+
await button.dispatchEvent(event);
11+
assert.htmlEqual(target.innerHTML, `
12+
<button>A,B,C</button>
13+
`);
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
let array = ['a', 'b', 'c'];
3+
$: uppercase = array.map(str => str.toUpperCase());
4+
5+
function onClick() {
6+
this.innerHTML = uppercase.join(',');
7+
}
8+
</script>
9+
10+
<button on:click={onClick}>Click me</button>

0 commit comments

Comments
 (0)