Skip to content

Commit

Permalink
fix corner case in reduce_vars (#5121)
Browse files Browse the repository at this point in the history
fixes #5120
  • Loading branch information
alexlamsl authored Aug 25, 2021
1 parent db94d21 commit c3aef23
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
21 changes: 10 additions & 11 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -11231,21 +11231,20 @@ merge(Compressor.prototype, {
if (!(node instanceof AST_SymbolRef)) return;
var def = node.definition();
if (def === defun_def) {
node.thedef = lambda_def;
lambda_def.references.push(node);
node.thedef = def = lambda_def;
} else {
def.single_use = false;
var fn = node.fixed_value();
if (!is_lambda(fn)) return;
if (!fn.name) return;
if (fn.name.definition() !== def) return;
if (def.scope !== fn.name.scope) return;
if (fixed.variables.get(fn.name.name) !== def) return;
fn.name = fn.name.clone();
var value_def = value.variables.get(fn.name.name) || value[def_fn_name](fn.name);
node.thedef = value_def;
value_def.references.push(node);
if (is_lambda(fn)
&& fn.name
&& fn.name.definition() === def
&& def.scope === fn.name.scope
&& fixed.variables.get(fn.name.name) === def) {
fn.name = fn.name.clone();
node.thedef = def = value.variables.get(fn.name.name) || value[def_fn_name](fn.name);
}
}
def.references.push(node);
}));
} else {
if (fixed instanceof AST_Scope) {
Expand Down
4 changes: 2 additions & 2 deletions test/compress/exponentiation.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ issue_4664: {
expect: {
(function f() {
new function(a) {
console.log(typeof f, 1073741824, typeof this);
}(A = 0);
console.log(typeof f, a, typeof this);
}((A = 0, 2 ** 30));
})();
}
expect_stdout: "function 1073741824 object"
Expand Down
29 changes: 29 additions & 0 deletions test/compress/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6600,3 +6600,32 @@ shorter_without_void: {
"baz",
]
}

issue_5120: {
options = {
functions: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
var a = function f() {
function g() {
f || g();
}
g();
return f.valueOf();
};
console.log(a() === a ? "PASS" : "FAIL");
}
expect: {
function a() {
(function g() {
a || g();
})();
return a.valueOf();
}
console.log(a() === a ? "PASS" : "FAIL");
}
expect_stdout: "PASS"
}

0 comments on commit c3aef23

Please sign in to comment.