Skip to content

Commit

Permalink
fix corner case in hoist_vars (#4894)
Browse files Browse the repository at this point in the history
fixes #4893
  • Loading branch information
alexlamsl committed May 1, 2021
1 parent 6ab26ee commit fb03561
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lib/compress.js
Expand Up @@ -1793,7 +1793,7 @@ merge(Compressor.prototype, {
return make_node(AST_Assign, candidate, {
operator: "=",
left: make_node(AST_SymbolRef, candidate.name, candidate.name),
right: candidate.value
right: candidate.value,
});
}
var assign = candidate;
Expand Down Expand Up @@ -8433,6 +8433,7 @@ merge(Compressor.prototype, {
if (value) {
if (value instanceof AST_Sequence) value = value.clone();
var name = make_node(AST_SymbolRef, defn.name, defn.name);
name.fixed = value;
a.push(make_node(AST_Assign, defn, {
operator: "=",
left: name,
Expand Down
78 changes: 78 additions & 0 deletions test/compress/hoist_vars.js
Expand Up @@ -295,3 +295,81 @@ issue_4859: {
}
expect_stdout: "Infinity"
}

issue_4893_1: {
options = {
collapse_vars: true,
evaluate: true,
hoist_vars: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
function f() {
function g() {}
var a = null;
var b = null;
var c = null;
b.p += a = 42;
f;
}
try {
f();
} catch (e) {
console.log("PASS");
}
}
expect: {
try{
(function f() {
var b;
b = null;
b.p += 42;
f;
})();
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
}

issue_4893_2: {
options = {
collapse_vars: true,
hoist_vars: true,
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
toplevel: true,
unused: true,
}
input: {
function f() {
function g() {}
var a = null;
var b = null;
var c = null;
b.p += a = 42;
f;
}
try {
f();
} catch (e) {
console.log("PASS");
}
}
expect: {
try{
(function() {
var b;
b = null;
b.p += 42;
})();
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
}
12 changes: 6 additions & 6 deletions test/compress/pure_funcs.js
Expand Up @@ -133,7 +133,7 @@ conditional: {
relational: {
options = {
pure_funcs: [ "foo" ],
side_effects :true,
side_effects: true,
}
input: {
foo() in new foo();
Expand All @@ -158,7 +158,7 @@ relational: {
arithmetic: {
options = {
pure_funcs: [ "foo" ],
side_effects :true,
side_effects: true,
}
input: {
foo() + foo();
Expand All @@ -183,7 +183,7 @@ arithmetic: {
boolean_and: {
options = {
pure_funcs: [ "foo" ],
side_effects :true,
side_effects: true,
}
input: {
foo() && foo();
Expand All @@ -208,7 +208,7 @@ boolean_and: {
boolean_or: {
options = {
pure_funcs: [ "foo" ],
side_effects :true,
side_effects: true,
}
input: {
foo() || foo();
Expand All @@ -233,7 +233,7 @@ boolean_or: {
assign: {
options = {
pure_funcs: [ "foo" ],
side_effects :true,
side_effects: true,
}
input: {
var a;
Expand All @@ -256,7 +256,7 @@ assign: {
unary: {
options = {
pure_funcs: [ "foo" ],
side_effects :true,
side_effects: true,
}
input: {
typeof foo();
Expand Down
2 changes: 1 addition & 1 deletion test/compress/templates.js
Expand Up @@ -343,7 +343,7 @@ issue_4676: {
reduce_vars: true,
templates: true,
toplevel: true,
unsafe:true,
unsafe: true,
unused: true,
}
input: {
Expand Down

0 comments on commit fb03561

Please sign in to comment.