fix.AddExplicitImplicitTypes.scala Maven / Gradle / Ivy
The newest version!
package fix
import scala.meta.Defn
import scala.meta.Mod
import scala.meta.Term
import scala.meta.Type
import scala.meta.XtensionClassifiable
import scala.meta.XtensionCollectionLikeUI
import scalafix.Patch
import scalafix.v1.SyntacticDocument
import scalafix.v1.SyntacticRule
import scalafix.v1.XtensionSeqPatch
/**
* `implicit val foo = new Foo`
* to
* `implicit val foo: Foo = new Foo`
*/
class AddExplicitImplicitTypes extends SyntacticRule("AddExplicitImplicitTypes") {
override def fix(implicit doc: SyntacticDocument): Patch = {
doc.tree.collect {
case t1: Defn.Val if t1.mods.exists(_.is[Mod.Implicit]) && t1.decltpe.isEmpty =>
t1.rhs match {
case t2: Term.New =>
t2.init.tpe match {
case name: Type.Name =>
Patch.replaceTree(
t1,
t1.copy(
decltpe = Some(name)
).toString
)
case _ => // TODO if there is type params
Patch.empty
}
case t2: Term.NewAnonymous =>
t2.templ.inits.map(_.tpe) match {
case (tpe @ Type.Name(_)) :: Nil =>
Patch.replaceTree(
t1,
t1.copy(
decltpe = Some(tpe)
).toString
)
case _ =>
Patch.empty
}
case _ =>
Patch.empty
}
}.asPatch
}
}