Minify does not properly define variables for while loops:
- Dominant language
- JavaScript
- Stars
- 4.4k
- Forks
- 217
- PR merge metrics
- No merged PRs in 30d
Description
**Describe the bug**
`arr` is undefined in the minified code
**To Reproduce**
```js
function getNDArray(size) {
const arr = [];
while (size) {
size -= 1;
arr.push([]);
}
return arr;
}
module.exports = getNDArray;
```
**Actual Output**
```js
function getNDArray(a) {
for (var b = []; a;)a -= 1, b.push([]);
return arr
}
module.exports = getNDArray;
```
**Expected Output**
```js
function getNDArray(a) {
var b = [];
for (a) a -= 1, b.push([]);
return b
}
module.exports = getNDArray;
```
**Configuration**
"@babel/cli": "7.6.4",
"@babel/core": "7.6.4",
"babel-preset-minify": "0.5.1",
babelrc:
```json5
{
"presets": [
[
"@babel/preset-env",
{
"modules": "commonjs",
}
],
"@babel/preset-react",
["minify", { "builtIns": false }]
]
}
```
Switching the while loop to a for loop fixes the symptoms of the problem.
Contributor guide
Research direction
Reproduce the reported output using the shown JavaScript input, Babel 7.6.4 configuration, and minify preset. Trace the minification handling for the while loop and verify that the completed output returns the declared array variable rather than the undefined name arr.
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
- 25/100