Skip to content

Commit

Permalink
fix corner case in merge_vars (#5715)
Browse files Browse the repository at this point in the history
fixes #5714
  • Loading branch information
alexlamsl committed Oct 17, 2022
1 parent 5a5200d commit 8319bad
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/compress.js
Expand Up @@ -6662,15 +6662,15 @@ Compressor.prototype.compress = function(node) {
do {
var head = first.shift();
if (tail.index > head.index) continue;
var id = head.definition.id;
if (!(id in prev)) continue;
var head_refs = references[id];
var prev_def = head.definition;
if (!(prev_def.id in prev)) continue;
var head_refs = references[prev_def.id];
if (!head_refs) continue;
if (head_refs.start.block !== tail_refs.start.block
|| !mergeable(head_refs, tail_refs)
|| (head_refs.start.loop || !same_scope(def)) && !mergeable(tail_refs, head_refs)
|| compressor.option("webkit") && is_funarg(def) !== is_funarg(head.definition)
|| head.definition.const_redefs
|| compressor.option("webkit") && is_funarg(def) !== is_funarg(prev_def)
|| prev_def.const_redefs
|| !all(head_refs.scopes, function(scope) {
return scope.find_variable(def.name) === def;
})) {
Expand All @@ -6682,12 +6682,14 @@ Compressor.prototype.compress = function(node) {
sym.name = def.name;
if (sym instanceof AST_SymbolRef) {
def.references.push(sym);
prev_def.replaced++;
} else {
def.orig.push(sym);
prev_def.eliminated++;
}
});
if (!head.definition.fixed) def.fixed = false;
merged[id] = def;
if (!prev_def.fixed) def.fixed = false;
merged[prev_def.id] = def;
changed = true;
break;
} while (first.length);
Expand Down
44 changes: 44 additions & 0 deletions test/compress/merge_vars.js
Expand Up @@ -3845,3 +3845,47 @@ issue_5471_2: {
}
expect_stdout: "PASS"
}

issue_5714: {
options = {
inline: true,
join_vars: true,
loops: true,
merge_vars: true,
unused: true,
}
input: {
"use strict";
console.log(function() {
var i = 1;
while (i--) {
var a = function f(b) {
console.log(b);
var c = function(d) {
console.log(typeof d);
}(console);
}();
var e = 42;
}
return e;
}());
}
expect: {
"use strict";
console.log(function() {
for (var i = 1; i--;) {
var b = void 0;
console.log(b);
b = console,
console.log(typeof b);
var e = 42;
}
return e;
}());
}
expect_stdout: [
"undefined",
"object",
"42",
]
}

0 comments on commit 8319bad

Please sign in to comment.