babel / minify

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

Incorrect minification of nested true if blocks #932

Open markwhitfeld opened 6 years ago

markwhitfeld commented 6 years ago

Describe the bug

Nested if blocks with true conditions are not minified correctly, not even close!

To Reproduce

Minimal code to reproduce the bug

(function () {
    let x = 1;
    console.log(x);
    if (true) {
      let x = 2;
      if (true) {
        let x = 3;
        console.log(x);
        if (true) {
            let x = 4;
            console.log(x);
        }
      }      
    }
    console.log(x);
  })();

Console output:

1
3
4
1

Actual Output

"use strict";
(function() {
  var a = 1;
  console.log(4);
  console.log(a);
})();

Console output:

4
1

Expected Output

I don't really care much for what code would be outputted so, take this output with a pinch of salt. What matters is that the console logs would give the same result.

"use strict";
(function() {
  var a = 1;
  console.log(a);
  console.log(3);
  console.log(4);
  console.log(a);
})();

Configuration

How are you using babel-minify?

I was using the Babel REPL and this bug appeared: https://babeljs.io/en/repl#?babili=true&browsers=&build=&builtIns=false&spec=false&loose=false&code_lz=BQMwrgdgxgLglgewgAmASmQbwFDL8gGwFMZkAPZAXmQEYBuXfKJAZwWIDoCEBzYMtA3zI4IVDABOYIhhzC8xUhWoAmIfNHipMrI2EBIReSrIAzOrz7mENp258BFjWOCTpsvfn2GSx6gBYnb2tbIi5efkFPfQBfTzwY-WRPOOEQ9jD7SPUYtHQ6IA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=script&lineWrap=true&presets=es2015%2Cbabili&prettier=true&targets=&version=6.26.0&envVersion=

msn0 commented 6 years ago

Looks like this patch https://github.com/babel/minify/pull/917 would fix the issue.

markwhitfeld commented 6 years ago

Fantastic! Thank you.