Skip to content

Commit

Permalink
correct scalafix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
bpholt committed Mar 14, 2024
1 parent a68fd8d commit 18820d3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.dwolla.scrooge.scalafix

import com.dwolla.scrooge.scalafix.AddCatsTaglessInstances._
import scalafix.v1._
import com.dwolla.scrooge.scalafix.AddCatsTaglessInstances.*
import scalafix.v1.*

import scala.meta._
import scala.meta.*
import scala.meta.tokens.Token.Ident
import GuardedPatchBuilder.toGuardedPatch

import scala.meta.Type.ParamClause

object ThriftServiceTrait {
def unapply(subtree: Tree): Option[(String, Defn.Trait)] =
PartialFunction.condOpt(subtree) {
case term@Defn.Trait(_, Type.Name(name), Nil, _, ExtendsThriftService(_)) => (name, term)
case term@Defn.Trait.After_4_6_0(_, Type.Name(name), ParamClause(Nil), _, ExtendsThriftService(_)) => (name, term)
}
}

Expand Down Expand Up @@ -43,7 +45,7 @@ object TemplateExtends {
}

PartialFunction.condOpt(subtree) {
case t@Template(_, TemplateInitsContainName(_), _, _) => t
case t@Template.After_4_4_0(_, TemplateInitsContainName(_), _, _, _) => t
}
}
}
Expand Down Expand Up @@ -100,14 +102,14 @@ case class ThriftServiceTrait(private val generatedThriftServiceObject: Defn.Obj
}
.map(Patch.replaceToken(_, s"${generatedThriftServiceObject.name.value}[F[_]]"))
.onlyIfMissing {
case Defn.Trait(_, Type.Name(name), _, _, _) if name == generatedThriftServiceObject.name.value => ()
case Defn.Trait.After_4_6_0(_, Type.Name(name), _, _, _) if name == generatedThriftServiceObject.name.value => ()
}
.in(generatedThriftServiceObject)

private val addServiceCompanionObjectWithImplicits: Patch =
Patch.addRight(methodPerEndpointTrait, companionObjectWithImplicits)
.onlyIfMissing {
case Defn.Object(_, Term.Name(name), Template(_, List(), _, _)) if name == generatedThriftServiceObject.name.value => ()
case Defn.Object(_, Term.Name(name), Template.After_4_4_0(_, List(), _, _, _)) if name == generatedThriftServiceObject.name.value => ()
}
.in(generatedThriftServiceObject)

Expand All @@ -116,7 +118,7 @@ case class ThriftServiceTrait(private val generatedThriftServiceObject: Defn.Obj
s"""
| trait MethodPerEndpoint extends ${generatedThriftServiceObject.name.value}[Future]""".stripMargin)
.onlyIfMissing {
case Defn.Trait(_, Type.Name("MethodPerEndpoint"), _, _, ExtendsServiceTrait(_)) => ()
case Defn.Trait.After_4_6_0(_, Type.Name("MethodPerEndpoint"), _, _, ExtendsServiceTrait(_)) => ()
}
.in(generatedThriftServiceObject)

Expand All @@ -138,7 +140,7 @@ case class ThriftServiceTrait(private val generatedThriftServiceObject: Defn.Obj
t.parent.fold(Patch.empty) {
Patch.replaceToken(t.tokens.head, "F")
.onlyIfMissing {
case Type.Apply(Type.Name(name), _) if name == generatedThriftServiceObject.name.value => ()
case Type.Apply.After_4_6_0(Type.Name(name), _) if name == generatedThriftServiceObject.name.value => ()
}
.in(_)
}
Expand All @@ -163,8 +165,8 @@ case class ThriftServiceTrait(private val generatedThriftServiceObject: Defn.Obj
methodPerEndpointTrait
.templ
.collect {
case d@Decl.Def(_, name, _, paramss, _) =>
s"override ${d.toString()} = fa.$name${invokeEachParamList(paramss)}"
case d@Decl.Def.After_4_7_3(_, name, paramss, _) =>
s"override ${d.toString()} = fa.$name${invokeEachParamList(paramss.flatMap(_.paramClauses.map(_.values)))}"
}

val code =
Expand All @@ -182,7 +184,7 @@ case class ThriftServiceTrait(private val generatedThriftServiceObject: Defn.Obj
modifyMethodPerEndpointTrait ++ modifyMethodPerEndpointCompanionObject
}

class GuardedPatch(private val tuple: (Patch, PartialFunction[Tree, _])) extends AnyVal {
class GuardedPatch(private val tuple: (Patch, PartialFunction[Tree, ?])) extends AnyVal {
def in(tree: Tree): Patch = {
val empty = tree.collect(tuple._2).isEmpty
if (empty) tuple._1
Expand All @@ -191,7 +193,7 @@ class GuardedPatch(private val tuple: (Patch, PartialFunction[Tree, _])) extends
}

class GuardedPatchBuilder(private val patch: Patch) extends AnyVal {
def onlyIfMissing(pf: PartialFunction[Tree, _]) = new GuardedPatch(patch -> pf)
def onlyIfMissing(pf: PartialFunction[Tree, ?]) = new GuardedPatch(patch -> pf)
}

object GuardedPatchBuilder {
Expand All @@ -210,13 +212,13 @@ case class Unapplied(patch: Patch) extends IdempotentPatch
object IdempotentPatch {
def apply(patch: Patch)
(tree: Tree)
(pf: PartialFunction[Tree, _]): IdempotentPatch =
(pf: PartialFunction[Tree, ?]): IdempotentPatch =
if (tree.collect(pf).nonEmpty) AlreadyApplied
else Unapplied(patch)

def apply(patch: Option[Patch])
(tree: Tree)
(pf: PartialFunction[Tree, _]): IdempotentPatch =
(pf: PartialFunction[Tree, ?]): IdempotentPatch =
IdempotentPatch(patch.getOrElse(Patch.empty))(tree)(pf)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ class AddCatsTaglessInstancesTest extends FunSuite {

test("rename MethodPerEndpoint to SimpleService[F[_]]") {
val expectedPatchContent =
s"Add(MethodPerEndpoint, MethodPerEndpoint [${range("MethodPerEndpoint")(input.indexOf)}), SimpleService[F[_]])"
s"Add(MethodPerEndpoint, Ident(MethodPerEndpoint) [${range("MethodPerEndpoint")(input.indexOf)}), SimpleService[F[_]])"

expect(patch.toString.contains(expectedPatchContent))
}

test("replace Future[*] with F[*]") {
val expectedPatchContent =
s"Add(Future, Future [${range("Future")(input.indexOf)}), F)"
s"Add(Future, Ident(Future) [${range("Future")(input.indexOf)}), F)"

expect(patch.toString.contains(expectedPatchContent))
}

test("add SimpleService companion object with implicits") {
val expectedPatchContent =
"""Add(}, } [467..468), }
"""Add(}, RightBrace(}) [467..468), }
|
| object SimpleService {
| implicit def SimpleServiceInReaderT[F[_]]: SimpleService[({type Λ[β0] = _root_.cats.data.ReaderT[F, SimpleService[F], β0]})#Λ] =
Expand All @@ -92,7 +92,7 @@ class AddCatsTaglessInstancesTest extends FunSuite {

test("add replacement MethodPerEndpoint that extends SimpleService[Future]") {
val expectedPatchContent =
"""Add(}, } [467..468), }
"""Add(}, RightBrace(}) [467..468), }
| trait MethodPerEndpoint extends SimpleService[Future])""".stripMargin

expect(patch.toString.contains(expectedPatchContent))
Expand Down

0 comments on commit 18820d3

Please sign in to comment.