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 transformation on React project with both simplify and dead code elimination enabled #385
Comments
I'm also seeing something along these lines as well, while trying to minify https://github.com/facebook/react/blob/master/src/isomorphic/modern/class/ReactComponent.js in the context of a webpack module. It ends up causing the error:
for me. I was able to get a more minimal repro of: Inputfunction ReactComponent() {
}
ReactComponent.prototype.isReactComponent = {};
if (false) {
const anyVar = 1;
} Expectedfunction ReactComponent(){}ReactComponent.prototype.isReactComponent={}; Actual
It appears to work correctly in the REPL at the moment, but not on babel-preset-babili@0.0.10 and a babel version of |
Strange that |
The problem seems to be that dead code elimination ignores the side effects of sequence expressions. function ReactComponent() {
}
if (ReactComponent.prototype.isReactComponent = {}, false) {
const anyVar = 1;
} which dead code elimination undestands as: if (false) {
const anyVar = 1;
} I'm not terribly familiar with the Babel AST but in light of dead code elimination this transformation only seems safe to me when the node being moved into the var prev = path.getSibling(path.key - 1);
if (!prev.isExpressionStatement()) {
return;
} with var prev = path.getSibling(path.key - 1);
if (!prev.isExpressionStatement() || !prev.isPure()) {
return;
} solves the minimal repro case for me (couldn't test it on my project yet). |
Yes! Exactly. The order of the transformations again. 0.0.9 didn't have DCE in
|
+ (Fix #385) + Add isPure check to IfStatement visitor in DCE + Move IfStatement visitor from Single Pass program.exit to DCEPlugin.visitor - so that it executes for all IfStatements as babel traverse happens and NOT during programPath.traverse. This eliminates the need for a second pass for things like the one mentioned in #385.
+ (Fix #385) + Add isPure check to IfStatement visitor in DCE + Move IfStatement visitor from Single Pass program.exit to DCEPlugin.visitor - so that it executes for all IfStatements as babel traverse happens and NOT during programPath.traverse. This eliminates the need for a second pass for things like the one mentioned in #385.
* DCE: Deopt impure statements in If.test + (Fix #385) + Add isPure check to IfStatement visitor in DCE + Move IfStatement visitor from Single Pass program.exit to DCEPlugin.visitor - so that it executes for all IfStatements as babel traverse happens and NOT during programPath.traverse. This eliminates the need for a second pass for things like the one mentioned in #385. * Add tests * Moar optimizations for side-effecty if statements * Add dependency jsesc for constant-folding
I couldn't make a minimal repro, but compiling my project with these two plugins enabled leads to the following error message (it's worth mentioning that babili is run on single fairly large file created by Webpack):
The issue doesn't manifest with either of the plugins disabled. I narrowed the bug to
createPrevExpressionEater("if")
insimplify
whose result is removed by theif (evaluateTest === false)
block in theIfStatement
part ofdead-code-elimination
.If there's any diagnostic I can run to be more helpful I'd be happy to do so.
The text was updated successfully, but these errors were encountered: