diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-11-24 07:26:22 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-24 07:26:22 +0800 |
commit | eb001dc1d9efbb81841410c06d854091473061ee (patch) | |
tree | 023b1b9608276d6bb34aa6b5cf4285b91f7b7bba | |
parent | aa9bdf416e5b288bbae8417780abf2f235bacfd4 (diff) | |
download | tracifyjs-eb001dc1d9efbb81841410c06d854091473061ee.tar.gz tracifyjs-eb001dc1d9efbb81841410c06d854091473061ee.zip |
fix argument/atom collision by `collapse_vars` (#2507)
fixes #2506
-rw-r--r-- | lib/compress.js | 24 | ||||
-rw-r--r-- | test/compress/collapse_vars.js | 38 |
2 files changed, 55 insertions, 7 deletions
diff --git a/lib/compress.js b/lib/compress.js index ce881c05..d8a0bd7c 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -806,6 +806,12 @@ merge(Compressor.prototype, { } } + function is_identifier_atom(node) { + return node instanceof AST_Infinity + || node instanceof AST_NaN + || node instanceof AST_Undefined; + } + function tighten_body(statements, compressor) { var CHANGED, max_iter = 10; do { @@ -890,14 +896,19 @@ merge(Compressor.prototype, { return node; } var def = candidate.name.definition(); + var value = candidate.value; if (def.references.length - def.replaced == 1 && !compressor.exposed(def)) { def.replaced++; - return maintain_this_binding(parent, node, candidate.value); + if (funarg && is_identifier_atom(value)) { + return value.transform(compressor); + } else { + return maintain_this_binding(parent, node, value); + } } return make_node(AST_Assign, candidate, { operator: "=", left: make_node(AST_SymbolRef, candidate.name, candidate.name), - right: candidate.value + right: value }); } candidate.write_only = false; @@ -971,7 +982,8 @@ merge(Compressor.prototype, { replace_all = def.references.length - def.replaced == 1; } var side_effects = value_has_side_effects(candidate); - var hit = candidate.name instanceof AST_SymbolFunarg; + var funarg = candidate.name instanceof AST_SymbolFunarg; + var hit = funarg; var abort = false, replaced = 0, can_replace = !args || !hit; if (!can_replace) { for (var j = compressor.self().argnames.lastIndexOf(candidate.name) + 1; !abort && j < args.length; j++) { @@ -987,7 +999,7 @@ merge(Compressor.prototype, { if (abort && def.references.length - def.replaced > replaced) replaced = false; else { abort = false; - hit = candidate.name instanceof AST_SymbolFunarg; + hit = funarg; for (var i = stat_index; !abort && i < statements.length; i++) { statements[i].transform(multi_replacer); } @@ -3810,9 +3822,7 @@ merge(Compressor.prototype, { if (self.operator == "delete" && !(e instanceof AST_SymbolRef || e instanceof AST_PropAccess - || e instanceof AST_NaN - || e instanceof AST_Infinity - || e instanceof AST_Undefined)) { + || is_identifier_atom(e))) { if (e instanceof AST_Sequence) { e = e.expressions.slice(); e.push(make_node(AST_True, self)); diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index e89d44df..844d5b0f 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -3618,3 +3618,41 @@ issue_2497: { } } } + +issue_2506: { + options = { + collapse_vars: true, + reduce_vars: true, + unused: true, + } + input: { + var c = 0; + function f0(bar) { + function f1(Infinity_2) { + function f13(NaN) { + if (false <= NaN & this >> 1 >= 0) { + c++; + } + } + var b_2 = f13(NaN, c++); + } + var bar = f1(-3, -1); + } + f0(false); + console.log(c); + } + expect: { + var c = 0; + function f0(bar) { + (function(Infinity_2) { + (function(NaN) { + if (false <= 0/0 & this >> 1 >= 0) + c++; + })(0, c++); + })(); + } + f0(false); + console.log(c); + } + expect_stdout: "1" +} |