All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fix.CollectHeadOption.scala Maven / Gradle / Ivy

There is a newer version: 0.6.7
Show newest version
package fix

import scala.meta.Term
import scala.meta.XtensionCollectionLikeUI
import scalafix.Patch
import scalafix.v1.SyntacticDocument
import scalafix.v1.SyntacticRule
import scalafix.v1.XtensionSeqPatch

class CollectHeadOption extends SyntacticRule("CollectHeadOption") {
  override def fix(implicit doc: SyntacticDocument): Patch = {
    doc.tree.collect {
      case t @ Term.Select(
            Term.Apply.After_4_6_0(
              Term.Select(obj, Term.Name("collect")),
              Term.ArgClause(func :: Nil, _)
            ),
            Term.Name("headOption")
          ) =>
        func match {
          case _: Term.PartialFunction =>
            Patch.replaceTree(t, s"${obj}.collectFirst${func}")
          case _ =>
            Patch.replaceTree(t, s"${obj}.collectFirst(${func})")
        }
    }
  }.asPatch
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy