Skip to content

Commit

Permalink
fix CollectHeadOption
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Dec 7, 2024
1 parent ed5f351 commit 3dd9cfe
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions input/src/main/scala/fix/CollectHeadOptionTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ package fix

object CollectHeadOptionTest {
def x1: Option[String] = List(1, 2, 3).collect { case n if n % 2 == 0 => n.toString }.headOption
def x2(f: PartialFunction[Int, String]): Option[String] = List(1, 2, 3).collect(f).headOption
}
1 change: 1 addition & 0 deletions output/src/main/scala/fix/CollectHeadOptionTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package fix

object CollectHeadOptionTest {
def x1: Option[String] = List(1, 2, 3).collectFirst{ case n if n % 2 == 0 => n.toString }
def x2(f: PartialFunction[Int, String]): Option[String] = List(1, 2, 3).collectFirst(f)
}
7 changes: 6 additions & 1 deletion rules/src/main/scala/fix/CollectHeadOption.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ class CollectHeadOption extends SyntacticRule("CollectHeadOption") {
),
Term.Name("headOption")
) =>
Patch.replaceTree(t, s"${obj}.collectFirst${func}")
func match {
case _: Term.PartialFunction =>
Patch.replaceTree(t, s"${obj}.collectFirst${func}")
case _ =>
Patch.replaceTree(t, s"${obj}.collectFirst(${func})")
}
}
}.asPatch
}

0 comments on commit 3dd9cfe

Please sign in to comment.