diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-01-24 03:00:47 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-24 11:00:47 +0800 |
commit | a36c5472d2cf9f79ca5245e9299a011ec7e4cef1 (patch) | |
tree | 1a32877a16ab2d10a89a619521ef1185a7964406 /lib/compress.js | |
parent | 8bfd891c09edfb54d1b93010487ee68ad64a457c (diff) | |
download | tracifyjs-a36c5472d2cf9f79ca5245e9299a011ec7e4cef1.tar.gz tracifyjs-a36c5472d2cf9f79ca5245e9299a011ec7e4cef1.zip |
fix corner cases with default parameters (#4589)
fixes #4588
Diffstat (limited to 'lib/compress.js')
-rw-r--r-- | lib/compress.js | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/lib/compress.js b/lib/compress.js index 6b8a7ef9..e5ee7406 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3817,6 +3817,14 @@ merge(Compressor.prototype, { return skip_directives(this.body); }); + AST_Lambda.DEFMETHOD("length", function() { + var argnames = this.argnames; + for (var i = 0; i < argnames.length; i++) { + if (argnames[i] instanceof AST_DefaultValue) break; + } + return i; + }); + function try_evaluate(compressor, node) { var ev = node.evaluate(compressor); if (ev === node) return node; @@ -4277,7 +4285,7 @@ merge(Compressor.prototype, { case "name": return val.node.name ? val.node.name.name : ""; case "length": - return val.node.argnames.length; + return val.node.length(); default: return this; } @@ -5705,19 +5713,33 @@ merge(Compressor.prototype, { unused_fn_names.push(node); } if (!(node instanceof AST_Accessor)) { + if (node.rest) { + node.rest = node.rest.transform(trimmer); + if (!(node.uses_arguments && !tt.has_directive("use strict")) + && (node.rest instanceof AST_DestructuredArray && node.rest.elements.length == 0 + || node.rest instanceof AST_DestructuredObject && node.rest.properties.length == 0)) { + node.rest = null; + } + } + var argnames = node.argnames; var trim = compressor.drop_fargs(node, parent) && !node.rest; - for (var a = node.argnames, i = a.length; --i >= 0;) { - var sym = a[i]; + var default_length = trim ? -1 : node.length(); + for (var i = argnames.length; --i >= 0;) { + var sym = argnames[i]; if (!(sym instanceof AST_SymbolFunarg)) { var arg = sym.transform(trimmer); if (arg) { trim = false; } else if (trim) { - log(sym.name, "Dropping unused function argument {name}"); - a.pop(); - } else { + log(sym.name, "Dropping unused default argument {name}"); + argnames.pop(); + } else if (i > default_length) { + log(sym.name, "Dropping unused default argument assignment {name}"); sym.name.__unused = true; - a[i] = sym.name; + argnames[i] = sym.name; + } else { + log(sym.name, "Dropping unused default argument value {name}"); + sym.value = make_node(AST_Number, sym, { value: 0 }); } continue; } @@ -5727,19 +5749,11 @@ merge(Compressor.prototype, { if (indexOf_assign(def, sym) < 0) sym.__unused = null; } else if (trim) { log(sym, "Dropping unused function argument {name}"); - a.pop(); + argnames.pop(); } else { sym.__unused = true; } } - if (node.rest) { - node.rest = node.rest.transform(trimmer); - if (!(node.uses_arguments && !tt.has_directive("use strict")) - && (node.rest instanceof AST_DestructuredArray && node.rest.elements.length == 0 - || node.rest instanceof AST_DestructuredObject && node.rest.properties.length == 0)) { - node.rest = null; - } - } fns_with_marked_args.push(node); } } |