Skip to content

Commit

Permalink
fix corner case in reduce_vars (#4934)
Browse files Browse the repository at this point in the history
fixes #4933
  • Loading branch information
alexlamsl committed May 14, 2021
1 parent 7576048 commit 2cff7c9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -6096,7 +6096,7 @@ merge(Compressor.prototype, {
return !compressor.exposed(def) && def.references.length == def.replaced;
} : function(def) {
if (!(def.id in in_use_ids)) return true;
if (def.orig.length < 2) return false;
if (def.orig.length - def.eliminated < 2) return false;
// function argument will always overshadow its name
if (def.orig[1] instanceof AST_SymbolFunarg) return true;
// retain if referenced within destructured object of argument
Expand Down Expand Up @@ -8551,7 +8551,7 @@ merge(Compressor.prototype, {
def.scope = scope;
scope.variables.set(def.name, def);
}),
value: defn.value
value: defn.value,
});
})
});
Expand Down Expand Up @@ -10672,6 +10672,14 @@ merge(Compressor.prototype, {
lambda_def.recursive_refs = def.recursive_refs;
}
value.walk(new TreeWalker(function(node) {
if (node instanceof AST_SymbolDeclaration) {
if (node !== name) {
var def = node.definition();
def.orig.push(node);
def.eliminated++;
}
return;
}
if (!(node instanceof AST_SymbolRef)) return;
var def = node.definition();
if (def === defun_def) {
Expand Down
52 changes: 52 additions & 0 deletions test/compress/varify.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,55 @@ default_init: {
expect_stdout: "PASS"
node_version: ">=4"
}

issue_4933_1: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
varify: true,
}
input: {
console.log(f());
function f() {
var a;
for (console in a = [ f ]) {
const b = a;
}
}
}
expect: {
console.log(function f() {
var a;
for (console in a = [ f ]) {
const b = a;
}
}());
}
expect_stdout: "undefined"
}

issue_4933_2: {
options = {
passes: 2,
reduce_vars: true,
toplevel: true,
unused: true,
varify: true,
}
input: {
console.log(f());
function f() {
var a;
for (console in a = [ f ]) {
const b = a;
}
}
}
expect: {
console.log(function f() {
for (console in [ f ]);
}());
}
expect_stdout: "undefined"
}

0 comments on commit 2cff7c9

Please sign in to comment.