question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

const when captured doesn't transpile properly

See original GitHub issue

The 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:closed
  • Created 6 years ago
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
brad4dcommented, Feb 8, 2018

Came up with a better fix, which is now in internal review.

0reactions
brad4dcommented, Jan 26, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

const aren't being transpiled when they have a function ...
Bug Report Current Behavior const aren't being transpiled in the following situation: they are defined on a dependency file, in node_modules ...
Read more >
Why does Babel not properly handle const? - Stack Overflow
Ergo Babel does properly handle const. The reason why your code wasn't transpiled to throw an error is because of part 1.
Read more >
babel/plugin-transform-typescript
This option differs from TypeScript's --isolatedModules behavior, which ignores the const modifier and compiles them as normal enums, and aligns Babel's ...
Read more >
Error - JavaScript - MDN Web Docs - Mozilla
Warning: Builtin subclassing cannot be reliably transpiled to pre-ES6 code, because there's no way to construct the base class with a ...
Read more >
I turned JS into a compiled language (for fun and Wasm)
The mutable keyword is necessary in our context as C++ closures capture variables as const by default, meaning they can't be modified. In...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found