enhance merge_vars (#5349)
This commit is contained in:
@@ -6025,10 +6025,7 @@ Compressor.prototype.compress = function(node) {
|
|||||||
}
|
}
|
||||||
if (node instanceof AST_Binary) {
|
if (node instanceof AST_Binary) {
|
||||||
if (!lazy_op[node.operator]) return;
|
if (!lazy_op[node.operator]) return;
|
||||||
node.left.walk(tw);
|
walk_cond(node);
|
||||||
push();
|
|
||||||
node.right.walk(tw);
|
|
||||||
pop();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (node instanceof AST_Break) {
|
if (node instanceof AST_Break) {
|
||||||
@@ -6053,13 +6050,7 @@ Compressor.prototype.compress = function(node) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (node instanceof AST_Conditional) {
|
if (node instanceof AST_Conditional) {
|
||||||
node.condition.walk(tw);
|
walk_cond(node.condition, node.consequent, node.alternative);
|
||||||
push();
|
|
||||||
node.consequent.walk(tw);
|
|
||||||
pop();
|
|
||||||
push();
|
|
||||||
node.alternative.walk(tw);
|
|
||||||
pop();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (node instanceof AST_Continue) {
|
if (node instanceof AST_Continue) {
|
||||||
@@ -6100,15 +6091,7 @@ Compressor.prototype.compress = function(node) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (node instanceof AST_If) {
|
if (node instanceof AST_If) {
|
||||||
node.condition.walk(tw);
|
walk_cond(node.condition, node.body, node.alternative);
|
||||||
push();
|
|
||||||
node.body.walk(tw);
|
|
||||||
pop();
|
|
||||||
if (node.alternative) {
|
|
||||||
push();
|
|
||||||
node.alternative.walk(tw);
|
|
||||||
pop();
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (node instanceof AST_LabeledStatement) {
|
if (node instanceof AST_LabeledStatement) {
|
||||||
@@ -6276,6 +6259,41 @@ Compressor.prototype.compress = function(node) {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function walk_cond(condition, consequent, alternative) {
|
||||||
|
var save = segment;
|
||||||
|
var segments = [ save, save ];
|
||||||
|
if (condition instanceof AST_Binary) switch (condition.operator) {
|
||||||
|
case "&&":
|
||||||
|
segments[0] = walk_cond(condition.left, condition.right)[0];
|
||||||
|
break;
|
||||||
|
case "||":
|
||||||
|
case "??":
|
||||||
|
segments[1] = walk_cond(condition.left, null, condition.right)[1];
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
condition.walk(tw);
|
||||||
|
break;
|
||||||
|
} else if (condition instanceof AST_Conditional) {
|
||||||
|
walk_cond(condition.condition, condition.consequent, condition.alternative);
|
||||||
|
} else {
|
||||||
|
condition.walk(tw);
|
||||||
|
}
|
||||||
|
segment = segments[0];
|
||||||
|
if (consequent) {
|
||||||
|
push();
|
||||||
|
consequent.walk(tw);
|
||||||
|
}
|
||||||
|
segments[0] = segment;
|
||||||
|
segment = segments[1];
|
||||||
|
if (alternative) {
|
||||||
|
push();
|
||||||
|
alternative.walk(tw);
|
||||||
|
}
|
||||||
|
segments[1] = segment;
|
||||||
|
segment = save;
|
||||||
|
return segments;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
tw.directives = Object.create(compressor.directives);
|
tw.directives = Object.create(compressor.directives);
|
||||||
self.walk(tw);
|
self.walk(tw);
|
||||||
|
|||||||
@@ -169,6 +169,158 @@ conditional_branch: {
|
|||||||
expect_stdout: "PASS"
|
expect_stdout: "PASS"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
conditional_chain_1: {
|
||||||
|
options = {
|
||||||
|
merge_vars: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function f(a, b) {
|
||||||
|
var c, d;
|
||||||
|
if (a && (c = a))
|
||||||
|
console.log(c);
|
||||||
|
else
|
||||||
|
b || (d = b) ? console.log("foo") : console.log(d);
|
||||||
|
}
|
||||||
|
f("", null);
|
||||||
|
f("", true);
|
||||||
|
f(42, null);
|
||||||
|
f(42, true);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function f(a, b) {
|
||||||
|
var a, a;
|
||||||
|
if (a && (a = a))
|
||||||
|
console.log(a);
|
||||||
|
else
|
||||||
|
b || (a = b) ? console.log("foo") : console.log(a);
|
||||||
|
}
|
||||||
|
f("", null);
|
||||||
|
f("", true);
|
||||||
|
f(42, null);
|
||||||
|
f(42, true);
|
||||||
|
}
|
||||||
|
expect_stdout: [
|
||||||
|
"null",
|
||||||
|
"foo",
|
||||||
|
"42",
|
||||||
|
"42",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
conditional_chain_2: {
|
||||||
|
options = {
|
||||||
|
merge_vars: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function f(a, b) {
|
||||||
|
var c, d;
|
||||||
|
if (a && (c = a))
|
||||||
|
console.log(c);
|
||||||
|
else
|
||||||
|
b || (d = b) ? console.log(c) : console.log(d);
|
||||||
|
}
|
||||||
|
f("", null);
|
||||||
|
f("", true);
|
||||||
|
f(42, null);
|
||||||
|
f(42, true);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function f(a, b) {
|
||||||
|
var c, a;
|
||||||
|
if (a && (c = a))
|
||||||
|
console.log(c);
|
||||||
|
else
|
||||||
|
b || (a = b) ? console.log(c) : console.log(a);
|
||||||
|
}
|
||||||
|
f("", null);
|
||||||
|
f("", true);
|
||||||
|
f(42, null);
|
||||||
|
f(42, true);
|
||||||
|
}
|
||||||
|
expect_stdout: [
|
||||||
|
"null",
|
||||||
|
"undefined",
|
||||||
|
"42",
|
||||||
|
"42",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
conditional_chain_3: {
|
||||||
|
options = {
|
||||||
|
merge_vars: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function f(a, b) {
|
||||||
|
var c, d;
|
||||||
|
if (a && (c = a) || b || (d = b))
|
||||||
|
console.log(c);
|
||||||
|
else
|
||||||
|
console.log(d);
|
||||||
|
}
|
||||||
|
f("", null);
|
||||||
|
f("", true);
|
||||||
|
f(42, null);
|
||||||
|
f(42, true);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function f(a, b) {
|
||||||
|
var c, a;
|
||||||
|
if (a && (c = a) || b || (a = b))
|
||||||
|
console.log(c);
|
||||||
|
else
|
||||||
|
console.log(a);
|
||||||
|
}
|
||||||
|
f("", null);
|
||||||
|
f("", true);
|
||||||
|
f(42, null);
|
||||||
|
f(42, true);
|
||||||
|
}
|
||||||
|
expect_stdout: [
|
||||||
|
"null",
|
||||||
|
"undefined",
|
||||||
|
"42",
|
||||||
|
"42",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
conditional_chain_4: {
|
||||||
|
options = {
|
||||||
|
merge_vars: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function f(a, b) {
|
||||||
|
var c, d;
|
||||||
|
if (a && b ? c = a : d = b)
|
||||||
|
console.log(c);
|
||||||
|
else
|
||||||
|
console.log(d);
|
||||||
|
}
|
||||||
|
f("", null);
|
||||||
|
f("", true);
|
||||||
|
f(42, null);
|
||||||
|
f(42, true);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function f(a, b) {
|
||||||
|
var c, d;
|
||||||
|
if (a && b ? c = a : d = b)
|
||||||
|
console.log(c);
|
||||||
|
else
|
||||||
|
console.log(d);
|
||||||
|
}
|
||||||
|
f("", null);
|
||||||
|
f("", true);
|
||||||
|
f(42, null);
|
||||||
|
f(42, true);
|
||||||
|
}
|
||||||
|
expect_stdout: [
|
||||||
|
"null",
|
||||||
|
"undefined",
|
||||||
|
"null",
|
||||||
|
"42",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
if_branch: {
|
if_branch: {
|
||||||
options = {
|
options = {
|
||||||
merge_vars: true,
|
merge_vars: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user