Uglifyjs: Properties not resolved with compress option

Created on 27 Nov 2017  路  5Comments  路  Source: mishoo/UglifyJS

Bug report

Not exactly sure how to describe what is happening, but the code sample and output are hopefully enough to be obvious. The entire 'dimensions' object is dropped and a new property 'a2' is created and used, but clearly it doesn't actually exist and things blow up.

ES5

Uglify version 3.2.0

Note: This did work OK with the 3.1.x line.

JavaScript input

testFunc = function () {

    var dimensions = {
        minX: 5,
        maxX: 6,
        minY: 7,
        maxY: 8
    };
    var scale = 1;

    var d = {
        x: (dimensions.maxX + dimensions.minX) / 2,
        y: (dimensions.maxY + dimensions.minY) / 2
    };

    var dcx = (d.x * scale);
    var dcy = (d.y * scale);

    var dc = [dcx, dcy];
    return {
        position: dc
    };
};

The uglifyjs CLI command executed or minify() options used.

uglifyjs file.js -b -m -c > testout.js

JavaScript output or error produced.

testFunc = function() {
    return {
        position: [ 1 * ((a2.maxX + a2.minX) / 2), 1 * ((a2.maxY + a2.minY) / 2) ]
    };
};
bug

Most helpful comment

Disable the compress option hoist_props as a temporary workaround:

$ cat t2519.js | bin/uglifyjs -m -c hoist_props=false -b | node
{ position: [ 5.5, 7.5 ] }
$ cat t2519.js | bin/uglifyjs -m -c hoist_props=false -b
function testFunc() {
    var n = {
        minX: 5,
        maxX: 6,
        minY: 7,
        maxY: 8
    }, m = {
        x: (n.maxX + n.minX) / 2,
        y: (n.maxY + n.minY) / 2
    };
    return {
        position: [ 1 * m.x, 1 * m.y ]
    };
}

console.log(testFunc());

All 5 comments

Confirmed bug. Thanks for the report.

$ cat t2519.js 
function testFunc() {
    var dimensions = {
        minX: 5,
        maxX: 6,
        minY: 7,
        maxY: 8
    };
    var scale = 1;

    var d = {
        x: (dimensions.maxX + dimensions.minX) / 2,
        y: (dimensions.maxY + dimensions.minY) / 2
    };

    var dcx = (d.x * scale);
    var dcy = (d.y * scale);

    var dc = [dcx, dcy];
    return {
        position: dc
    };
}
console.log(testFunc());

```
$ cat t2519.js | node
{ position: [ 5.5, 7.5 ] }


$ cat t2519.js | bin/uglifyjs -bc | node
position: [ 1 * ((dimensions.maxX + dimensions.minX) / 2), 1 * ((dimensions.maxY + dimensions.minY) / 2) ]
^
ReferenceError: dimensions is not defined

```js
$ cat t2519.js | bin/uglifyjs -bc
function testFunc() {
    return {
        position: [ 1 * ((dimensions.maxX + dimensions.minX) / 2), 1 * ((dimensions.maxY + dimensions.minY) / 2) ]
    };
}

console.log(testFunc());

Disable the compress option hoist_props as a temporary workaround:

$ cat t2519.js | bin/uglifyjs -m -c hoist_props=false -b | node
{ position: [ 5.5, 7.5 ] }
$ cat t2519.js | bin/uglifyjs -m -c hoist_props=false -b
function testFunc() {
    var n = {
        minX: 5,
        maxX: 6,
        minY: 7,
        maxY: 8
    }, m = {
        x: (n.maxX + n.minX) / 2,
        y: (n.maxY + n.minY) / 2
    };
    return {
        position: [ 1 * m.x, 1 * m.y ]
    };
}

console.log(testFunc());

The fix will be in the next release - 3.2.1.

@kzc is there a timeline available for when this patch will be released? We've got a production release on monday and either I'm going to be disabling the hoist_props option for all our apps, or if this patch is released before that, I'm just going to wait it out.

Releases are generally made on the weekend. Either disable the option or lock down a specific version of uglify in the meantime.

Was this page helpful?
0 / 5 - 0 ratings