enhance merge_vars (#5349)

This commit is contained in:
Alex Lam S.L
2022-02-12 12:41:02 +00:00
committed by GitHub
parent 33c163f648
commit 6d0bb58d68
2 changed files with 190 additions and 20 deletions

View File

@@ -6025,10 +6025,7 @@ Compressor.prototype.compress = function(node) {
}
if (node instanceof AST_Binary) {
if (!lazy_op[node.operator]) return;
node.left.walk(tw);
push();
node.right.walk(tw);
pop();
walk_cond(node);
return true;
}
if (node instanceof AST_Break) {
@@ -6053,13 +6050,7 @@ Compressor.prototype.compress = function(node) {
return true;
}
if (node instanceof AST_Conditional) {
node.condition.walk(tw);
push();
node.consequent.walk(tw);
pop();
push();
node.alternative.walk(tw);
pop();
walk_cond(node.condition, node.consequent, node.alternative);
return true;
}
if (node instanceof AST_Continue) {
@@ -6100,15 +6091,7 @@ Compressor.prototype.compress = function(node) {
return true;
}
if (node instanceof AST_If) {
node.condition.walk(tw);
push();
node.body.walk(tw);
pop();
if (node.alternative) {
push();
node.alternative.walk(tw);
pop();
}
walk_cond(node.condition, node.body, node.alternative);
return true;
}
if (node instanceof AST_LabeledStatement) {
@@ -6276,6 +6259,41 @@ Compressor.prototype.compress = function(node) {
}
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);
self.walk(tw);