microsoft / microsoft/TypeScript

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

未关闭
#44,152 1 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Experience Enhancement Suggestion
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

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.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先跟踪 TypeScript 4.3 中现有的 always-truthy Promise 检查,以及针对 IfStatement 表达式的诊断 2801。将其对 BinaryExpression 和 ConditionalExpression 节点的处理方式与 issue 中的逻辑和三元示例进行比较。完成的标准是,所示的 Promise 情况能够生成预期的诊断,同时不改变运行时输出。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。