improve handling of switch statements (#4114)

This commit is contained in:
Alex Lam S.L
2020-09-16 20:12:08 +01:00
committed by GitHub
parent 2a053710bd
commit a9d934ab4e
3 changed files with 37 additions and 37 deletions

View File

@@ -631,6 +631,9 @@ var AST_Switch = DEFNODE("Switch", "expression", {
}, },
_validate: function() { _validate: function() {
must_be_expression(this, "expression"); must_be_expression(this, "expression");
this.body.forEach(function(node) {
if (!(node instanceof AST_SwitchBranch)) throw new Error("body must be AST_SwitchBranch[]");
});
}, },
}, AST_Block); }, AST_Block);

View File

@@ -672,15 +672,6 @@ merge(Compressor.prototype, {
exp.left.definition().bool_fn++; exp.left.definition().bool_fn++;
} }
}); });
def(AST_Case, function(tw) {
push(tw);
this.expression.walk(tw);
pop(tw);
push(tw);
walk_body(this, tw);
pop(tw);
return true;
});
def(AST_Conditional, function(tw) { def(AST_Conditional, function(tw) {
this.condition.walk(tw); this.condition.walk(tw);
push(tw); push(tw);
@@ -691,12 +682,6 @@ merge(Compressor.prototype, {
pop(tw); pop(tw);
return true; return true;
}); });
def(AST_Default, function(tw, descend) {
push(tw);
descend();
pop(tw);
return true;
});
def(AST_Defun, function(tw, descend, compressor) { def(AST_Defun, function(tw, descend, compressor) {
var id = this.name.definition().id; var id = this.name.definition().id;
if (tw.defun_visited[id]) return true; if (tw.defun_visited[id]) return true;
@@ -822,6 +807,27 @@ merge(Compressor.prototype, {
pop(tw); pop(tw);
return true; return true;
}); });
def(AST_Switch, function(tw) {
this.expression.walk(tw);
var first = true;
this.body.forEach(function(branch) {
if (branch instanceof AST_Default) return;
branch.expression.walk(tw);
if (first) {
first = false;
push(tw);
}
})
if (!first) pop(tw);
walk_body(this, tw);
return true;
});
def(AST_SwitchBranch, function(tw) {
push(tw);
walk_body(this, tw);
pop(tw);
return true;
});
def(AST_SymbolCatch, function() { def(AST_SymbolCatch, function() {
this.definition().fixed = false; this.definition().fixed = false;
}); });
@@ -4311,7 +4317,7 @@ merge(Compressor.prototype, {
if (!compressor.option("merge_vars")) return; if (!compressor.option("merge_vars")) return;
var self = this, segment = null; var self = this, segment = null;
var first = [], last = [], index = 0; var first = [], last = [], index = 0;
var declarations = Object.create(null); var declarations = new Dictionary();
var references = Object.create(null); var references = Object.create(null);
var prev = Object.create(null); var prev = Object.create(null);
var tw = new TreeWalker(function(node, descend) { var tw = new TreeWalker(function(node, descend) {
@@ -4394,19 +4400,16 @@ merge(Compressor.prototype, {
} }
if (node instanceof AST_Switch) { if (node instanceof AST_Switch) {
node.expression.walk(tw); node.expression.walk(tw);
var first = true; var save = segment;
node.body.forEach(function(branch) { node.body.forEach(function(branch) {
if (branch instanceof AST_Default) return; if (branch instanceof AST_Default) return;
if (!first) push();
branch.expression.walk(tw); branch.expression.walk(tw);
if (!first) pop(); if (save === segment) push();
first = false;
}); });
segment = save;
node.body.forEach(function(branch) { node.body.forEach(function(branch) {
push(); push();
branch.body.forEach(function(stat) { walk_body(branch, tw);
stat.walk(tw);
});
pop(); pop();
}); });
return true; return true;
@@ -4421,16 +4424,12 @@ merge(Compressor.prototype, {
} }
if (node instanceof AST_Try) { if (node instanceof AST_Try) {
push(); push();
node.body.forEach(function(stat) { walk_body(node, tw);
stat.walk(tw);
});
pop(); pop();
if (node.bcatch) { if (node.bcatch) {
references[node.bcatch.argname.definition().id] = false; references[node.bcatch.argname.definition().id] = false;
push(); push();
node.bcatch.body.forEach(function(stat) { walk_body(node.bcatch, tw);
stat.walk(tw);
});
pop(); pop();
} }
if (node.bfinally) node.bfinally.walk(tw); if (node.bfinally) node.bfinally.walk(tw);
@@ -4448,9 +4447,7 @@ merge(Compressor.prototype, {
node.value.walk(tw); node.value.walk(tw);
mark(node.name, true); mark(node.name, true);
} else { } else {
var id = node.name.definition().id; declarations.add(node.name.definition().id, node.name);
if (!(id in declarations)) declarations[id] = [];
declarations[id].push(node.name);
} }
return true; return true;
} }
@@ -4475,7 +4472,7 @@ merge(Compressor.prototype, {
continue; continue;
} }
var orig = [], refs = []; var orig = [], refs = [];
if (id in declarations) declarations[id].forEach(function(sym) { if (declarations.has(id)) declarations.get(id).forEach(function(sym) {
sym.thedef = def; sym.thedef = def;
sym.name = def.name; sym.name = def.name;
orig.push(sym); orig.push(sym);

View File

@@ -2176,6 +2176,7 @@ issue_1670_6: {
keep_fargs: false, keep_fargs: false,
reduce_funcs: true, reduce_funcs: true,
reduce_vars: true, reduce_vars: true,
sequences: true,
side_effects: true, side_effects: true,
switches: true, switches: true,
unused: true, unused: true,
@@ -2193,10 +2194,9 @@ issue_1670_6: {
})(1); })(1);
} }
expect: { expect: {
(function(a) { (function() {
a = 1; console.log(1);
console.log(a); })();
})(1);
} }
expect_stdout: "1" expect_stdout: "1"
} }