fix for loop scoping

pull/1839/head
Rich Harris 7 years ago
parent 9dfc89ecd7
commit f08f40739c

@ -377,6 +377,7 @@ export default class Expression {
code.appendLeft(node.end, `; ${insert}`);
}
component.has_reactive_assignments = true;
pending_assignments = new Set();
}
}

@ -30,7 +30,7 @@ export function createScopes(expression: Node) {
scope.declarations.set(name, node);
});
});
} else if (/For(?:In|Of)Statement/.test(node.type)) {
} else if (/For(?:In|Of)?Statement/.test(node.type)) {
scope = new Scope(scope, true);
map.set(node, scope);
} else if (node.type === 'BlockStatement') {

@ -0,0 +1,17 @@
export default {
html: `
<button>foo</button>
<p>x: 0</p>
`,
async test({ assert, component, target, window }) {
const buttons = target.querySelectorAll('button');
const click = new window.MouseEvent('click');
await buttons[0].dispatchEvent(click);
assert.htmlEqual(target.innerHTML, `
<button>foo</button>
<p>x: 42</p>
`);
}
};

@ -0,0 +1,14 @@
<script>
let x = 0;
function foo() {
(() => {
for (let x = 0; x < 10; x++) {}
x = 42;
})();
}
</script>
<button on:click={foo}>foo</button>
<p>x: {x}</p>

@ -0,0 +1,21 @@
export default {
html: `
<button>foo</button>
<button>bar</button>
<p>x: 0</p>
`,
async test({ assert, component, target, window }) {
const buttons = target.querySelectorAll('button');
const click = new window.MouseEvent('click');
await buttons[0].dispatchEvent(click);
assert.htmlEqual(target.innerHTML, `
<button>foo</button>
<button>bar</button>
<p>x: 42</p>
`);
}
};

@ -0,0 +1,12 @@
<script>
let x = 0;
</script>
<button on:click="{() => {
(() => {
for (let x = 0; x < 10; x++) {}
x = 42;
})();
}}">foo</button>
<p>x: {x}</p>
Loading…
Cancel
Save