-
-
Notifications
You must be signed in to change notification settings - Fork 225
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
if statement wrongly removed when var declaration inside conditional block #574
Comments
Looks like a bug. You can workaround by declaring the var manually on top. function x(v) {
var w;
if (v) w = true;
if (w) console.log("hello",v);
} This should fix both the issues and produce correct output. Another option is to disable DCE and simplify in the options, this will result in increased o/p though {
deadcode: false,
simplify: false
} |
@vigneshshanmugam Maybe there should be a transform to pull |
yeah that would be an option, we can fix some of the scoping issues with es2015. Will give it a try. |
It seems that when you declare and initialize a variable in a code block that executes conditionally, it is assumed that the variable will have this value also outside of the code block. This is wrong when the conditional code block does not get executed, in which case the variable will be undefined. As a result, further optimizations, like removing if statements, produce wrong results.
E.g., the following code produces a wrong result
It produces this:
Which, when executed, outputs:
while it should only output the second line.
A related problem is with this code:
Which produces:
When executed this throws an exception
Cannot read property 'length' of undefined
.However, unlike the previous example, when disabling the
minify-dead-code-elimination
plugin, this produces correct code.The latter example is what happens for example when processing the firebase scripts (see: https://github.com/Polymer/polymer-cli/issues/701)
The text was updated successfully, but these errors were encountered: