Visitar URL original
Fix transformation of nested return statements in "convert to async function" quick fix by uhyo · Pull Request #41014 · microsoft/TypeScript · GitHub
The Wayback Machine - https://web.archive.org/web/20221224173159/https://github.com/microsoft/TypeScript/pull/41014
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

Fix transformation of nested return statements in "convert to async function" quick fix #41014

Closed
wants to merge 2 commits into from

Conversation

uhyo
Copy link
Contributor

@uhyo uhyo commented Oct 9, 2020 •

Happy hacktoberfest! 🎃🥳

Fixes #40040.

Currently converting return statements that are not at the top level of then callback function results in a broken result.
With this PR, these kinds of functions are correctly converted using the IIFE pattern.

Example

Code Before "convert to async function"

function getTheNumber2(): Promise<number> {
  return Promise.resolve()
    .then(() => {
        if (true) {
            return 1;
        }
    })
    .then(function name(foo) {
        while (true) {
            return Promise.resolve(3);
        }
    })
    .then(() => {
        if (true) {
            return Promise.resolve(123)
        }
    })
    .then((bar) => {
        if (true) {
            return 2;
        }
    });
}

Current Behavior (v4.1.0-dev.20201009)

async function getTheNumber2(): Promise<number> {
  await Promise.resolve();
if(true) {
return 1;
}
const foo=undefined;
while(true) {
return Promise.resolve(3);
}
if(true) {
return Promise.resolve(123);
}
const bar=undefined;
if(true) {
return 2;
}
}

New Behavior

async function getTheNumber2(): Promise<number> {
  await Promise.resolve();
  const foo = (() => {
    if (true) {
      return 1;
    }
  })();
  await function name() {
    while (true) {
      return Promise.resolve(3);
    }
  } ();
  const bar = await (() => {
    if (true) {
      return Promise.resolve(123);
    }
  })();
  if (true) {
    return 2;
  }
}

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Oct 9, 2020
@sandersn sandersn added this to Not started in PR Backlog Oct 20, 2020
@sandersn sandersn moved this from Not started to Needs review in PR Backlog Oct 20, 2020
@andrewbranch
Copy link
Member

andrewbranch commented Oct 20, 2020

I would lean toward disabling the refactor when this pattern is encountered. The original Promise code mimics a believable code structure, but I can’t imagine anyone ever writing code that looks like this output. We have found a good number of cases where the equivalent async code just looks bizarre, and we’ve so far been comfortable with not offering to do the conversion.

@andrewbranch
Copy link
Member

andrewbranch commented Apr 5, 2021

@uhyo are you interested in continuing to work on this PR, or should we close it?

@uhyo
Copy link
Contributor Author

uhyo commented Apr 6, 2021

Thank you for mention. I understand that disabling the refactor would be a better solution for this, so let me close this PR.

@uhyo uhyo closed this Apr 6, 2021
PR Backlog automation moved this from Needs review to Done Apr 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
PR Backlog
  
Done
Development

Successfully merging this pull request may close these issues.

"Convert to async function" generates incorrect code for nested return statements
3 participants