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

babel-plugin-minify-dead-code-elimination fails to remove variable declaration after return statement #1020

Open fengzilong opened 2 years ago

fengzilong commented 2 years ago

Describe the bug

As described in title

To Reproduce

Minimal code to reproduce the bug

function foo() {
  if ( true ) {
    return
  }

  const timer = setInterval(
    () => { console.log( timer ) }, 1000
  )
}

Actual Output

there is no Error thrown

function foo() {
  var timer = setInterval(function () {
    console.log(timer);
  }, 1000);
}

Expected Output

function foo() {}

Possible solution

If the timer reference doesn't appeared before return in current scope, it can be safely removed

Additional context

Here is the Babel REPL link to reproduce