diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-09-16 20:12:08 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-17 03:12:08 +0800 |
commit | a9d934ab4eacc3c987a91f8e7fd176e983c36f55 (patch) | |
tree | 12cf34bd9778f88b2b8e724c3f77a613fb3730da /lib | |
parent | 2a053710bdafde88c305d318ed2e0196d5dcdfc7 (diff) | |
download | tracifyjs-a9d934ab4eacc3c987a91f8e7fd176e983c36f55.tar.gz tracifyjs-a9d934ab4eacc3c987a91f8e7fd176e983c36f55.zip |
improve handling of `switch` statements (#4114)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ast.js | 3 | ||||
-rw-r--r-- | lib/compress.js | 63 |
2 files changed, 33 insertions, 33 deletions
@@ -631,6 +631,9 @@ var AST_Switch = DEFNODE("Switch", "expression", { }, _validate: function() { 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); diff --git a/lib/compress.js b/lib/compress.js index e3139054..f19832e1 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -672,15 +672,6 @@ merge(Compressor.prototype, { 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) { this.condition.walk(tw); push(tw); @@ -691,12 +682,6 @@ merge(Compressor.prototype, { pop(tw); return true; }); - def(AST_Default, function(tw, descend) { - push(tw); - descend(); - pop(tw); - return true; - }); def(AST_Defun, function(tw, descend, compressor) { var id = this.name.definition().id; if (tw.defun_visited[id]) return true; @@ -822,6 +807,27 @@ merge(Compressor.prototype, { pop(tw); 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() { this.definition().fixed = false; }); @@ -4311,7 +4317,7 @@ merge(Compressor.prototype, { if (!compressor.option("merge_vars")) return; var self = this, segment = null; var first = [], last = [], index = 0; - var declarations = Object.create(null); + var declarations = new Dictionary(); var references = Object.create(null); var prev = Object.create(null); var tw = new TreeWalker(function(node, descend) { @@ -4394,19 +4400,16 @@ merge(Compressor.prototype, { } if (node instanceof AST_Switch) { node.expression.walk(tw); - var first = true; + var save = segment; node.body.forEach(function(branch) { if (branch instanceof AST_Default) return; - if (!first) push(); branch.expression.walk(tw); - if (!first) pop(); - first = false; + if (save === segment) push(); }); + segment = save; node.body.forEach(function(branch) { push(); - branch.body.forEach(function(stat) { - stat.walk(tw); - }); + walk_body(branch, tw); pop(); }); return true; @@ -4421,16 +4424,12 @@ merge(Compressor.prototype, { } if (node instanceof AST_Try) { push(); - node.body.forEach(function(stat) { - stat.walk(tw); - }); + walk_body(node, tw); pop(); if (node.bcatch) { references[node.bcatch.argname.definition().id] = false; push(); - node.bcatch.body.forEach(function(stat) { - stat.walk(tw); - }); + walk_body(node.bcatch, tw); pop(); } if (node.bfinally) node.bfinally.walk(tw); @@ -4448,9 +4447,7 @@ merge(Compressor.prototype, { node.value.walk(tw); mark(node.name, true); } else { - var id = node.name.definition().id; - if (!(id in declarations)) declarations[id] = []; - declarations[id].push(node.name); + declarations.add(node.name.definition().id, node.name); } return true; } @@ -4475,7 +4472,7 @@ merge(Compressor.prototype, { continue; } 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.name = def.name; orig.push(sym); |