Skip to content

Commit

Permalink
Keep iife assignment in return statement for async/generators (#1073)
Browse files Browse the repository at this point in the history
Fixes #1029
  • Loading branch information
jridgewell committed Sep 22, 2021
1 parent 3202410 commit ad494a7
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 1 deletion.
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();
}
}

0 comments on commit ad494a7

Please sign in to comment.