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
Thanks for reporting, definitely a valid use case.
Thanks for replying! I don't suppose I can help?
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
@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
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.
This is similar to https://github.com/babel/babili/pull/197 where we remove undefined only when it's in a "safe" context.
Thanks guys! This is really awesome.
@kangax do you detect to see if undefined is overwritten? It's possible it might be unsafe, unlike void 0.
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 — https://github.com/babel/babel/issues/5204