Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Support for Ternary Operator assignments in var context

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

Maintainer thường phản hồi trong vòng 1 ngày

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

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
45/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
java
Lĩnh vực
devtools

Hướng nghiên cứu

Bắt đầu với công việc tái cấu trúc var hiện có được tham chiếu trong #217 và sử dụng các ví dụ ternary cùng các điều kiện tiên quyết được liệt kê làm đặc tả hành vi. Công việc được xem là hoàn tất khi recipe chỉ áp dụng var cho các phép gán ternary đủ điều kiện, bỏ qua các trường hợp loại trừ đã nêu và kiểm tra kết quả trước và sau.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

enhancement

What problem are you trying to solve?

While developing #217 I discovered that handling, especially detecting ternary operator assignments, is hard.
With this issue, Rewrite become able to apply ´var´ to ternary operator assignments.

What precondition(s) should be checked before applying this recipe?

var should not be applied to variable declarations where:

  • the type of the ternary operator and the left-hand side don't match
  • the type is byte or short
  • only null is assigned
  • the declaration is outside a method declaration

Describe the situation before applying the recipe

class A {
    void foo(String bar) {
        int i1 = bar != null ? 3 : 5;
        Integer i2 = bar != null ? 3 : null;
        Integer i3 = bar == null ? null : bar.lenght();
        Integer i4 = bar != null ? throw new IlligalArgumentExcpetion() : -1;
        int i5 = bar != null ? bar.lenght() : 0;
        float f1 = bar != null ? 3f : 3.5;
    }
}

Describe the situation after applying the recipe


class A {
    void foo(String bar) {
        var i1 = bar != null ? 3 : 5;
        var i2 = bar != null ? 3 : null;
        var i3 = bar == null ? null : bar.lenght();
        var i4 = bar != null ? throw new IlligalArgumentExcpetion() : -1;
        var i5 = bar != null ? bar.lenght() : 0;
        var f1 = bar != null ? 3f : 3.5;
    }
}

Have you considered any alternatives or workarounds?

An alternative would be to skip every ternary operator, this would lead to no 100% local variable type inference for rewrite.

Any additional context

It would be nice to write a separate recipe for this and integrate them later on as global var refactoring.

Are you interested in contributing this recipe to OpenRewrite?

not now.

Ngôn ngữ chính
Java
Star
156
Fork
129
Merge trung bình
7 giờ 21 phút
Pull request đã merge (30 ngày)
42

Chuẩn bị môi trường

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.

Issue khác của openrewrite/rewrite-migrate-java

Tất cả issue của openrewrite/rewrite-migrate-java

Issue tương tự

Thêm issue về Java

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.