babel-preset-minify fatally breaking for-of loops
- Dominant language
- JavaScript
- Stars
- 4.4k
- Forks
- 217
- PR merge metrics
- No merged PRs in 30d
Description
## Bug Report
**Current Behavior**
babel-preset-minify breaks for-of loops in some cases. It seems like there's two necessary conditions:
1. babel-preset-env detects that the value being looped over is an array and activates its array-iteration fast-path.
2. A conditional with a return statement exists before the for-of loop.
**Input Code**
```js
function Main() {
if (Math.random() > 0.5) {
return;
}
const templates = [];
for (const template of templates) {
template.foo();
}
}
```
**Current Output**
Here's the buggy output prettified:
```js
"use strict";
function Main() {
if (!(0.5 < Math.random()))
for (var template, templates = [], _i = 0; _i < (void 0).length; _i++)
(template = (void 0)[_i]), template.foo();
}
```
Note the presence of `(void 0).length` and `(void 0)[_i]`, which will always cause exceptions. Each case of `(void 0)` should be `templates`.
**Babel Configuration (.babelrc, package.json, cli command)**
```js
{
"presets": [
"@babel/preset-env",
[
"babel-preset-minify",
{
"builtIns": false,
"mangle": false
}
]
]
}
```
**Environment**
- Babel version(s): 7.6.4
- Node v10.16.3, npm 6.10.3
- OS: MacOS 10.14.6
- Monorepo: no
- How you are using Babel: cli, loader
```json
{
"dependencies": {
"@babel/cli": "7.6.4",
"@babel/core": "7.6.4",
"@babel/preset-env": "7.6.3",
"babel-preset-minify": "0.5.1"
}
}
```
**Additional context**
I ran into this issue while using Storybook, which includes babel-preset-minify by default in production builds.
Contributor guide
Assessment
This issue has not been assessed yet.