aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2017-03-25 16:21:42 +0800
committerGitHub <noreply@github.com>2017-03-25 16:21:42 +0800
commit8ca2401ebe024287ce1133d2707b1a8ce91f4e6c (patch)
treefd3c61620059e83a4d110181ca32e4804270637b
parent491f16c766c92e20260b99696b6081f333ceaf0f (diff)
downloadtracifyjs-8ca2401ebe024287ce1133d2707b1a8ce91f4e6c.tar.gz
tracifyjs-8ca2401ebe024287ce1133d2707b1a8ce91f4e6c.zip
fix `dead_code` on `AST_Switch` (#1667)
Need to call `extract_declarations_from_unreachable_code()`. fixes #1663
-rw-r--r--lib/compress.js32
-rw-r--r--test/compress/switch.js32
2 files changed, 50 insertions, 14 deletions
diff --git a/lib/compress.js b/lib/compress.js
index ab7cca6f..47eb4d73 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2529,14 +2529,14 @@ merge(Compressor.prototype, {
// no need to descend these node types
return node;
}
- else if (node instanceof AST_Switch && node === self) {
+ else if (node === self) {
node = node.clone();
descend(node, this);
return ruined ? node : make_node(AST_BlockStatement, node, {
- body: node.body.reduce(function(a, branch){
- return a.concat(branch.body);
- }, [])
- }).transform(compressor);
+ body: node.body.map(function(stat) {
+ return stat instanceof AST_SwitchBranch ? make_node(AST_BlockStatement, stat, stat) : stat;
+ })
+ }).optimize(compressor);
}
else if (node instanceof AST_If || node instanceof AST_Try) {
var save = in_if;
@@ -2559,10 +2559,10 @@ merge(Compressor.prototype, {
}
if (in_block) return node;
stopped = true;
- return in_list ? MAP.skip : make_node(AST_EmptyStatement, node);
+ return skip(node);
}
else if (node instanceof AST_SwitchBranch && this.parent() === self) {
- if (stopped) return MAP.skip;
+ if (stopped) return skip(node);
if (node instanceof AST_Case) {
var exp = node.expression.evaluate(compressor);
if (exp === node.expression) {
@@ -2572,16 +2572,20 @@ merge(Compressor.prototype, {
if (exp === value || started) {
started = true;
if (aborts(node)) stopped = true;
- descend(node, this);
- return node;
- }
- return MAP.skip;
+ } else return skip(node);
}
- descend(node, this);
- return node;
+ }
+
+ function skip(node) {
+ var a = [];
+ extract_declarations_from_unreachable_code(compressor, node, a);
+ return in_list ? MAP.splice(a) : make_node(AST_BlockStatement, node, {
+ body: a
+ });
}
});
- tt.stack = compressor.stack.slice(); // so that's able to see parent nodes
+ // allow transform() to view the whole AST
+ tt.stack = compressor.stack.slice(0, -1);
self = self.transform(tt);
} catch(ex) {
if (ex !== self) throw ex;
diff --git a/test/compress/switch.js b/test/compress/switch.js
index 62e39cf7..01d45f78 100644
--- a/test/compress/switch.js
+++ b/test/compress/switch.js
@@ -258,3 +258,35 @@ keep_default: {
}
}
}
+
+issue_1663: {
+ options = {
+ dead_code: true,
+ evaluate: true,
+ }
+ input: {
+ var a = 100, b = 10;
+ function f() {
+ switch (1) {
+ case 1:
+ b = a++;
+ return ++b;
+ default:
+ var b;
+ }
+ }
+ f();
+ console.log(a, b);
+ }
+ expect: {
+ var a = 100, b = 10;
+ function f() {
+ b = a++;
+ return ++b;
+ var b;
+ }
+ f();
+ console.log(a, b);
+ }
+ expect_stdout: true
+}