8000 Bug: [return-await] Gives false positive with unknown values · Issue #9903 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

Bug: [return-await] Gives false positive with unknown values #9903

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

Closed
4 tasks done
danny-pflughoeft opened this issue Aug 29, 2024 · 3 comments · Fixed by #10069
Closed
4 tasks done

Bug: [return-await] Gives false positive with unknown values #9903

danny-pflughoeft opened this issue Aug 29, 2024 · 3 comments · Fixed by #10069
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working good first issue Good for newcomers locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@danny-pflughoeft
Copy link
danny-pflughoeft commented Aug 29, 2024

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

link

Repro Code

async function test(unknownParam: unknown) {
  try {
    return await unknownParam;
  } finally {
    console.log('In finally block')
  }
}

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/return-await": "error",
  },
};

Expected Result

Since unknownParam could be a Promise (we don't know), the correct code is return await unknownParam, so that the finally block will execute after the promise completes, not before.

Actual Result

The rule does not allow that, and forces you to write incorrect code.

@danny-pflughoeft danny-pflughoeft added bug Something isn't working package: eslint-plugi 8000 n Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Aug 29, 2024
@bradzacher bradzacher added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Aug 29, 2024
@kirkwaiblinger
Copy link
Member
kirkwaiblinger commented Aug 29, 2024

TBH my opinion is that we should just remove entirely the check for return awaiting non-thenables from the rule. It just duplicates the behavior in await thenable, but with drift in the logic.

if (!isThenable) {
if (isAwait) {
// any/unknown could be thenable; do not auto-fix
const useAutoFix = !(isTypeAnyType(type) || isTypeUnknownType(type));
context.report({
messageId: 'nonPromiseAwait',
node,
...fixOrSuggest(useAutoFix, {
messageId: 'nonPromiseAwait',
fix: fixer => removeAwait(fixer, node),
}),
});
}
return;
}

AwaitExpression(node): void {
const type = services.getTypeAtLocation(node.argument);
if (isTypeAnyType(type) || isTypeUnknownType(type)) {
return;
}
const originalNode = services.esTreeNodeToTSNodeMap.get(node);
if (!tsutils.isThenableType(checker, originalNode.expression, type)) {
context.report({
messageId: 'await',
node,
suggest: [
{
messageId: 'removeAwait',
fix(fixer): TSESLint.RuleFix {
const awaitKeyword = nullThrows(
context.sourceCode.getFirstToken(node, isAwaitKeyword),
NullThrowsReasons.MissingToken('await', 'await expression'),
);
return fixer.remove(awaitKeyword);
},
},
],
});
}
},

@typescript-eslint/triage-team Thoughts? Otherwise we can of course just sync the logic with await-thenable.

@JoshuaKGoldberg
Copy link
Member

Agreed, though that feels like a breaking change either way. 😬

@kirkwaiblinger
Copy link
Member
kirkwaiblinger commented Aug 30, 2024

Eh. It feels like it doesn't quite technically satisfy any of the requirements in https://typescript-eslint.io/users/versioning#eslint-plugin.

But, it is quietly documented on the rule page by example only (invalidInTryCatch6, invalidAlways3). So, arguably would be slightly breaking 🤔

It sounds like between the choices of

  1. Resolve this ticket by synchronizing the behavior with await-thenable, and spinning off separate issue for deprecating and later removing the await-thenable-like check from return-await
  2. Resolve this ticket by removing the await-thenable-like check from return-await entirely

we are going to go with Option 1.

@kirkwaiblinger kirkwaiblinger added the good first issue Good for newcomers label Sep 20, 2024
@github-actions github-actions bot added the locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. label Oct 18, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working good first issue Good for newcomers locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
0