butternut icon indicating copy to clipboard operation
butternut copied to clipboard

Remove side-effect free statements

Open boopathi opened this issue 7 years ago • 2 comments

Input:

function foo() {
  a; 5; 6;
}

expected:

function foo() {}

boopathi avatar May 11 '17 18:05 boopathi

Thinking about this, the low-hanging fruit would be any ExpressionStatement nodes whose expression is anything except a CallExpression, NewExpression, AssignmentExpression or UpdateExpression... does that sound about right?

There's also assignments to unused variables etc, but that's a little trickier, wasn't planning to tackle that just yet 😀

Rich-Harris avatar May 11 '17 19:05 Rich-Harris

Also,

  • o.foo - getters
  • instanceof - Symbol.hasInstance
  • The tricky ones are a + "" a.toString is called. So all of arithmetic operators are to be treated as impure.

Edit:

  • obj.valueOf

boopathi avatar May 11 '17 19:05 boopathi