enhance join_vars (#3783)
This commit is contained in:
@@ -2338,10 +2338,7 @@ merge(Compressor.prototype, {
|
|||||||
exprs = body.expressions.slice();
|
exprs = body.expressions.slice();
|
||||||
}
|
}
|
||||||
if (!exprs) return;
|
if (!exprs) return;
|
||||||
if (defn instanceof AST_Definitions) {
|
var trimmed = false;
|
||||||
var def = defn.definitions[defn.definitions.length - 1];
|
|
||||||
if (trim_assigns(def.name, def.value, exprs)) return exprs;
|
|
||||||
}
|
|
||||||
for (var i = exprs.length - 1; --i >= 0;) {
|
for (var i = exprs.length - 1; --i >= 0;) {
|
||||||
var expr = exprs[i];
|
var expr = exprs[i];
|
||||||
if (!(expr instanceof AST_Assign)) continue;
|
if (!(expr instanceof AST_Assign)) continue;
|
||||||
@@ -2349,8 +2346,38 @@ merge(Compressor.prototype, {
|
|||||||
if (!(expr.left instanceof AST_SymbolRef)) continue;
|
if (!(expr.left instanceof AST_SymbolRef)) continue;
|
||||||
var tail = exprs.slice(i + 1);
|
var tail = exprs.slice(i + 1);
|
||||||
if (!trim_assigns(expr.left, expr.right, tail)) continue;
|
if (!trim_assigns(expr.left, expr.right, tail)) continue;
|
||||||
return exprs.slice(0, i + 1).concat(tail);
|
trimmed = true;
|
||||||
|
exprs = exprs.slice(0, i + 1).concat(tail);
|
||||||
}
|
}
|
||||||
|
if (defn instanceof AST_Definitions) {
|
||||||
|
var def = defn.definitions[defn.definitions.length - 1];
|
||||||
|
if (trim_assigns(def.name, def.value, exprs)) trimmed = true;
|
||||||
|
if (join_var_assign(defn.definitions, exprs)) trimmed = true;
|
||||||
|
}
|
||||||
|
return trimmed && exprs;
|
||||||
|
}
|
||||||
|
|
||||||
|
function join_var_assign(definitions, exprs) {
|
||||||
|
var trimmed = false;
|
||||||
|
while (exprs.length) {
|
||||||
|
var expr = exprs[0];
|
||||||
|
if (!(expr instanceof AST_Assign)) break;
|
||||||
|
if (expr.operator != "=") break;
|
||||||
|
var lhs = expr.left;
|
||||||
|
if (!(lhs instanceof AST_SymbolRef)) break;
|
||||||
|
var def = lhs.definition();
|
||||||
|
if (def.scope !== scope) break;
|
||||||
|
var name = make_node(AST_SymbolVar, lhs, lhs);
|
||||||
|
definitions.push(make_node(AST_VarDef, expr, {
|
||||||
|
name: name,
|
||||||
|
value: expr.right
|
||||||
|
}));
|
||||||
|
def.orig.push(name);
|
||||||
|
def.replaced++;
|
||||||
|
exprs.shift();
|
||||||
|
trimmed = true;
|
||||||
|
}
|
||||||
|
return trimmed;
|
||||||
}
|
}
|
||||||
|
|
||||||
function trim_assigns(name, value, exprs) {
|
function trim_assigns(name, value, exprs) {
|
||||||
|
|||||||
@@ -804,7 +804,7 @@ collapse_vars_assignment: {
|
|||||||
function log(x) { return console.log(x), x; }
|
function log(x) { return console.log(x), x; }
|
||||||
function f0(c) {
|
function f0(c) {
|
||||||
var a = 3 / c;
|
var a = 3 / c;
|
||||||
return a = a;
|
return a;
|
||||||
}
|
}
|
||||||
function f1(c) {
|
function f1(c) {
|
||||||
return 1 - 3 / c;
|
return 1 - 3 / c;
|
||||||
@@ -2012,6 +2012,7 @@ issue_1631_3: {
|
|||||||
join_vars: true,
|
join_vars: true,
|
||||||
sequences: true,
|
sequences: true,
|
||||||
side_effects: true,
|
side_effects: true,
|
||||||
|
unused: true,
|
||||||
}
|
}
|
||||||
input: {
|
input: {
|
||||||
function g() {
|
function g() {
|
||||||
@@ -2031,8 +2032,8 @@ issue_1631_3: {
|
|||||||
function f() {
|
function f() {
|
||||||
return a = 2, 4;
|
return a = 2, 4;
|
||||||
}
|
}
|
||||||
var a = 0, b = 1, t = f();
|
var a = 0, t = f();
|
||||||
return b = a + t;
|
return a + t;
|
||||||
}
|
}
|
||||||
console.log(g());
|
console.log(g());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2444,3 +2444,21 @@ issue_3746: {
|
|||||||
}
|
}
|
||||||
expect_stdout: "PASS"
|
expect_stdout: "PASS"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
join_vars_assign: {
|
||||||
|
options = {
|
||||||
|
join_vars: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var y, x;
|
||||||
|
x = Object("PAS");
|
||||||
|
y = Object("S");
|
||||||
|
console.log(x + y);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
var x = Object("PAS"), y = Object("S");
|
||||||
|
console.log(x + y);
|
||||||
|
}
|
||||||
|
expect_stdout: "PASS"
|
||||||
|
}
|
||||||
|
|||||||
@@ -767,18 +767,17 @@ issue_3071_1: {
|
|||||||
var obj = {};
|
var obj = {};
|
||||||
obj.one = 1;
|
obj.one = 1;
|
||||||
obj.two = 2;
|
obj.two = 2;
|
||||||
console.log(obj.one);
|
console.log(obj.one, obj.two);
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
console.log(1);
|
console.log(1, 2);
|
||||||
}
|
}
|
||||||
expect_stdout: "1"
|
expect_stdout: "1 2"
|
||||||
}
|
}
|
||||||
|
|
||||||
issue_3071_2: {
|
issue_3071_2: {
|
||||||
options = {
|
options = {
|
||||||
evaluate: true,
|
|
||||||
hoist_props: true,
|
hoist_props: true,
|
||||||
inline: true,
|
inline: true,
|
||||||
join_vars: true,
|
join_vars: true,
|
||||||
@@ -793,19 +792,18 @@ issue_3071_2: {
|
|||||||
obj = {};
|
obj = {};
|
||||||
obj.one = 1;
|
obj.one = 1;
|
||||||
obj.two = 2;
|
obj.two = 2;
|
||||||
console.log(obj.one);
|
console.log(obj.one, obj.two);
|
||||||
var obj;
|
var obj;
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
console.log(1);
|
console.log(1, 2);
|
||||||
}
|
}
|
||||||
expect_stdout: "1"
|
expect_stdout: "1 2"
|
||||||
}
|
}
|
||||||
|
|
||||||
issue_3071_2_toplevel: {
|
issue_3071_2_toplevel: {
|
||||||
options = {
|
options = {
|
||||||
evaluate: true,
|
|
||||||
hoist_props: true,
|
hoist_props: true,
|
||||||
inline: true,
|
inline: true,
|
||||||
join_vars: true,
|
join_vars: true,
|
||||||
@@ -821,14 +819,14 @@ issue_3071_2_toplevel: {
|
|||||||
obj = {};
|
obj = {};
|
||||||
obj.one = 1;
|
obj.one = 1;
|
||||||
obj.two = 2;
|
obj.two = 2;
|
||||||
console.log(obj.one);
|
console.log(obj.one, obj.two);
|
||||||
var obj;
|
var obj;
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
console.log(1);
|
console.log(1, 2);
|
||||||
}
|
}
|
||||||
expect_stdout: "1"
|
expect_stdout: "1 2"
|
||||||
}
|
}
|
||||||
|
|
||||||
issue_3071_3: {
|
issue_3071_3: {
|
||||||
|
|||||||
@@ -1875,8 +1875,8 @@ join_expr: {
|
|||||||
expect: {
|
expect: {
|
||||||
var c = "FAIL";
|
var c = "FAIL";
|
||||||
(function() {
|
(function() {
|
||||||
var a = 0;
|
var a = 0, a = { b: 0 };
|
||||||
switch (a = { b: 0 }, a.b) {
|
switch (a.b) {
|
||||||
case 0:
|
case 0:
|
||||||
c = "PASS";
|
c = "PASS";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user