-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Incorrect mangling of local variables with try {} catch(e) {} #409
Comments
This is still happening. |
Is this all the code you're uglifying? I don't see anything wrong with it, or with the uglified result: try{var part=file.slice(0,2);reader.readAsArrayBuffer(part)}catch(e){onUnreadable()} |
No. It's part of a pretty big codebase. |
I suspect this is because of UglifyJS's default to be compliant with IE8 bugs and leaks the Anyhow, a complete repro case would be welcome. |
I'm already using --screw-ie8 |
And I can't really reproduce it right now. The bug comes and goes as I change code somewhere else. I suspect that for some reason the compressor fails to honour all possible local variables when creating the shorter "aliases". No idea. |
Could be a serious bug then, I'd appreciate it if you can post some minimal code which demonstrates the issue. |
I'll post it here once I manage to condense it to a minimal example. |
This looks like something I'm seeing as well, though curiously enough only happening on IE8. |
Minimal example: bad = function (e) {
return function(error) {
try {
e();
} catch (e) {
error(e);
}
};
}; In version 2.4.5, with or without --screw-ie8, the compressor would eliminate the As of 2.4.6 this behaviour is fixed with --screw-ie8. As of 2.5.0 this case still mangles incorrectly without --screw-ie8 (though the compressor no longer considers the argument unused). |
Just showing the output for ticket viewers. original
with uglifyjs v2.50 -m --screw-ie8 -b
with uglifyjs v2.50 -m -b
|
Thanks. I'll take a look at it right now. |
Relevant bit: https://github.com/mishoo/UglifyJS2/blob/593677d/lib/scope.js#L98-L106 I'm going to have to test this carefully to ensure no regression springs up. |
Yeah, the uglify scope code is pretty tricky/subtle - due to the complexity of ecmascript no doubt. The test case in question tests my limited javascript knowledge...
Regarding the variable
by the ecmascript standard is it supposed to replace the |
@rvanvelzen Do we try to make a fix for this issue or just enable the screw_ie8 option by default? |
I'd rather do both. I don't feel like we should ship a feature that may break code. If we are to enable |
@rvanvelzen Have you made any progress on this issue? I don't know how ECMAscript is supposed to behave regarding a catch var shadowing a var of same name in outer scope. I'm surprised that such code is common in the wild. |
There are more things under the I'd like to stay on the safe side here, so this probably asks for some additional flag. I would, however, turn on by default interpreting the |
The catch parameter is only available inside the catch block. Note that other variables declared inside the catch block are available outside. IE8 does this incorrectly, which is why it's so hard to fix. Relevant bit of the spec: http://es5.github.io/#x12.14 |
I've also noticed this. It resulted in a button in our product mysteriously not working despite the code looking correct. It seems like it is definitely the same issue, but I'll post my version anyway, just in case it's different in some way that I don't understand. Thankfully there is a workaround, but it is easy to slip up and accidentally trigger this behaviour. In my case it was compiled using Babel:
When this is compiled to ES5, this becomes something like (it's a little simplified):
The
|
Maybe we should just warn on shadowed names in catch blocks. It should be obvious (by now) that it's a Bad Thing™ to do. |
It may be obvious after reading this thread, sure. But I think this behaviour is very unexpected beforehand; it certainly took me by surprise. I think a warning would probably suffice for cases like mine, as long as the warning gives some sort of idea of what problem might result from it. If it were just something like "warning: catch block variable shadows variable from enclosing scope", that wouldn't give me any idea that it's going to compile valid code into something that doesn't work at all. |
We were just bitten by this at GitHub. Minimal example: function a(b) {
try {
} catch (undefined) {}
return b === undefined
} I would expect |
No doubt it's a bug. |
Should point out that
This is the desired output:
Notice that
Notice that
|
Sure, that's what we did when we discovered the bug. The reason we have this idiom in the codebase is that CoffeeScript generates it: $ echo try a | coffee -bcs
// Generated by CoffeeScript 1.10.0
try {
a;
} catch (undefined) {} I can confirm that |
See fix: #1179 Don't support ie8 by default. |
This issue can be closed. |
I had a few lines in my codebase that looked like this:
And "e is not a function" got thrown at "onUnreadable();".
I temporarily fixed it by rewriting my code like this:
The issue should be obvious here.
I would love you guys if you fixed this, because I'm kinda worried that those bugs exists elsewhere in my code...
The text was updated successfully, but these errors were encountered: