diff options
author | Mihai Bazon <mihai@bazon.net> | 2013-03-01 13:12:03 +0200 |
---|---|---|
committer | Mihai Bazon <mihai@bazon.net> | 2013-03-01 13:12:03 +0200 |
commit | dac6efb43d7112f697b863fc6783e6324e0aa4c0 (patch) | |
tree | 23c491ad7c89bbff1268d7bedef1e9b4a88ca47f | |
parent | 8880f4824c2214b1a6bd6ebc954a04d38124dadb (diff) | |
download | tracifyjs-dac6efb43d7112f697b863fc6783e6324e0aa4c0.tar.gz tracifyjs-dac6efb43d7112f697b863fc6783e6324e0aa4c0.zip |
Drop last `default:` if it's the last branch and empty
Close #141
-rw-r--r-- | lib/compress.js | 17 | ||||
-rw-r--r-- | test/compress/switch.js | 50 |
2 files changed, 62 insertions, 5 deletions
diff --git a/lib/compress.js b/lib/compress.js index f4048991..efdb49fe 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1414,11 +1414,18 @@ merge(Compressor.prototype, { body: self.expression }).transform(compressor); } - var last_branch = self.body[self.body.length - 1]; - if (last_branch) { - var stat = last_branch.body[last_branch.body.length - 1]; // last statement - if (stat instanceof AST_Break && loop_body(compressor.loopcontrol_target(stat.label)) === self) - last_branch.body.pop(); + for(;;) { + var last_branch = self.body[self.body.length - 1]; + if (last_branch) { + var stat = last_branch.body[last_branch.body.length - 1]; // last statement + if (stat instanceof AST_Break && loop_body(compressor.loopcontrol_target(stat.label)) === self) + last_branch.body.pop(); + if (last_branch instanceof AST_Default && last_branch.body.length == 0) { + self.body.pop(); + continue; + } + } + break; } var exp = self.expression.evaluate(compressor); out: if (exp.length == 2) try { diff --git a/test/compress/switch.js b/test/compress/switch.js index 6fde5dd3..62e39cf7 100644 --- a/test/compress/switch.js +++ b/test/compress/switch.js @@ -208,3 +208,53 @@ constant_switch_9: { } } } + +drop_default_1: { + options = { dead_code: true }; + input: { + switch (foo) { + case 'bar': baz(); + default: + } + } + expect: { + switch (foo) { + case 'bar': baz(); + } + } +} + +drop_default_2: { + options = { dead_code: true }; + input: { + switch (foo) { + case 'bar': baz(); break; + default: + break; + } + } + expect: { + switch (foo) { + case 'bar': baz(); + } + } +} + +keep_default: { + options = { dead_code: true }; + input: { + switch (foo) { + case 'bar': baz(); + default: + something(); + break; + } + } + expect: { + switch (foo) { + case 'bar': baz(); + default: + something(); + } + } +} |