diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-05-20 15:58:46 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-20 15:58:46 +0800 |
commit | 58fae7dc070449e650d1a48ad7144cb5571a510f (patch) | |
tree | ad1bce884d8ed589d4d3271155fe2cdd74c7ec97 /lib | |
parent | 5bf8d7e9490155ed6fabea5f1140a87a1e9b596e (diff) | |
download | tracifyjs-58fae7dc070449e650d1a48ad7144cb5571a510f.tar.gz tracifyjs-58fae7dc070449e650d1a48ad7144cb5571a510f.zip |
enhance `if_return` to handle `return void...` (#1977)
fixes #512
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/lib/compress.js b/lib/compress.js index f6bf3d05..73ab21f4 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -914,12 +914,12 @@ merge(Compressor.prototype, { continue loop; case stat instanceof AST_If: if (stat.body instanceof AST_Return) { + var value = stat.body.value; //--- // pretty silly case, but: // if (foo()) return; return; ==> foo(); return; - if (((in_lambda && ret.length == 0) - || (ret[0] instanceof AST_Return && !ret[0].value)) - && !stat.body.value && !stat.alternative) { + if ((in_lambda && ret.length == 0 || ret[0] instanceof AST_Return && !ret[0].value) + && !value && !stat.alternative) { CHANGED = true; var cond = make_node(AST_SimpleStatement, stat.condition, { body: stat.condition @@ -929,7 +929,7 @@ merge(Compressor.prototype, { } //--- // if (foo()) return x; return y; ==> return foo() ? x : y; - if (ret[0] instanceof AST_Return && stat.body.value && ret[0].value && !stat.alternative) { + if (ret[0] instanceof AST_Return && value && ret[0].value && !stat.alternative) { CHANGED = true; stat = stat.clone(); stat.alternative = ret[0]; @@ -939,7 +939,7 @@ merge(Compressor.prototype, { //--- // if (foo()) return x; [ return ; ] ==> return foo() ? x : undefined; if (multiple_if_returns && (ret.length == 0 || ret[0] instanceof AST_Return) - && stat.body.value && !stat.alternative && in_lambda) { + && value && !stat.alternative && in_lambda) { CHANGED = true; stat = stat.clone(); stat.alternative = ret[0] || make_node(AST_Return, stat, { @@ -949,8 +949,8 @@ merge(Compressor.prototype, { continue loop; } //--- - // if (foo()) return; [ else x... ]; y... ==> if (!foo()) { x...; y... } - if (!stat.body.value && in_lambda) { + // if (foo()) return [ void bar() ]; [ else x...; ] y... ==> if (!foo()) { x...; y... } else bar(); + if (in_lambda && (!value || value instanceof AST_UnaryPrefix && value.operator == "void")) { CHANGED = true; stat = stat.clone(); stat.condition = stat.condition.negate(compressor); @@ -959,11 +959,12 @@ merge(Compressor.prototype, { stat.body = make_node(AST_BlockStatement, stat, { body: body }); - stat.alternative = null; + stat.alternative = value ? make_node(AST_SimpleStatement, value, { + body: value.expression + }) : null; ret = funs.concat([ stat.transform(compressor) ]); continue loop; } - //--- // if (a) return b; if (c) return d; e; ==> return a ? b : c ? d : void e; // |