Skip to content

Commit

Permalink
fix corner case in collapse_vars (#3527)
Browse files Browse the repository at this point in the history
fixes #3526
  • Loading branch information
alexlamsl committed Oct 25, 2019
1 parent 27b159e commit 85237b0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/compress.js
Expand Up @@ -1228,6 +1228,7 @@ merge(Compressor.prototype, {
var scan_lhs = lhs && !side_effects && !is_lhs_read_only(lhs, compressor);
var scan_rhs = foldable(get_rhs(candidate));
if (!scan_lhs && !scan_rhs) continue;
var modify_toplevel = false;
// Locate symbols which may execute code outside of scanning range
var lvalues = get_lvalues(candidate);
var lhs_local = is_lhs_local(lhs);
Expand Down Expand Up @@ -1580,7 +1581,16 @@ merge(Compressor.prototype, {
if (candidate instanceof AST_VarDef) {
lvalues[candidate.name.name] = lhs;
}
var scan_iife = scope instanceof AST_Toplevel;
var tw = new TreeWalker(function(node) {
if (scan_iife && node.TYPE == "Call") {
var exp = node.expression;
if (exp instanceof AST_PropAccess) return;
if (exp instanceof AST_Function && !exp.contains_this()) return;
modify_toplevel = true;
scan_iife = false;
return;
}
var value;
if (node instanceof AST_SymbolRef) {
value = node.fixed_value() || node;
Expand Down Expand Up @@ -1667,6 +1677,7 @@ merge(Compressor.prototype, {
var def = sym.definition();
if (def.orig.length == 1 && def.orig[0] instanceof AST_SymbolDefun) return false;
if (def.scope !== scope) return true;
if (modify_toplevel && compressor.exposed(def)) return true;
return !all(def.references, function(ref) {
return ref.scope.resolve() === scope;
});
Expand Down
52 changes: 52 additions & 0 deletions test/compress/collapse_vars.js
Expand Up @@ -6296,3 +6296,55 @@ issue_3520: {
}
expect_stdout: "2"
}

issue_3526_1: {
options = {
collapse_vars: true,
}
input: {
var b = function() {
this.a = "FAIL";
}();
var a = "PASS";
var b;
var c = b;
console.log(a);
}
expect: {
var b = function() {
this.a = "FAIL";
}();
var a = "PASS";
var b;
var c = b;
console.log(a);
}
expect_stdout: "PASS"
}

issue_3526_2: {
options = {
collapse_vars: true,
}
input: {
function f() {
this.a = "FAIL";
}
var b = f();
var a = "PASS";
var b;
var c = b;
console.log(a);
}
expect: {
function f() {
this.a = "FAIL";
}
var b = f();
var a = "PASS";
var b;
var c = b;
console.log(a);
}
expect_stdout: "PASS"
}

0 comments on commit 85237b0

Please sign in to comment.