New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wrong scope for var declared in catch block with same name as catch argument #4176
Comments
I see the bug but I must admit I am just too stupid to understand what is going on here. So if I take this example try {
throw new Error();
} catch (e) {
var e = 'value';
console.log(e); // "value" of course
}
console.log(e); // undefined !? It seems that this is equivalent to try {
throw new Error();
} catch (e2) {
var e;
e2 = 'value';
console.log(e2);
}
console.log(e); Is this really what is going on, i.e. the assignment to |
Ha! I thought I understood the issue until I saw your examples. My description in the issue title is clearly wrong. The ES spec would be the authoritative source, but if I had to guess, it appears that the scope of the Let's add a var x = 1;
try {
throw 2;
} catch (x) {
console.log("catch before var decl:", x);
var x = 3;
console.log("catch after var decl:", x);
} finally {
console.log("finally before var decl:", x);
var x = 4;
console.log("finally after var decl:", x);
}
console.log("after:", x);
@alexlamsl Can you help us out here with your take on how |
Correction: it appears that the scope of the |
I asked around on Twitter and I believe the "correct" explanation is:
|
But the effect is what you described. |
In ES6+ terms try { console.log(x) } catch (_) { console.log('not declared') }
(function() {
try { console.log(x) } catch (_) { console.log('not declared') }
x = 'start';
try { console.log(x) } catch (_) { console.log('not declared') }
try {
try { console.log(x) } catch (_) { console.log('not declared') }
x = 'try';
try { console.log(x) } catch (_) { console.log('not declared') }
throw 'throw';
} catch (x) {
try { console.log(x) } catch (_) { console.log('not declared') }
var x = 'catch';
try { console.log(x) } catch (_) { console.log('not declared') }
} finally {
try { console.log(x) } catch (_) { console.log('not declared') }
x = 'finally';
try { console.log(x) } catch (_) { console.log('not declared') }
}
try { console.log(x) } catch (_) { console.log('not declared') }
})();
try { console.log(x) } catch (_) { console.log('not declared') } gives:
So it's equivalent to (minus all the (function() {
var x; // hoisted from `catch`
x = 'start';
{
x = 'try';
}
{
let x = 'throw';
x = 'catch';
}
{
x = 'finally';
}
})(); |
According to the ES5.1 specification:
NewDeclarativeEnvironment is also used by |
Fix at #4178 |
Rollup Version
rollup v2.53.1
Operating System (or Browser)
n/a
Node Version (if applicable)
n/a
Link To Reproduction
https://rollupjs.org/repl/?version=2.53.1&shareable=JTdCJTIybW9kdWxlcyUyMiUzQSU1QiU3QiUyMm5hbWUlMjIlM0ElMjJtYWluLmpzJTIyJTJDJTIyY29kZSUyMiUzQSUyMnZhciUyMGUlMjAlM0QlMjAlNUMlMjJGQUlMMWElNUMlMjIlMkMlMjB4JTIwJTNEJTIwMSUzQiU1Q24lNUNuY29uc29sZS5sb2coZnVuY3Rpb24oKSUyMCU3QiU1Q24lMjAlMjAlMjAlMjB0cnklMjAlN0IlNUNuJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTJGJTJGJTIwZW1wdHklNUNuJTIwJTIwJTIwJTIwJTdEJTIwY2F0Y2glMjAoZSklMjAlN0IlNUNuJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwdmFyJTIwZSUyMCUzRCUyMCU1QyUyMkZBSUwxYiU1QyUyMiUzQiU1Q24lMjAlMjAlMjAlMjAlN0QlNUNuJTVDbiUyMCUyMCUyMCUyMGlmJTIwKGUpJTVDbiUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGNvbnNvbGUubG9nKCU1QyUyMkZBSUwxYyU1QyUyMiklM0IlNUNuJTIwJTIwJTIwJTIwZWxzZSU1Q24lMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBjb25zb2xlLmxvZyghZSUyMCUyNiUyNiUyMCU1QyUyMlBBU1MxJTVDJTIyKSUzQiU1Q24lNUNuJTIwJTIwJTIwJTIwcmV0dXJuJTIweCUyMCUzRiUyMCU1QyUyMkZBSUwyYSU1QyUyMiUyMCUzQSUyMHglMjAlM0QlMjAlNUMlMjJQQVNTMiU1QyUyMiUzQiU1Q24lNUNuJTIwJTIwJTIwJTIwdHJ5JTIwJTdCJTVDbiUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG5vdF9yZWFjaGVkKCklM0IlNUNuJTIwJTIwJTIwJTIwJTdEJTIwY2F0Y2glMjAoeCklMjAlN0IlNUNuJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwdmFyJTIweCUyMCUzRCUyMCU1QyUyMkZBSUwyYiU1QyUyMiUzQiU1Q24lMjAlMjAlMjAlMjAlN0QlNUNuJTdEKCkpJTNCJTIyJTJDJTIyaXNFbnRyeSUyMiUzQXRydWUlN0QlNUQlMkMlMjJvcHRpb25zJTIyJTNBJTdCJTIyZm9ybWF0JTIyJTNBJTIyZXMlMjIlMkMlMjJuYW1lJTIyJTNBJTIybXlCdW5kbGUlMjIlMkMlMjJhbWQlMjIlM0ElN0IlMjJpZCUyMiUzQSUyMiUyMiU3RCUyQyUyMmdsb2JhbHMlMjIlM0ElN0IlN0QlN0QlMkMlMjJleGFtcGxlJTIyJTNBbnVsbCU3RA==
Expected Behaviour
Derived from example using rollup on minified code.
Actual Behaviour
The text was updated successfully, but these errors were encountered: