microsoft / microsoft/TypeScript

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

Abierto
#44,152 1 comentario 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Experience Enhancement Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza rastreando la comprobación existente de Promise always-truthy de TypeScript 4.3 y el diagnóstico 2801 para las expresiones IfStatement. Compara cómo se manejan los nodos BinaryExpression y ConditionalExpression con los ejemplos lógicos y ternarios del issue. Se considerará terminado cuando los casos de Promise mostrados produzcan los diagnósticos previstos sin cambiar la salida en tiempo de ejecución.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Nueva funcionalidad
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.