microsoft / microsoft/TypeScript

Allow explicit fallthrough when noFallthroughCasesInSwitch is enabled

Đang mở
#62,295 1 bình luận 3 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Awaiting More Feedback Suggestion
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

🔍 Search Terms

fallthrough noFallthroughCasesInSwitch

✅ Viability Checklist
⭐ Suggestion
    switch (...) {
        case 'A':
            doSomething();
            // fallthrough - this will allow falling to next statement
        case 'B':
            doSomethingElse();
   }

Instead of usage of @ts-expect-error this is nicely readable and intention is clearly visible in generated code.

📃 Motivating Example

Until now for noFallthroughCasesInSwitch is missing good way to allow falling through if needed. The only possible way @ts-expect-error is disabling not only fallthrough check, but also other checks which is not desired.

💻 Use Cases
  1. What do you want to use this for? Because eslint rule no-fallthrough does not handle TS exhaustive match, I would like to you noFallthroughCasesInSwitch config rule

  2. What shortcomings exist with current approaches?

    • eslint can not be used, its no-fallthrough rule can not detect exhaustive match, so the following code is incorrectly reported by eslint:
    function transform(action: 'KEEP' | 'INVERSE', b: boolean): boolean {
        switch (action) {
            case 'KEEP':
                switch (b) {
                    case true:
                        return true;
                    case false:
                        return false;
                }
            case 'INVERSE':
                switch (b) {
                    case true:
                        return false;
                    case false:
                        return true;
                }
        }
    }
    
    • satisfying eslint is not possible e.g. by putting break, because TS will start report Unreachable code detected and this can not be disabled by @ts-expect-error
    • disabling reported case by @ts-expect-error is also not viable, because it is disabling all type checks, so e.g. in case case something: validation that something has the correct type is also disabled (and issue #19139 is still opened)
  3. What workarounds are you using in the meantime? In my specific case, I had only one of these switches with exhaustive match, so I could put the statement as the last to not disable any check.

Implementation of #19139 would give me decent workaround and it is definitely more useful that this specific feature, but I believe both of them should be implemented.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Issue không nêu tên tệp hoặc test nào; hãy bắt đầu bằng cách xác định chẩn đoán noFallthroughCasesInSwitch và các kiểm tra luồng điều khiển của switch trong trình biên dịch TypeScript. So sánh marker fallthrough được đề xuất với các trường hợp @ts-expect-error và exhaustive-match hiện có, sau đó thêm coverage tập trung cho thấy fallthrough có chủ đích vẫn được kiểm tra kiểu.

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ó
5/5
Thời gian dự kiến
Hơn một tuần
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
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.