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 incorrect when var inside if #1021

Open
edemaine opened this issue Jan 25, 2022 · 0 comments
Open

babel-plugin-minify-dead-code incorrect when var inside if #1021

edemaine opened this issue Jan 25, 2022 · 0 comments

Comments

@edemaine
Copy link

Describe the bug

TLDR: babel-plugin-minify-dead-code seems to assume that var initializers are executed, but they're executed only if reached.

Longer version: var x = y does two things: it declares x at the function scope (no matter what), and sets x = y when that line gets reached. As a result, if (condition) var x = y declares x, but x will remain undefined if condition is false. babel-plugin-minify-dead-code seems to assume that the assignment happens though, and generates incorrect code.

To Reproduce

Demo link (please uncheck "Bug fixes" after loading)

function f(condition) {
  if (condition) {
    var args = ', b';
  }
  return `a${args != null ? args : ''}`;
}

Actual Output

function f(condition) {
  if (condition) {
    var args = ', b';
  }

  return "a".concat(args);
}

Expected Output

function f(condition) {
  if (condition) {
    var args = ', b';
  }

  return "a".concat(args != null ? args : '');
}

Configuration

How are you using babel-minify?

babel Node API

babel-minify version: 0.5.1

babel version : 7.15.5

babel-minify-config:

{
  mange: false,
  evaluate: false,
  removeUndefined: false
}

babelrc:

{
  presets: [
   ['@babel/env', {modules: false}],
   ['minify', {mangle: false, evaluate: false, removeUndefined: false}]
  ]
}

Additional context

This arose when minifying (a PR for) the CoffeeScript compiler.

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

No branches or pull requests

1 participant