openrewrite / openrewrite/rewrite-static-analysis

Extend TernaryOperatorsShouldNotBeNested with pattern matching

Open
#158 0 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.