babel / babel/minify

Incorrect minification of nested true if blocks

Open
#932 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
4.4k
Forks
217
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**

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

**To Reproduce**

Minimal code to reproduce the bug

```js
(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**

```js
"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.

```js
"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=

Contributor guide

Open the contributing guide

Research direction

Start with the minimal JavaScript example in the Babel REPL configuration linked in the issue, using the reported ES2015 and babili presets. Trace the minification of nested if blocks with true conditions and compare the transformed console sequence with the original. Done means the minified output preserves the 1, 3, 4, 1 logging order.

Written by the indexing model from the issue text.

Assessment

Tech stack
babel, javascript
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.