Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Support for Ternary Operator assignments in var context

オープン
#237 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

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

評価

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

調査の方向性

#217で参照されている既存のvarリファクタリング作業から始め、列挙されている三項演算子の例と前提条件を動作仕様として使用します。レシピが対象となる三項代入にのみvarを適用し、記載された除外ケースをスキップし、変更前後の結果を検証すれば完了です。

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

説明

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.

主要言語
Java
スター
156
フォーク
129
平均マージ
7時間 12分
マージ済み PR(30日)
43

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

openrewrite/rewrite-migrate-java のほかの issue

openrewrite/rewrite-migrate-java の issue をすべて見る

似ている issue

Java の issue をもっと見る

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

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