Skip to content

Commit

Permalink
fix corner cases in collapse_vars & reduce_vars (#5916)
Browse files Browse the repository at this point in the history
fixes #5915
  • Loading branch information
alexlamsl authored Aug 12, 2024
1 parent 52b6852 commit 303417c
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 8 deletions.
10 changes: 2 additions & 8 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,7 @@ Compressor.prototype.compress = function(node) {
def(AST_LambdaDefinition, function(tw, descend, compressor) {
var fn = this;
var def = fn.name.definition();
if (!safe_to_trim(fn)) def.fixed = false;
var parent = tw.parent();
if (parent instanceof AST_ExportDeclaration || parent instanceof AST_ExportDefault) def.single_use = false;
if (!safe_to_visit(tw, fn)) return true;
Expand Down Expand Up @@ -3545,17 +3546,10 @@ Compressor.prototype.compress = function(node) {
if (node instanceof AST_Symbol) {
var def = node.definition();
var scope = def.scope.resolve();
var found = false;
var avoid = def.orig.reduce(function(scopes, sym) {
def.orig.forEach(function(sym) {
if (sym instanceof AST_SymbolDefun) {
if (sym.scope !== scope) push_uniq(scopes, sym.scope);
} else {
found = true;
}
return scopes;
}, []);
if (found) avoid.forEach(function(scope) {
push_uniq(scopes, scope);
});
}
});
Expand Down
78 changes: 78 additions & 0 deletions test/compress/collapse_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -10357,3 +10357,81 @@ issue_5869: {
}
expect_stdout: TypeError("Cannot set properties of undefined")
}

issue_5915_1: {
options = {
collapse_vars: true,
}
input: {
f = void 0;
{
console.log(typeof f);
function f() {}
}
}
expect: {
f = void 0;
{
console.log(typeof f);
function f() {}
}
}
expect_stdout: true
}

issue_5915_2: {
options = {
collapse_vars: true,
}
input: {
f = void 0;
{
function f() {}
console.log(typeof f);
}
}
expect: {
f = void 0;
{
function f() {}
console.log(typeof f);
}
}
expect_stdout: true
}

issue_5915_3: {
options = {
collapse_vars: true,
}
input: {
f = void 0;
function f() {}
{
console.log(typeof f);
}
}
expect: {
function f() {}
console.log(typeof (f = void 0));
}
expect_stdout: "undefined"
}

issue_5915_4: {
options = {
collapse_vars: true,
}
input: {
{
f = void 0;
function f() {}
console.log(typeof f);
}
}
expect: {
function f() {}
console.log(typeof (f = void 0));
}
expect_stdout: "undefined"
}
93 changes: 93 additions & 0 deletions test/compress/reduce_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -8493,3 +8493,96 @@ issue_5892: {
}
expect_stdout: "PASS"
}

issue_5915_1: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
f = void 0;
if (console) {
console.log(typeof f);
function f() {}
}
}
expect: {
f = void 0;
if (console) {
console.log(typeof f);
function f() {}
}
}
expect_stdout: true
}

issue_5915_2: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
f = void 0;
if (console) {
function f() {}
console.log(typeof f);
}
}
expect: {
f = void 0;
if (console) {
function f() {}
console.log(typeof f);
}
}
expect_stdout: true
}

issue_5915_3: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
f = void 0;
function f() {}
if (console) {
console.log(typeof f);
}
}
expect: {
void 0;
if (console)
console.log("undefined");
}
expect_stdout: "undefined"
}

issue_5915_4: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
if (console) {
f = void 0;
function f() {}
console.log(typeof f);
}
}
expect: {
if (console) {
void 0;
console.log("undefined");
}
}
expect_stdout: "undefined"
}

0 comments on commit 303417c

Please sign in to comment.