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_PropAccess) {
|
||||
var exp = node.expression;
|
||||
return side_effects || !value_def && exp.may_throw_on_access(compressor)
|
||||
|| exp instanceof AST_SymbolRef && is_arguments(exp.definition());
|
||||
return side_effects
|
||||
|| 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_SymbolRef) {
|
||||
|
||||
@@ -620,11 +620,10 @@ collapse_rhs: {
|
||||
expect: {
|
||||
"use strict";
|
||||
var a = "FAIL";
|
||||
a = "PASS";
|
||||
class A {
|
||||
p = "PASS";
|
||||
}
|
||||
console.log(a);
|
||||
console.log(a = "PASS");
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
node_version: ">=12"
|
||||
|
||||
@@ -958,8 +958,7 @@ collapse_vars_misc: {
|
||||
}
|
||||
expect: {
|
||||
function f0(o, a, h) {
|
||||
var b = 3 - a;
|
||||
return o.run(b)[7] = h;
|
||||
return o.run(3 - a)[7] = h;
|
||||
}
|
||||
function f1(x) { return 5 - x }
|
||||
function f2(x) { return foo() / (5 - x) }
|
||||
@@ -2276,8 +2275,8 @@ var_defs: {
|
||||
}
|
||||
expect: {
|
||||
var f1 = function(x, y) {
|
||||
var r = x + y, z = r * r - r, b = 7;
|
||||
console.log(z + b);
|
||||
var r = x + y;
|
||||
console.log(r * r - r + 7);
|
||||
};
|
||||
f1("1", 0);
|
||||
}
|
||||
@@ -2907,8 +2906,7 @@ issue_2187_1: {
|
||||
var a = 1;
|
||||
!function(foo) {
|
||||
foo();
|
||||
var a = 2;
|
||||
console.log(a);
|
||||
console.log(2);
|
||||
}(function() {
|
||||
console.log(a);
|
||||
});
|
||||
@@ -6961,8 +6959,7 @@ sequence_in_iife_2: {
|
||||
}
|
||||
expect: {
|
||||
var a = "foo", b = 42;
|
||||
b = a;
|
||||
console.log(a, b);
|
||||
console.log(a, b = a);
|
||||
}
|
||||
expect_stdout: "foo foo"
|
||||
}
|
||||
@@ -8804,3 +8801,59 @@ issue_4732_2: {
|
||||
}
|
||||
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: {
|
||||
void function f() {
|
||||
while (a = 0 == (a = void 0), console.log(a), void 0);
|
||||
var a;
|
||||
while (b = void 0, b = console.log(0 == (b && b.p)), void 0);
|
||||
var b;
|
||||
f;
|
||||
}();
|
||||
}
|
||||
|
||||
@@ -2492,14 +2492,14 @@ issue_3297_3: {
|
||||
input: {
|
||||
function function1(session) {
|
||||
var public = {
|
||||
processBulk: processBulk
|
||||
processBulk: processBulk,
|
||||
};
|
||||
return public;
|
||||
function processBulk(bulk) {
|
||||
var subparam1 = session();
|
||||
function processOne(param1) {
|
||||
var param2 = {
|
||||
subparam1: subparam1
|
||||
subparam1: subparam1,
|
||||
};
|
||||
doProcessOne({
|
||||
param1: param1,
|
||||
@@ -2525,18 +2525,18 @@ issue_3297_3: {
|
||||
return {
|
||||
processBulk: function n(o) {
|
||||
var r, t, u = c();
|
||||
o && 0 < o.length && (r = {
|
||||
param1: o.shift(),
|
||||
param2: {
|
||||
subparam1: u
|
||||
}
|
||||
},
|
||||
o && 0 < o.length && (r = o.shift(),
|
||||
t = function() {
|
||||
n(o);
|
||||
},
|
||||
console.log(JSON.stringify(r)),
|
||||
console.log(JSON.stringify({
|
||||
param1: r,
|
||||
param2: {
|
||||
subparam1: u,
|
||||
},
|
||||
})),
|
||||
t());
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
function1(function() {
|
||||
|
||||
@@ -663,12 +663,21 @@ side_effects_cascade_1: {
|
||||
if (a < 0) a = 0;
|
||||
b.a = a;
|
||||
}
|
||||
var m = {}, n = {};
|
||||
f(13, m);
|
||||
f("foo", n);
|
||||
console.log(m.a, n.a);
|
||||
}
|
||||
expect: {
|
||||
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: {
|
||||
|
||||
Reference in New Issue
Block a user