Skip to content
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

babel-plugin-minify-dead-code-elimination SequenceExpression use case #373

Closed
jtenner opened this issue Jan 13, 2017 · 9 comments
Closed

Comments

@jtenner
Copy link

jtenner commented Jan 13, 2017

SequenceExpression in ExpressionStatement containing UnaryExpression(void 0) does not remove dead code.

I realize this is a small use case, but if an ExpressionStatement contains a SequenceExpression with void 0 it can be removed with no worry of side-effects.

  a, b, c, d, void 0;

should be:

  a, b, c, d;

Thanks for reading!

-Josh

@jtenner jtenner changed the title babel-plugin-minify-dead-code-elimination SequenceExpression problem babel-plugin-minify-dead-code-elimination SequenceExpression use case Jan 13, 2017
@kangax
Copy link
Member

kangax commented Jan 13, 2017

Thanks for reporting, definitely a valid use case.

@jtenner
Copy link
Author

jtenner commented Jan 16, 2017

Thanks for replying! I don't suppose I can help?

@boopathi
Copy link
Member

It's an unsafe transformation -

function f1() {
  return 1, 2, void 0;
}

function f2() {
  return 1, 2;
}

console.log(f1(), f2()); // will print undefined, 2

@jtenner
Copy link
Author

jtenner commented Jan 16, 2017

@boopathi when the parent is an ExpressionStatement it is completely safe.

Also when the SequenceExpression contains void 0 and it's not the last item it is safe too.

//Any other statement with an expression that contains a sequence expression
let g = (void 0, void 0, 1);
//becomes
let g = (1);

//ExpressionStatement (SequenceExpression)
void 0, 1, 2, 3, void 0;
//becomes
1, 2, 3;

//taking your example
function f1() {
  return 1, void 0, 2, void 0;
}

function f2() {
  return 1, 2, void 0;
}

console.log(f1(), f2()); // will print 2

@jtenner
Copy link
Author

jtenner commented Jan 16, 2017

Example visitor that might be useful.

Check out this example here. It should be correct.

For some reason the transformation yields two sequence expressions (and it looks like a babel bug!) but the general idea here is that some void 0 operations are definitely ignored and we can easily determine if it is safe to ignore them.

@kangax
Copy link
Member

kangax commented Jan 16, 2017

This is similar to #197 where we remove undefined only when it's in a "safe" context.

@jtenner
Copy link
Author

jtenner commented Jan 23, 2017

Thanks guys! This is really awesome.

@jtenner
Copy link
Author

jtenner commented Jan 24, 2017

@kangax do you detect to see if undefined is overwritten? It's possible it might be unsafe, unlike void 0.

@kangax
Copy link
Member

kangax commented Jan 24, 2017

You're right, it's unsafe. I looked into fixing it in Babili, then realized it has to do with Babel's path.evaluate() incorrectly thinking that undefined is undefined. Filed a bug — babel/babel#5204

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants