microsoft / microsoft/TypeScript

Extend the "forgot an await" check from 4.3 to do limited AST analysis to cover more cases

Ouverte
#44,152 1 commentaire 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Experience Enhancement Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

Suggestion

🔍 Search Terms

Promise await async "Did you forget to use 'await'?"

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

The new always-truthy promise checks are amazing!
They're very much like @typescript-eslint's no-misused-promises.

As I was investigating https://github.com/typescript-eslint/typescript-eslint/issues/3403, one thing I noticed was that it looks like TypeScript does not do any AST interrogation for this feature - it just inspects the type of the expression.

It'd be great if we could extend this feature to do some AST traversal for common cases (logical expressions and ternaries) so that it can catch more errors.

To clarify what I'm asking for:

  1. If an IfStatement's .expression is a BinaryExpression and the .operator is one of BarBarToken, AmpersandAmpersandToken, or QuestionQuestionToken, then TypeScript should recursively check the .left and .right of the node.
  2. if an IfStatement's .expression is a ConditionalExpression, then TypeScript should recursively check the .whenTrue and .whenFalse of the node.

📃 Motivating Example

declare async function isValid(): Promise<boolean>;
declare const user: {isActive: boolean} | undefined;

// As of TS4.3
if (isValid()) {
//  ^^^^^^^^^
// This condition will always return true since this 'Promise<boolean>' appears to always be defined. (2801)
}
// with this proposal, the following would be an error as well:

if (user?.isActive && isValid()) {
//                    ^^^^^^^^^
}

if (user?.isActive || isValid()) {
//                    ^^^^^^^^^
}

if (user?.isActive ?? isValid()) {
//                    ^^^^^^^^^
}
if (user?.isActive ? isValid() : false) {
//                   ^^^^^^^^^
}
if (user?.isActive ? false : isValid()) {
//                           ^^^^^^^^^
}
if (
  user?.isActive
  ? someOtherCondition
  ? isValid()
//  ^^^^^^^^^
  : false
  : isValid()
//  ^^^^^^^^^
) {
}

💻 Use Cases

The current approach is a step in the right direction, but it could cover even more cases!
Workaround is to use @typescript-eslint/no-misused-promises which does these in-depth checks.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par suivre la vérification existante des Promise always-truthy de TypeScript 4.3 et le diagnostic 2801 pour les expressions IfStatement. Comparez la façon dont les nœuds BinaryExpression et ConditionalExpression sont traités avec les exemples logiques et ternaires de l’issue. Le travail est terminé lorsque les cas Promise présentés produisent les diagnostics attendus sans modifier la sortie à l’exécution.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
45/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.