babel / minify

:scissors: An ES6+ aware minifier based on the Babel toolchain (beta)
https://babeljs.io/repl
MIT License
4.39k stars 225 forks source link

Incorrect output in minify-dead-code-elimination #898

Open jimdebeer opened 6 years ago

jimdebeer commented 6 years ago

Describe the bug

A strange edge case code does not get eliminated correctly.

To Reproduce

Minimal code to reproduce the bug

function b() {}

function a() {
  if ('production' === 'production') {
    return
  }

  var c = b()

  if (!c) {
    return
  }
}

Actual Output

If there is no Error thrown,

function b() {}

function a() {

  var c = b();
}

Expected Output

function b() {}

function a() {

}

Configuration

How are you using babel-minify?

babel dependency

babel version : 7.0.0-beta.3

Used stand alone using the Node Api + babel-core

{
  plugins: [],
  presets: []
}

Additional context

See it in action in the babel-repl

Encountered this construction here

Tried to recreate it as minimal as possible using only babel-core and the dead-code plugin

Cyp commented 5 years ago

Slightly simpler testcase:

function a() {
  return;

  foo();
  let c = bar();
  baz(c);
}
//With deadcode:
"use strict";function a(){var c=bar()}

The return statement and all code after it is removed, except for the variable declaration which fails to be removed when it should.

Opposite testcase:

function a() {
  q = 42;
  ++q;
  return;

  {
    var q;
  }
}
//With deadcode:
"use strict";function a(){q=42;++q}

The variable declaration here fails to be preserved, even though it should be in this case.

msn0 commented 5 years ago

Hi I'm going to try to look at this. Thanks!