Support for Ternary Operator assignments in var context
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 #217 中引用的现有 var 重构工作开始,并将列出的三元示例和前置条件作为行为规范。完成的标准是:该 recipe 仅对符合条件的三元赋值应用 var,跳过所述排除情况,并验证变更前后的结果。
由索引模型根据 Issue 内容生成。
描述
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 分钟
- 30 天内合并 PR
- 43
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
openrewrite/rewrite-migrate-java 的其他 Issue
-
bug
难度 3/5 半天 新手友好度 40/100
openrewrite/rewrite-migrate-java#1292 ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 55/100
openrewrite/rewrite-migrate-java#1289 ·
维护者通常 1 天内回复
-
bug
难度 4/5 3-5 天 新手友好度 68/100
openrewrite/rewrite-migrate-java#1246 ·
维护者通常 1 天内回复
-
难度 5/5 一周以上 新手友好度 32/100
openrewrite/rewrite-migrate-java#1227 · 2 条评论 ·
维护者通常 1 天内回复
-
OptionalNotPresentToIsEmpty rewrites `!isPresent()` on a non-`java.util.Optional` type, producing uncompilable `isEmpty()`可能重新可做 关联的 PR 已关闭且未合并。 未关闭
难度 3/5 1-2 天 新手友好度 70/100
openrewrite/rewrite-migrate-java#1146 · 3 条评论 ·
维护者通常 1 天内回复
查看 openrewrite/rewrite-migrate-java 的全部 Issue
相似的 Issue
-
RRateLimiter acquire methods accept negative permits and raise available permits above the rate.未关闭
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 4 天内回复
-
难度 1/5 1 小时以内 新手友好度 75/100
krahets/hello-algo#1993 ·
-
难度 2/5 1-3 小时 新手友好度 62/100
NationalSecurityAgency/ghidra#9884 ·
维护者通常 2 天内回复
-
Status: Pending Review Type: Feature Request
难度 1/5 1 小时以内 新手友好度 82/100
OpenRefine/OpenRefine#8019 ·
维护者通常 1 天内回复
-
`timer` rule rejects dates the engine accepts可能已有人在做 @MFA-G 今天认领。 未关闭bug ready
难度 2/5 1-3 小时 新手友好度 72/100
camunda/bpmnlint-plugin-camunda-compat#281 · 3 条评论 ·
维护者通常 1 天内回复