openrewrite / openrewrite/rewrite-static-analysis
Extend TernaryOperatorsShouldNotBeNested with pattern matching
Open
Nobody has claimed this yet.
enhancement
- Dominant language
- Java
- Stars
- 62
- Forks
- 112
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 40
Description
What problem are you trying to solve?
Pattern matching switch expressions can be used to simplify nested ternaries
Describe the solution you'd like
"""
import java.util.Set;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
class Test {
public Set<String> makeASet() {
List<String> s = Arrays.asList("a","b","c","nope");
return s.stream().map(item -> item.startsWith("a") ? "a" : item.startsWith("b") ? "b" : "nope").collect(Collectors.toSet());
}
}
""",
"""
import java.util.Set;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
class Test {
public Set<String> makeASet() {
List<String> s = Arrays.asList("a","b","c","nope");
return s.stream().map(item ->
switch (item) {
case String st && st.startsWith("a") -> "a";
case String st && st.startsWith("b") -> "b";
default -> "nope";
}
).collect(Collectors.toSet());
}
}
"""
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the TernaryOperatorsShouldNotBeNested recipe and its existing tests. Read how nested ternary examples are represented, then add coverage for the pattern-matching switch example shown in the issue and run the recipe's test suite to confirm the expected transformation is recognized.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100