diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-01-12 16:00:19 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-13 00:00:19 +0800 |
commit | 46ad273df49b2e320c6ee01f167fb3f9b8242213 (patch) | |
tree | 34acc8e4f307055104d006f0fae7902fefac5cd4 | |
parent | b689028e874e35f529b2cc1df906f70d500f6085 (diff) | |
download | tracifyjs-46ad273df49b2e320c6ee01f167fb3f9b8242213.tar.gz tracifyjs-46ad273df49b2e320c6ee01f167fb3f9b8242213.zip |
enhance `rests` (#4546)
-rw-r--r-- | lib/compress.js | 25 | ||||
-rw-r--r-- | test/compress/rests.js | 56 |
2 files changed, 81 insertions, 0 deletions
diff --git a/lib/compress.js b/lib/compress.js index 83d20e8f..c9bd182e 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4871,8 +4871,18 @@ merge(Compressor.prototype, { return trim_block(self); }); + function drop_rest_farg(fn, compressor) { + if (!compressor.option("rests")) return; + if (fn.uses_arguments) return; + if (!(fn.rest instanceof AST_DestructuredArray)) return; + if (!compressor.drop_fargs(fn, compressor.parent())) return; + fn.argnames = fn.argnames.concat(fn.rest.elements); + fn.rest = null; + } + function opt_arrow(self, compressor) { if (!compressor.option("arrows")) return self; + drop_rest_farg(self, compressor); var body = tighten_body(self.value ? [ self.first_statement() ] : self.body, compressor); switch (body.length) { case 1: @@ -4892,7 +4902,14 @@ merge(Compressor.prototype, { OPT(AST_Arrow, opt_arrow); OPT(AST_AsyncArrow, opt_arrow); + OPT(AST_Defun, function(self, compressor) { + drop_rest_farg(self, compressor); + self.body = tighten_body(self.body, compressor); + return self; + }); + OPT(AST_Function, function(self, compressor) { + drop_rest_farg(self, compressor); self.body = tighten_body(self.body, compressor); if (compressor.option("inline")) for (var i = 0; i < self.body.length; i++) { var stat = self.body[i]; @@ -10686,6 +10703,14 @@ merge(Compressor.prototype, { return try_evaluate(compressor, self); }); + OPT(AST_DestructuredArray, function(self, compressor) { + if (compressor.option("rests") && self.rest instanceof AST_DestructuredArray) { + self.elements = self.elements.concat(self.rest.elements); + self.rest = null; + } + return self; + }); + OPT(AST_DestructuredKeyVal, function(self, compressor) { if (compressor.option("objects")) { var key = self.key; diff --git a/test/compress/rests.js b/test/compress/rests.js index e322112c..ed927700 100644 --- a/test/compress/rests.js +++ b/test/compress/rests.js @@ -486,6 +486,62 @@ keep_arguments: { node_version: ">=6" } +drop_rest_array: { + options = { + rests: true, + } + input: { + var [ ...[ a ]] = [ "PASS" ]; + console.log(a); + } + expect: { + var [ a ] = [ "PASS" ]; + console.log(a); + } + expect_stdout: "PASS" + node_version: ">=6" +} + +drop_rest_arrow: { + options = { + arrows: true, + keep_fargs: false, + reduce_vars: true, + rests: true, + } + input: { + console.log(((...[ a ]) => a)("PASS")); + } + expect: { + console.log((a => a)("PASS")); + } + expect_stdout: "PASS" + node_version: ">=6" +} + +drop_rest_lambda: { + options = { + keep_fargs: false, + reduce_vars: true, + rests: true, + toplevel: true, + } + input: { + function f(...[ a ]) { + return a; + } + console.log(f("PASS"), f(42)); + } + expect: { + function f(a) { + return a; + } + console.log(f("PASS"), f(42)); + } + expect_stdout: "PASS 42" + node_version: ">=6" +} + issue_4525_1: { options = { arguments: true, |