Skip to content

Commit

Permalink
fix argument/atom collision by collapse_vars (#2507)
Browse files Browse the repository at this point in the history
fixes #2506
  • Loading branch information
alexlamsl committed Nov 23, 2017
1 parent aa9bdf4 commit eb001dc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
24 changes: 17 additions & 7 deletions lib/compress.js
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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++) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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));
Expand Down
38 changes: 38 additions & 0 deletions test/compress/collapse_vars.js
Expand Up @@ -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"
}

0 comments on commit eb001dc

Please sign in to comment.