Skip to content

Commit 24619da

Browse files
authored
fix corner cases in collapse_vars & unused (#4807)
fixes #4806
1 parent b89cc84 commit 24619da

File tree

3 files changed

+121
-13
lines changed

3 files changed

+121
-13
lines changed

lib/compress.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2701,8 +2701,8 @@ merge(Compressor.prototype, {
27012701

27022702
function patch_sequence(node) {
27032703
if (node instanceof AST_Sequence) switch (node.expressions.length) {
2704-
case 0: return null;
2705-
case 1: return node.expressions[0];
2704+
case 0: return null;
2705+
case 1: return maintain_this_binding(compressor, this.parent(), node, node.expressions[0]);
27062706
}
27072707
}
27082708

@@ -6386,14 +6386,13 @@ merge(Compressor.prototype, {
63866386
scope = save_scope;
63876387
}
63886388
}, function(node, in_list) {
6389-
if (node instanceof AST_BlockStatement) {
6390-
return trim_block(node, tt.parent(), in_list);
6391-
} else if (node instanceof AST_For) {
6392-
// Certain combination of unused name + side effect leads to invalid AST:
6393-
// https://github.com/mishoo/UglifyJS/issues/44
6394-
// https://github.com/mishoo/UglifyJS/issues/1838
6395-
// https://github.com/mishoo/UglifyJS/issues/3371
6396-
// We fix it at this stage by moving the `var` outside the `for`.
6389+
if (node instanceof AST_BlockStatement) return trim_block(node, tt.parent(), in_list);
6390+
// Certain combination of unused name + side effect leads to invalid AST:
6391+
// https://github.com/mishoo/UglifyJS/issues/44
6392+
// https://github.com/mishoo/UglifyJS/issues/1838
6393+
// https://github.com/mishoo/UglifyJS/issues/3371
6394+
// We fix it at this stage by moving the `var` outside the `for`.
6395+
if (node instanceof AST_For) {
63976396
var block;
63986397
if (node.init instanceof AST_BlockStatement) {
63996398
block = node.init;
@@ -6414,7 +6413,8 @@ merge(Compressor.prototype, {
64146413
node.init = null;
64156414
}
64166415
return !block ? node : in_list ? List.splice(block.body) : block;
6417-
} else if (node instanceof AST_ForIn) {
6416+
}
6417+
if (node instanceof AST_ForIn) {
64186418
if (!drop_vars || !compressor.option("loops")) return;
64196419
if (!is_empty(node.body)) return;
64206420
var sym = get_init_symbol(node);
@@ -6435,8 +6435,10 @@ merge(Compressor.prototype, {
64356435
body.push(node.init);
64366436
}
64376437
return insert_statements(body, node, in_list);
6438-
} else if (node instanceof AST_Sequence) {
6439-
if (node.expressions.length == 1) return node.expressions[0];
6438+
}
6439+
if (node instanceof AST_Sequence) {
6440+
if (node.expressions.length > 1) return;
6441+
return maintain_this_binding(compressor, tt.parent(), node, node.expressions[0]);
64406442
}
64416443
});
64426444
tt.push(compressor.parent());

test/compress/collapse_vars.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8857,3 +8857,26 @@ dot_non_local: {
88578857
}
88588858
expect_stdout: "42"
88598859
}
8860+
8861+
issue_4806: {
8862+
options = {
8863+
collapse_vars: true,
8864+
}
8865+
input: {
8866+
var a, o = {
8867+
f: function() {
8868+
console.log(this === o ? "FAIL" : "PASS");
8869+
},
8870+
};
8871+
(a = 42, o.f)(42);
8872+
}
8873+
expect: {
8874+
var a, o = {
8875+
f: function() {
8876+
console.log(this === o ? "FAIL" : "PASS");
8877+
},
8878+
};
8879+
(0, o.f)(a = 42);
8880+
}
8881+
expect_stdout: "PASS"
8882+
}

test/compress/drop-unused.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3274,3 +3274,86 @@ issue_4662: {
32743274
}
32753275
expect_stdout: "1 1"
32763276
}
3277+
3278+
issue_4806_1: {
3279+
options = {
3280+
evaluate: true,
3281+
reduce_vars: true,
3282+
toplevel: true,
3283+
unused: true,
3284+
}
3285+
input: {
3286+
O = {
3287+
f: function() {
3288+
console.log(this === O ? "FAIL" : "PASS");
3289+
},
3290+
};
3291+
var a;
3292+
(a = 42, O.f)();
3293+
a;
3294+
}
3295+
expect: {
3296+
O = {
3297+
f: function() {
3298+
console.log(this === O ? "FAIL" : "PASS");
3299+
},
3300+
};
3301+
(0, O.f)();
3302+
42;
3303+
}
3304+
expect_stdout: "PASS"
3305+
}
3306+
3307+
issue_4806_2: {
3308+
options = {
3309+
sequences: true,
3310+
toplevel: true,
3311+
unused: true,
3312+
}
3313+
input: {
3314+
O = {
3315+
f: function() {
3316+
console.log(this === O ? "FAIL" : "PASS");
3317+
},
3318+
};
3319+
var a;
3320+
(a = 42, O.f)();
3321+
a;
3322+
}
3323+
expect: {
3324+
O = {
3325+
f: function() {
3326+
console.log(this === O ? "FAIL" : "PASS");
3327+
},
3328+
},
3329+
(0, O.f)();
3330+
}
3331+
expect_stdout: "PASS"
3332+
}
3333+
3334+
issue_4806_3: {
3335+
options = {
3336+
side_effects: true,
3337+
toplevel: true,
3338+
unused: true,
3339+
}
3340+
input: {
3341+
O = {
3342+
f: function() {
3343+
console.log(this === O ? "FAIL" : "PASS");
3344+
},
3345+
};
3346+
var a;
3347+
(a = 42, O.f)();
3348+
a;
3349+
}
3350+
expect: {
3351+
O = {
3352+
f: function() {
3353+
console.log(this === O ? "FAIL" : "PASS");
3354+
},
3355+
};
3356+
(0, O.f)();
3357+
}
3358+
expect_stdout: "PASS"
3359+
}

0 commit comments

Comments
 (0)