diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-10-12 16:10:32 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-12 23:10:32 +0800 |
commit | 4d33cb2f94d2a9326c67032836f8fa3d4c0731f8 (patch) | |
tree | a5dfde9ef96880a0483e6acce931db10617135a3 | |
parent | 00d0eda85baba441e8aaac056b6f32b12e79dd6e (diff) | |
download | tracifyjs-4d33cb2f94d2a9326c67032836f8fa3d4c0731f8.tar.gz tracifyjs-4d33cb2f94d2a9326c67032836f8fa3d4c0731f8.zip |
fix corner case in `inilne` (#4204)
fixes #4202
-rw-r--r-- | lib/compress.js | 25 | ||||
-rw-r--r-- | test/compress/const.js | 32 |
2 files changed, 45 insertions, 12 deletions
diff --git a/lib/compress.js b/lib/compress.js index 3464a232..f6de8656 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -7198,24 +7198,24 @@ merge(Compressor.prototype, { return defined[name] || identifier_atom[name] || scope.var_names()[name]; } - function can_inject_args(catches, used, safe_to_inject) { + function can_inject_args(defined, used, safe_to_inject) { for (var i = 0; i < fn.argnames.length; i++) { var arg = fn.argnames[i]; if (arg.__unused) continue; - if (!safe_to_inject || var_exists(catches, arg.name)) return false; + if (!safe_to_inject || var_exists(defined, arg.name)) return false; used[arg.name] = true; if (in_loop) in_loop.push(arg.definition()); } return true; } - function can_inject_vars(catches, used, safe_to_inject) { + function can_inject_vars(defined, used, safe_to_inject) { for (var i = 0; i < fn.body.length; i++) { var stat = fn.body[i]; if (stat instanceof AST_Defun) { if (!safe_to_inject || var_exists(used, stat.name.name)) return false; if (!all(stat.enclosed, function(def) { - return def.scope === stat || !catches[def.name]; + return def.scope === stat || !defined[def.name]; })) return false; continue; } @@ -7223,7 +7223,7 @@ merge(Compressor.prototype, { if (!safe_to_inject) return false; for (var j = stat.definitions.length; --j >= 0;) { var name = stat.definitions[j].name; - if (var_exists(catches, name.name)) return false; + if (var_exists(defined, name.name)) return false; if (in_loop) in_loop.push(name.definition()); } } @@ -7231,15 +7231,16 @@ merge(Compressor.prototype, { } function can_inject_symbols() { - var catches = Object.create(null); + var defined = Object.create(null); var child; scope = compressor.self(); do { child = scope; scope = compressor.parent(++level); - if (scope instanceof AST_Catch) { - if (scope.argname) catches[scope.argname.name] = true; - } else if (scope instanceof AST_DWLoop) { + if (scope.variables) scope.variables.each(function(def) { + defined[def.name] = true; + }); + if (scope instanceof AST_DWLoop) { in_loop = []; } else if (scope instanceof AST_For) { if (scope.init === child) continue; @@ -7255,9 +7256,9 @@ merge(Compressor.prototype, { var safe_to_inject = (!(scope instanceof AST_Toplevel) || compressor.toplevel.vars) && (exp !== fn || fn.parent_scope.resolve() === compressor.find_parent(AST_Scope)); var inline = compressor.option("inline"); - var used = Object.create(catches); - if (!can_inject_args(catches, used, inline >= 2 && safe_to_inject)) return false; - if (!can_inject_vars(catches, used, inline >= 3 && safe_to_inject)) return false; + var used = Object.create(defined); + if (!can_inject_args(defined, used, inline >= 2 && safe_to_inject)) return false; + if (!can_inject_vars(defined, used, inline >= 3 && safe_to_inject)) return false; return !in_loop || in_loop.length == 0 || !is_reachable(fn, in_loop); } diff --git a/test/compress/const.js b/test/compress/const.js index 72033f4a..28dda019 100644 --- a/test/compress/const.js +++ b/test/compress/const.js @@ -840,3 +840,35 @@ issue_4198: { } expect_stdout: "PASS" } + +issue_4202: { + options = { + inline: true, + toplevel: true, + } + input: { + { + const o = {}; + (function() { + function f() { + o.p = 42; + } + f(f); + })(); + console.log(o.p++); + } + } + expect: { + { + const o = {}; + (function() { + function f() { + o.p = 42; + } + f(f); + })(); + console.log(o.p++); + } + } + expect_stdout: "42" +} |