enhance collapse_vars (#4735)
This commit is contained in:
@@ -2068,8 +2068,9 @@ merge(Compressor.prototype, {
|
|||||||
if (node instanceof AST_ObjectIdentity) return symbol_in_lvalues(node, parent);
|
if (node instanceof AST_ObjectIdentity) return symbol_in_lvalues(node, parent);
|
||||||
if (node instanceof AST_PropAccess) {
|
if (node instanceof AST_PropAccess) {
|
||||||
var exp = node.expression;
|
var exp = node.expression;
|
||||||
return side_effects || !value_def && exp.may_throw_on_access(compressor)
|
return side_effects
|
||||||
|| exp instanceof AST_SymbolRef && is_arguments(exp.definition());
|
|| exp instanceof AST_SymbolRef && is_arguments(exp.definition())
|
||||||
|
|| !value_def && (in_try || !lhs_local) && exp.may_throw_on_access(compressor);
|
||||||
}
|
}
|
||||||
if (node instanceof AST_Spread) return true;
|
if (node instanceof AST_Spread) return true;
|
||||||
if (node instanceof AST_SymbolRef) {
|
if (node instanceof AST_SymbolRef) {
|
||||||
|
|||||||
@@ -620,11 +620,10 @@ collapse_rhs: {
|
|||||||
expect: {
|
expect: {
|
||||||
"use strict";
|
"use strict";
|
||||||
var a = "FAIL";
|
var a = "FAIL";
|
||||||
a = "PASS";
|
|
||||||
class A {
|
class A {
|
||||||
p = "PASS";
|
p = "PASS";
|
||||||
}
|
}
|
||||||
console.log(a);
|
console.log(a = "PASS");
|
||||||
}
|
}
|
||||||
expect_stdout: "PASS"
|
expect_stdout: "PASS"
|
||||||
node_version: ">=12"
|
node_version: ">=12"
|
||||||
|
|||||||
@@ -958,8 +958,7 @@ collapse_vars_misc: {
|
|||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
function f0(o, a, h) {
|
function f0(o, a, h) {
|
||||||
var b = 3 - a;
|
return o.run(3 - a)[7] = h;
|
||||||
return o.run(b)[7] = h;
|
|
||||||
}
|
}
|
||||||
function f1(x) { return 5 - x }
|
function f1(x) { return 5 - x }
|
||||||
function f2(x) { return foo() / (5 - x) }
|
function f2(x) { return foo() / (5 - x) }
|
||||||
@@ -2276,8 +2275,8 @@ var_defs: {
|
|||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
var f1 = function(x, y) {
|
var f1 = function(x, y) {
|
||||||
var r = x + y, z = r * r - r, b = 7;
|
var r = x + y;
|
||||||
console.log(z + b);
|
console.log(r * r - r + 7);
|
||||||
};
|
};
|
||||||
f1("1", 0);
|
f1("1", 0);
|
||||||
}
|
}
|
||||||
@@ -2907,8 +2906,7 @@ issue_2187_1: {
|
|||||||
var a = 1;
|
var a = 1;
|
||||||
!function(foo) {
|
!function(foo) {
|
||||||
foo();
|
foo();
|
||||||
var a = 2;
|
console.log(2);
|
||||||
console.log(a);
|
|
||||||
}(function() {
|
}(function() {
|
||||||
console.log(a);
|
console.log(a);
|
||||||
});
|
});
|
||||||
@@ -6961,8 +6959,7 @@ sequence_in_iife_2: {
|
|||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
var a = "foo", b = 42;
|
var a = "foo", b = 42;
|
||||||
b = a;
|
console.log(a, b = a);
|
||||||
console.log(a, b);
|
|
||||||
}
|
}
|
||||||
expect_stdout: "foo foo"
|
expect_stdout: "foo foo"
|
||||||
}
|
}
|
||||||
@@ -8804,3 +8801,59 @@ issue_4732_2: {
|
|||||||
}
|
}
|
||||||
expect_stdout: "PASS"
|
expect_stdout: "PASS"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dot_in_try: {
|
||||||
|
options = {
|
||||||
|
collapse_vars: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var o, a = 6, b = 7, c;
|
||||||
|
try {
|
||||||
|
c = a * b;
|
||||||
|
o.p(c);
|
||||||
|
} catch (e) {
|
||||||
|
console.log(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
var o, a = 6, b = 7, c;
|
||||||
|
try {
|
||||||
|
c = a * b;
|
||||||
|
o.p(c);
|
||||||
|
} catch (e) {
|
||||||
|
console.log(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect_stdout: "42"
|
||||||
|
}
|
||||||
|
|
||||||
|
dot_non_local: {
|
||||||
|
options = {
|
||||||
|
collapse_vars: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var o, a = 6, b = 7, c;
|
||||||
|
function f() {
|
||||||
|
c = a * b;
|
||||||
|
o.p(c);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
f();
|
||||||
|
} catch (e) {
|
||||||
|
console.log(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
var o, a = 6, b = 7, c;
|
||||||
|
function f() {
|
||||||
|
c = a * b;
|
||||||
|
o.p(c);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
f();
|
||||||
|
} catch (e) {
|
||||||
|
console.log(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect_stdout: "42"
|
||||||
|
}
|
||||||
|
|||||||
@@ -2726,8 +2726,8 @@ issue_3944: {
|
|||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
void function f() {
|
void function f() {
|
||||||
while (a = 0 == (a = void 0), console.log(a), void 0);
|
while (b = void 0, b = console.log(0 == (b && b.p)), void 0);
|
||||||
var a;
|
var b;
|
||||||
f;
|
f;
|
||||||
}();
|
}();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2492,14 +2492,14 @@ issue_3297_3: {
|
|||||||
input: {
|
input: {
|
||||||
function function1(session) {
|
function function1(session) {
|
||||||
var public = {
|
var public = {
|
||||||
processBulk: processBulk
|
processBulk: processBulk,
|
||||||
};
|
};
|
||||||
return public;
|
return public;
|
||||||
function processBulk(bulk) {
|
function processBulk(bulk) {
|
||||||
var subparam1 = session();
|
var subparam1 = session();
|
||||||
function processOne(param1) {
|
function processOne(param1) {
|
||||||
var param2 = {
|
var param2 = {
|
||||||
subparam1: subparam1
|
subparam1: subparam1,
|
||||||
};
|
};
|
||||||
doProcessOne({
|
doProcessOne({
|
||||||
param1: param1,
|
param1: param1,
|
||||||
@@ -2525,18 +2525,18 @@ issue_3297_3: {
|
|||||||
return {
|
return {
|
||||||
processBulk: function n(o) {
|
processBulk: function n(o) {
|
||||||
var r, t, u = c();
|
var r, t, u = c();
|
||||||
o && 0 < o.length && (r = {
|
o && 0 < o.length && (r = o.shift(),
|
||||||
param1: o.shift(),
|
|
||||||
param2: {
|
|
||||||
subparam1: u
|
|
||||||
}
|
|
||||||
},
|
|
||||||
t = function() {
|
t = function() {
|
||||||
n(o);
|
n(o);
|
||||||
},
|
},
|
||||||
console.log(JSON.stringify(r)),
|
console.log(JSON.stringify({
|
||||||
|
param1: r,
|
||||||
|
param2: {
|
||||||
|
subparam1: u,
|
||||||
|
},
|
||||||
|
})),
|
||||||
t());
|
t());
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
function1(function() {
|
function1(function() {
|
||||||
|
|||||||
@@ -663,12 +663,21 @@ side_effects_cascade_1: {
|
|||||||
if (a < 0) a = 0;
|
if (a < 0) a = 0;
|
||||||
b.a = a;
|
b.a = a;
|
||||||
}
|
}
|
||||||
|
var m = {}, n = {};
|
||||||
|
f(13, m);
|
||||||
|
f("foo", n);
|
||||||
|
console.log(m.a, n.a);
|
||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
function f(a, b) {
|
function f(a, b) {
|
||||||
(a -= 42) < 0 && (a = 0), b.a = a;
|
b.a = a = (a -= 42) < 0 ? 0 : a;
|
||||||
}
|
}
|
||||||
|
var m = {}, n = {};
|
||||||
|
f(13, m),
|
||||||
|
f("foo", n),
|
||||||
|
console.log(m.a, n.a);
|
||||||
}
|
}
|
||||||
|
expect_stdout: "0 NaN"
|
||||||
}
|
}
|
||||||
|
|
||||||
side_effects_cascade_2: {
|
side_effects_cascade_2: {
|
||||||
|
|||||||
Reference in New Issue
Block a user