Minify breaks code
- Dominant language
- JavaScript
- Stars
- 4.4k
- Forks
- 217
- PR merge metrics
- No merged PRs in 30d
Description
#### Input Code
Requires jQuery $.
```js
(function(){
// Environment code
var opt = {
containment: "#id",
grid: [10, 10]
};
var $elem = $("#elem");
$.fn.rect = function () {
var offset = this.offset();
var width = this.outerWidth();
var height = this.outerHeight();
return {
top: offset.top,
left: offset.left,
right: offset.left + width,
bottom: offset.top + height,
width: width,
height: height
};
};
var newPoint = {
top: 0,
left: 0
};
var elemRect = {
width: 100,
height: 100
};
// Actual code
if (opt.containment) {
let cont;
if (opt.containment === "parent")
cont = $elem.parent();
else
cont = $(opt.containment);
if (cont.length !== 0) {
let contRect = cont.rect();
let stepX = opt.grid ? opt.grid[0] : 1;
let stepY = opt.grid ? opt.grid[1] : 1;
while (newPoint.left < contRect.left) newPoint.left += stepX;
while (newPoint.left + elemRect.width > contRect.right) newPoint.left -= stepX;
while (newPoint.top < contRect.top) newPoint.top += stepY;
while (newPoint.top + elemRect.height > contRect.bottom) newPoint.top -= stepY;
}
}
})();
```
#### Actual Output
```js
"use strict";(function(){var a={containment:"#id",grid:[10,10]},b=$("#elem");$.fn.rect=function(){var a=this.offset(),b=this.outerWidth(),c=this.outerHeight();return{top:a.top,left:a.left,right:a.left+b,bottom:a.top+c,width:b,height:c}};var c={top:0,left:0},d={width:100,height:100};if(a.containment){var e;if(e="parent"===a.containment?b.parent():$(a.containment),0!==e.length){for(var f=e.rect(),g=a.grid?a.grid[0]:1,h=a.grid?a.grid[1]:1;c.leftcontRect.right;)c.left-=stepX;for(;c.topcontRect.bottom;)c.top-=stepY}}})();
```
#### Expected Output
The problem is with `contRect`. It's a local variable and should be preserved, under which name ever. Instead, it is resolved the first call and then left as-is for the other three calls. That symbol won't be defined, and the browser complains about it. The resulting code doesn't work anymore.
#### Details
Command invocation:
`babel "test.js" --out-file "test.min.js" --presets="%AppData%\npm\node_modules\babel-preset-env","%AppData%\npm\node_modules\babel-preset-minify"`
babel version 6.26.0, babel-minify version 0.3.0, Node.js version 8.9.4, Windows 10
uglify-es handles this code just fine.
Contributor guide
Assessment
This issue has not been assessed yet.