Skip to content

Commit

Permalink
fix corner case in functions (#5035)
Browse files Browse the repository at this point in the history
fixes #5034
  • Loading branch information
alexlamsl committed Jun 24, 2021
1 parent 82772cc commit 1a064b6
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -6562,10 +6562,25 @@ merge(Compressor.prototype, {
if (def.orig.length > 1) return null;
if (def.assignments > 0) return false;
if (def.name == name) return def;
if (name == "await" && is_async(fn)) return false;
if (name == "yield" && is_generator(fn)) return false;
var forbidden;
switch (name) {
case "await":
forbidden = is_async;
break;
case "yield":
forbidden = is_generator;
break;
}
return all(def.references, function(ref) {
return ref.scope.find_variable(name) === sym;
var scope = ref.scope;
if (scope.find_variable(name) !== sym) return false;
if (forbidden) {
scope = scope.resolve();
do {
if (forbidden(scope)) return false;
} while ((scope = scope.parent_scope.resolve()) && scope !== fn);
}
return true;
}) && def;
}

Expand Down
34 changes: 34 additions & 0 deletions test/compress/awaits.js
Original file line number Diff line number Diff line change
Expand Up @@ -1989,3 +1989,37 @@ issue_5032_webkit: {
]
node_version: ">=8"
}

issue_5034: {
options = {
functions: true,
reduce_vars: true,
unused: true,
}
input: {
(function() {
var await = function f() {
return async function() {
return f;
};
};
await()().then(function(value) {
console.log(value === await ? "PASS" : "FAIL");
});
})();
}
expect: {
(function() {
var await = function f() {
return async function() {
return f;
};
};
await()().then(function(value) {
console.log(value === await ? "PASS" : "FAIL");
});
})();
}
expect_stdout: "PASS"
node_version: ">=8"
}
30 changes: 30 additions & 0 deletions test/compress/yields.js
Original file line number Diff line number Diff line change
Expand Up @@ -1216,3 +1216,33 @@ issue_5032_webkit: {
]
node_version: ">=4"
}

issue_5034: {
options = {
functions: true,
reduce_vars: true,
unused: true,
}
input: {
console.log(function() {
var yield = function f() {
return function*() {
return f;
};
};
return yield()().next().value === yield;
}() ? "PASS" : "FAIL");
}
expect: {
console.log(function() {
var yield = function f() {
return function*() {
return f;
};
};
return yield()().next().value === yield;
}() ? "PASS" : "FAIL");
}
expect_stdout: "PASS"
node_version: ">=4"
}

0 comments on commit 1a064b6

Please sign in to comment.