diff options
author | kzc <zaxxon2011@gmail.com> | 2015-10-28 13:25:27 -0400 |
---|---|---|
committer | Richard van Velzen <rvanvelzen@experty.com> | 2015-10-29 08:19:12 +0100 |
commit | 7491d07666822fe943bd5f5768a5761b562c634a (patch) | |
tree | 8377225481369213a554ed2dd3fc051ed49c0fe1 | |
parent | 335e349314963e1761f7531656a64efaff548ceb (diff) | |
download | tracifyjs-7491d07666822fe943bd5f5768a5761b562c634a.tar.gz tracifyjs-7491d07666822fe943bd5f5768a5761b562c634a.zip |
optimize `return undefined` and `return void 0`
-rw-r--r-- | lib/compress.js | 10 | ||||
-rw-r--r-- | test/compress/return_undefined.js | 229 |
2 files changed, 239 insertions, 0 deletions
diff --git a/lib/compress.js b/lib/compress.js index 216aade9..ebe7e957 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -65,6 +65,7 @@ function Compressor(options, false_by_default) { keep_fnames : false, hoist_vars : false, if_return : !false_by_default, + return_void_0 : !false_by_default, join_vars : !false_by_default, cascade : !false_by_default, side_effects : !false_by_default, @@ -2519,4 +2520,13 @@ merge(Compressor.prototype, { OPT(AST_Object, literals_in_boolean_context); OPT(AST_RegExp, literals_in_boolean_context); + OPT(AST_Return, function(self, compressor){ + if (compressor.option("return_void_0")) { + if (self.value instanceof AST_Undefined) { + self.value = null; + } + } + return self; + }); + })(); diff --git a/test/compress/return_undefined.js b/test/compress/return_undefined.js new file mode 100644 index 00000000..be79d89d --- /dev/null +++ b/test/compress/return_undefined.js @@ -0,0 +1,229 @@ +return_void_0_true: { + options = { + return_void_0 : true, + sequences : false, + if_return : true, + evaluate : true, + dead_code : true, + conditionals : true, + comparisons : true, + booleans : true, + unused : true, + side_effects : true, + properties : true, + drop_debugger : true, + loops : true, + hoist_funs : true, + keep_fargs : true, + keep_fnames : false, + hoist_vars : true, + join_vars : true, + cascade : true, + negate_iife : true + }; + input: { + function f0() { + } + function f1() { + return undefined; + } + function f2() { + return void 0; + } + function f3() { + return void 123; + } + function f4() { + return; + } + function f5(a, b) { + console.log(a, b); + baz(a); + return; + } + function f6(a, b) { + console.log(a, b); + if (a) { + foo(b); + baz(a); + return a + b; + } + return undefined; + } + function f7(a, b) { + console.log(a, b); + if (a) { + foo(b); + baz(a); + return void 0; + } + return a + b; + } + function f8(a, b) { + foo(a); + bar(b); + return void 0; + } + function f9(a, b) { + foo(a); + bar(b); + return undefined; + } + } + expect: { + function f0() {} + function f1() {} + function f2() {} + function f3() {} + function f4() {} + function f5(a, b) { + console.log(a, b); + baz(a); + } + function f6(a, b) { + console.log(a, b); + if (a) { + foo(b); + baz(a); + return a + b; + } + } + function f7(a, b) { + console.log(a, b); + if (!a) + return a + b; + foo(b); + baz(a); + } + function f8(a, b) { + foo(a); + bar(b); + } + function f9(a, b) { + foo(a); + bar(b); + } + } +} + +return_void_0_false: { + options = { + return_void_0 : false, + sequences : false, + if_return : true, + evaluate : true, + dead_code : true, + conditionals : true, + comparisons : true, + booleans : true, + unused : true, + side_effects : true, + properties : true, + drop_debugger : true, + loops : true, + hoist_funs : true, + keep_fargs : true, + keep_fnames : false, + hoist_vars : true, + join_vars : true, + cascade : true, + negate_iife : true + }; + input: { + function f0() { + } + function f1() { + return undefined; + } + function f2() { + return void 0; + } + function f3() { + return void 123; + } + function f4() { + return; + } + function f5(a, b) { + console.log(a, b); + baz(a); + return; + } + function f6(a, b) { + console.log(a, b); + if (a) { + foo(b); + baz(a); + return a + b; + } + return undefined; + } + function f7(a, b) { + console.log(a, b); + if (a) { + foo(b); + baz(a); + return void 0; + } + return a + b; + } + function f8(a, b) { + foo(a); + bar(b); + return void 0; + } + function f9(a, b) { + foo(a); + bar(b); + return undefined; + } + } + expect: { + function f0() { + } + function f1() { + return void 0; + } + function f2() { + return void 0; + } + function f3() { + return void 0; + } + function f4() { + } + function f5(a, b) { + console.log(a, b); + baz(a); + } + function f6(a, b) { + console.log(a, b); + if (a) { + foo(b); + baz(a); + return a + b; + } + return void 0; + } + function f7(a, b) { + console.log(a, b); + if (a) { + foo(b); + baz(a); + return void 0; + } + return a + b; + } + function f8(a, b) { + foo(a); + bar(b); + return void 0; + } + function f9(a, b) { + foo(a); + bar(b); + return void 0; + } + } +} + |