const when captured doesn't transpile properly
See original GitHub issueThe following code prints 0 to 9:
(function f() {
var x = (0);
while (true) {
if (x < 10) {
const y = x;
setTimeout(function() {return console.log(y);});
x++;
continue;
} else {
return null;
}
}
})();
But after advanced optimization it prints 9 times “9”.
Side note: Changing while (true)
to for (;true;)
fixes it. 😃
Link to reproduce: https://closure-compiler.appspot.com/home#code%3D%252F%252F%2520%253D%253DClosureCompiler%253D%253D%250A%252F%252F%2520%2540output_file_name%2520default.js%250A%252F%252F%2520%2540compilation_level%2520ADVANCED_OPTIMIZATIONS%250A%252F%252F%2520%2540formatting%2520pretty_print%250A%252F%252F%2520%253D%253D%252FClosureCompiler%253D%253D%250A%250A(function%2520f()%2520%257B%250A%2520%2520%2520%2520var%2520x%2520%253D%2520(0)%253B%250A%2520%2520%2520%2520while%2520(true)%2520%257B%250A%2520%2520%2520%2520%2520%2520%2520%2520if%2520(x%2520%253C%252010)%2520%257B%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520const%2520y%2520%253D%2520x%253B%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520setTimeout(function()%2520%257Breturn%2520console.log(y)%253B%257D)%253B%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520x%252B%252B%253B%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520continue%253B%250A%2520%2520%2520%2520%2520%2520%2520%2520%257D%2520else%2520%257B%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520return%2520null%253B%250A%2520%2520%2520%2520%2520%2520%2520%2520%257D%250A%2520%2520%2520%2520%257D%250A%257D)()%253B%250A%250A
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (10 by maintainers)
Top GitHub Comments
Came up with a better fix, which is now in internal review.
I have a fix out for internal review. Basic idea is to do the update as the first statement in the loop instead of the last.