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 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Experience Enhancement Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、IfStatement 式に対する既存の TypeScript 4.3 の always-truthy な Promise チェックと診断 2801 をたどります。BinaryExpression ノードと ConditionalExpression ノードの処理を、issue の論理演算子と三項演算子の例における処理と比較します。完了の条件は、示されている Promise のケースが、ランタイム出力を変更せずに意図した診断を生成することです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。