babel / babel/minify

simplify pulls VariableDeclaration into loop, then mangle fails to mangle same variable after loop

Open
#999 1 comment 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**

Minification produces output where a variable is only minified sometimes within a function. Thanks for your help!

**To Reproduce**

Minimal code to reproduce the bug

```js
function foo() {
let varName = [];
while (false) {}
varName;
}
```

**Actual Output**

```js
"use strict";
function foo(){
for(var a=[];false;);
varName
}
```

**Expected Output**

I'm not sure if it's expected to pull the `varName` variable into the loop in this circumstance, so the expected output is one of the following:

```js
"use strict";
function foo(){
for(var a=[];false;);
a
}
```

or

```js
"use strict";
function foo(){
var a=[];
for(;false;);
a
}
```

**Configuration**

Turning off all minify plugins **except** for `mangle` and `simplify` (bug requires both).

```
npx babel ./src --out-dir ./lib
```

babel-minify version: `0.5.1`

babel core : `7.11.6`

babel-minify-config:

```json5
{
"mangle": true,
"simplify": true,
"evaluate": false,
"replace": false,
"booleans": false,
"builtIns": false,
"consecutiveAdds": false,
"deadcode": false,
"flipComparisons": false,
"guards": false,
"infinity": false,
"memberExpressions": false,
"mergeVars": false,
"numericLiterals": false,
"propertyLiterals": false,
"regexpConstructors": false,
"removeConsole": false,
"removeDebugger": false,
"removeUndefined": false,
"simplifyComparisons": false,
"typeConstructors": false,
"undefinedToVoid": false
}
```

babelrc:

```json5
module.exports = {
presets: [
'@babel/preset-env',
['minify', {
"mangle": true,
"simplify": true,
"evaluate": false,
"replace": false,
"booleans": false,
"builtIns": false,
"consecutiveAdds": false,
"deadcode": false,
"flipComparisons": false,
"guards": false,
"infinity": false,
"memberExpressions": false,
"mergeVars": false,
"numericLiterals": false,
"propertyLiterals": false,
"regexpConstructors": false,
"removeConsole": false,
"removeDebugger": false,
"removeUndefined": false,
"simplifyComparisons": false,
"typeConstructors": false,
"undefinedToVoid": false
}]
],
plugins: [
],
}
```

**Possible solution**

Not sure. But it appears the logic in [this region](https://github.com/babel/minify/blob/1ad7838116ec34621d39bb1b4a985e7601eab659/packages/babel-plugin-minify-simplify/src/index.js#L335) may need to be improved, when it decides if a variable is referenced outside the loop.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.