Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep iife assignment in return statement for async/generators #1073

Merged
merged 1 commit into from Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/compress/index.js
Expand Up @@ -3587,7 +3587,15 @@ function is_reachable(self, defs) {
if (node instanceof AST_Scope && node !== self) {
var parent = info.parent();

if (parent instanceof AST_Call && parent.expression === node) return;
if (
parent instanceof AST_Call
&& parent.expression === node
// Async/Generators aren't guaranteed to sync evaluate all of
// their body steps, so it's possible they close over the variable.
&& !(node.async || node.is_generator)
) {
return;
}

if (walk(node, find_ref)) return walk_abort;

Expand Down
154 changes: 154 additions & 0 deletions test/compress/dead-code.js
Expand Up @@ -1253,3 +1253,157 @@ issue_718: {
export {y}
}
}

issue_1029_1: {
options = {
dead_code: true,
}
input: {
function asyncFn() {
let promise;
return promise = (async () => {
await true;
console.log(promise);
})()
}
asyncFn({});
}
expect: {
function asyncFn() {
let promise;
return promise = (async () => {
await true;
console.log(promise);
})();
}
asyncFn({});
}
}

issue_1029_2: {
options = {
dead_code: true,
}
input: {
function asyncFn() {
let promise;
return promise = (async () => {
console.log(promise);
})()
}
asyncFn({});
}
expect: {
function asyncFn() {
let promise;
return promise = (async () => {
console.log(promise);
})();
}
asyncFn({});
}
}

issue_1029_3: {
options = {
dead_code: true,
}
input: {
function genFn() {
let gen;
return gen = function*() {
console.log(gen);
}();
}
genFn({}).next();
}
expect: {
function genFn() {
let gen;
return gen = function* () {
console.log(gen);
}();
}
genFn({}).next();
}
}

issue_1029_4: {
options = {
dead_code: true,
}
input: {
function fn() {
let val
return val = function() {
console.log(val);
return {};
}();
}
fn();
}
expect: {
function fn() {
let val
return function() {
console.log(val);
return {};
}();
}
fn();
}
}

issue_1029_5: {
options = {
dead_code: true,
}
input: {
function fn() {
let val
return val = function() {
setTimeout(() => console.log(val));
return {};
}();
}
fn();
}
expect: {
function fn() {
let val
return val = function() {
setTimeout(() => console.log(val));
return {};
}();
}
fn();
}
}

issue_1029_6: {
options = {
dead_code: true,
}
input: {
function fn() {
let val
return val = function() {
setTimeout(() => {
(() => console.log(val))();
})
return {};
}();
}
fn();
}
expect: {
function fn() {
let val
return val = function() {
setTimeout(() => console.log(val));
return {};
}();
}
fn();
}
}