Skip to content

Commit

Permalink
fix corner case in collapse_vars (#5261)
Browse files Browse the repository at this point in the history
fixes #5260
  • Loading branch information
alexlamsl committed Jan 3, 2022
1 parent dec359c commit c94624f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/compress.js
Expand Up @@ -2051,7 +2051,7 @@ Compressor.prototype.compress = function(node) {
if (node instanceof AST_BlockScope
&& !(node instanceof AST_Scope)
&& !(node.variables && node.variables.all(function(def) {
return !lvalues.has(def.name);
return !enclosed.has(def.name) && !lvalues.has(def.name);
}))) {
var replace = can_replace;
can_replace = false;
Expand Down Expand Up @@ -2149,6 +2149,7 @@ Compressor.prototype.compress = function(node) {
var read_toplevel = false;
var modify_toplevel = false;
// Locate symbols which may execute code outside of scanning range
var enclosed = new Dictionary();
var well_defined = true;
var lvalues = get_lvalues(candidate);
var lhs_local = is_lhs_local(lhs);
Expand Down Expand Up @@ -3010,6 +3011,9 @@ Compressor.prototype.compress = function(node) {
break;
}
}
node.enclosed.forEach(function(def) {
if (def.scope !== node) enclosed.set(def.name, true);
});
return true;
} else if (find_arguments && node instanceof AST_Sub) {
scope.each_argname(function(argname) {
Expand Down
35 changes: 35 additions & 0 deletions test/compress/const.js
Expand Up @@ -1752,3 +1752,38 @@ issue_5254: {
"bar",
]
}

issue_5260: {
options = {
collapse_vars: true,
}
input: {
"use strict";
var a = "foo", o;
while (console.log("bar"));
o = {
baz: function(b) {
console.log(a, b);
},
};
for (const a in o)
o[a](a);
}
expect: {
"use strict";
var a = "foo", o;
while (console.log("bar"));
o = {
baz: function(b) {
console.log(a, b);
},
};
for (const a in o)
o[a](a);
}
expect_stdout: [
"bar",
"foo baz",
]
node_version: ">=4"
}
35 changes: 35 additions & 0 deletions test/compress/let.js
Expand Up @@ -1906,3 +1906,38 @@ issue_5254: {
]
node_version: ">=4"
}

issue_5260: {
options = {
collapse_vars: true,
}
input: {
"use strict";
var a = "foo", o;
while (console.log("bar"));
o = {
baz: function(b) {
console.log(a, b);
},
};
for (let a in o)
o[a](a);
}
expect: {
"use strict";
var a = "foo", o;
while (console.log("bar"));
o = {
baz: function(b) {
console.log(a, b);
},
};
for (let a in o)
o[a](a);
}
expect_stdout: [
"bar",
"foo baz",
]
node_version: ">=4"
}

0 comments on commit c94624f

Please sign in to comment.