Skip to content

Commit

Permalink
fix corner case in inline (#5886)
Browse files Browse the repository at this point in the history
fixes #5884
  • Loading branch information
alexlamsl authored Jul 15, 2024
1 parent b6b0658 commit 722465b
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,7 @@ Compressor.prototype.compress = function(node) {
function reset_flags(node) {
node._squeezed = false;
node._optimized = false;
node.single_use = false;
if (node instanceof AST_BlockScope) node._var_names = undefined;
if (node instanceof AST_SymbolRef) node.fixed = undefined;
}
Expand Down Expand Up @@ -14178,6 +14179,7 @@ Compressor.prototype.compress = function(node) {
var fn = call.expression;
if (!(fn instanceof AST_LambdaExpression)) return;
if (fn.name) return;
if (fn.single_use) return;
if (fn.uses_arguments) return;
if (fn.pinned()) return;
if (is_generator(fn)) return;
Expand Down
5 changes: 2 additions & 3 deletions test/compress/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8706,10 +8706,9 @@ single_use_inline_collision: {
expect: {
var a = "PASS";
(function() {
(function() {
void function() {
while (console.log(a));
return;
})();
}();
(function(a) {
a || a("FAIL");
})(console.log);
Expand Down
68 changes: 68 additions & 0 deletions test/compress/hoist_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -801,3 +801,71 @@ issue_5638_4: {
}
expect_stdout: "foo 42"
}

issue_5884_1: {
options = {
hoist_vars: true,
inline: true,
join_vars: true,
reduce_vars: true,
sequences: true,
toplevel: true,
unused: true,
}
input: {
try {
var f = function() {
var a = [ "PASS" ];
for (b in a)
console.log(a[b]);
};
f();
} finally {
var b;
}
}
expect: {
var b;
try {
(function() {
var a = [ "PASS" ];
for (b in a)
console.log(a[b]);
})();
} finally {}
}
expect_stdout: "PASS"
}

issue_5884_2: {
options = {
hoist_vars: true,
inline: true,
join_vars: true,
passes: 2,
reduce_vars: true,
sequences: true,
toplevel: true,
unused: true,
}
input: {
try {
var f = function() {
var a = [ "PASS" ];
for (b in a)
console.log(a[b]);
};
f();
} finally {
var b;
}
}
expect: {
try {
var a = [ "PASS" ];
for (var b in a)
console.log(a[b]);
} finally {}
}
expect_stdout: "PASS"
}
54 changes: 52 additions & 2 deletions test/compress/join_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ inlined_assignments: {
expect_stdout: "PASS"
}

inline_for: {
single_use_for: {
options = {
inline: true,
join_vars: true,
Expand All @@ -683,16 +683,66 @@ inline_for: {
};
a();
}
expect: {
(function() {
for (; console.log("PASS"););
})();
}
expect_stdout: "PASS"
}

single_use_for_inline: {
options = {
inline: true,
join_vars: true,
passes: 2,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
var a = function() {
for (; console.log("PASS"););
};
a();
}
expect: {
for (; console.log("PASS"););
}
expect_stdout: "PASS"
}

inline_var: {
single_use_var: {
options = {
inline: true,
join_vars: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
A = "PASS";
var a = function() {
var b = A;
for (b in console.log(b));
};
a();
}
expect: {
A = "PASS";
(function() {
var b = A;
for (b in console.log(b));
})();
}
expect_stdout: "PASS"
}

single_use_var_inline: {
options = {
inline: true,
join_vars: true,
passes: 2,
reduce_vars: true,
toplevel: true,
unused: true,
Expand Down

0 comments on commit 722465b

Please sign in to comment.