aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2020-12-24 02:56:22 +0000
committerGitHub <noreply@github.com>2020-12-24 10:56:22 +0800
commit5f269cd57356fc4dbe7486dca22902d22695d499 (patch)
tree1a7ac6838449a7b154d411547d75be784c361aa2
parent6988cd95584ce8b66716c17afad9d2dcc71717d0 (diff)
downloadtracifyjs-5f269cd57356fc4dbe7486dca22902d22695d499.tar.gz
tracifyjs-5f269cd57356fc4dbe7486dca22902d22695d499.zip
fix corner case in `collapse_vars` (#4445)
fixes #4444
-rw-r--r--README.md2
-rw-r--r--lib/compress.js14
-rw-r--r--test/compress/default-values.js24
3 files changed, 36 insertions, 4 deletions
diff --git a/README.md b/README.md
index 4be02b5b..59aec09b 100644
--- a/README.md
+++ b/README.md
@@ -638,6 +638,8 @@ to be `false` and all symbol names will be omitted.
- `dead_code` (default: `true`) -- remove unreachable code
+- `default_values` (default: `true`) -- drop overshadowed default values
+
- `directives` (default: `true`) -- remove redundant or non-standard directives
- `drop_console` (default: `false`) -- Pass `true` to discard calls to
diff --git a/lib/compress.js b/lib/compress.js
index 4351768d..a611bf4a 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -1814,16 +1814,22 @@ merge(Compressor.prototype, {
}
if (!(fn instanceof AST_Lambda)) return true;
if (def && recursive_ref(compressor, def)) return true;
- if (!all(fn.argnames, function(argname) {
- return !(argname instanceof AST_Destructured);
- })) return true;
if (fn.collapse_scanning) return false;
fn.collapse_scanning = true;
var replace = can_replace;
can_replace = false;
var after = stop_after;
var if_hit = stop_if_hit;
- if (fn instanceof AST_Arrow && fn.value) {
+ if (!all(fn.argnames, function(argname) {
+ if (argname instanceof AST_DefaultValue) {
+ argname.value.transform(scanner);
+ if (abort) return false;
+ argname = argname.name;
+ }
+ return !(argname instanceof AST_Destructured);
+ })) {
+ abort = true;
+ } else if (fn instanceof AST_Arrow && fn.value) {
fn.value.transform(scanner);
} else for (var i = 0; !abort && i < fn.body.length; i++) {
var stat = fn.body[i];
diff --git a/test/compress/default-values.js b/test/compress/default-values.js
index 2e046444..64dad3cf 100644
--- a/test/compress/default-values.js
+++ b/test/compress/default-values.js
@@ -950,3 +950,27 @@ mangle_arrow_2_toplevel: {
expect_stdout: "PASS"
node_version: ">=6"
}
+
+issue_4444: {
+ options = {
+ collapse_vars: true,
+ }
+ input: {
+ var a = "PASS";
+ console.log(function(b) {
+ b = a;
+ (function(c = b.p) {})();
+ return a;
+ }());
+ }
+ expect: {
+ var a = "PASS";
+ console.log(function(b) {
+ b = a;
+ (function(c = b.p) {})();
+ return a;
+ }());
+ }
+ expect_stdout: "PASS"
+ node_version: ">=6"
+}