microsoft / microsoft/TypeScript
Extend the "forgot an await" check from 4.3 to do limited AST analysis to cover more cases
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.4k
- Merge trung bình
- 1 ngày 19 giờ
- Pull request đã merge (30 ngày)
- 117
Mô tả
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:
- If an
IfStatement's.expressionis aBinaryExpressionand the.operatoris one ofBarBarToken,AmpersandAmpersandToken, orQuestionQuestionToken, then TypeScript should recursively check the.leftand.rightof the node. - if an
IfStatement's.expressionis aConditionalExpression, then TypeScript should recursively check the.whenTrueand.whenFalseof 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.
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách lần theo kiểm tra Promise always-truthy hiện có trong TypeScript 4.3 và chẩn đoán 2801 cho các biểu thức IfStatement. So sánh cách xử lý các node BinaryExpression và ConditionalExpression với các ví dụ logic và toán tử ba ngôi trong issue. Được xem là hoàn tất khi các trường hợp Promise được nêu tạo ra các chẩn đoán dự kiến mà không thay đổi đầu ra khi chạy.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- typescript
- Lĩnh vực
- compilers
- Loại issue
- Tính năng
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 45/100