Skip to content

Commit

Permalink
Add RenameInjectProdAndCoproduct Scalafix rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
gabro committed Aug 10, 2017
1 parent aae219a commit 6fc2c6e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
6 changes: 3 additions & 3 deletions scalafix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Install the scalafix sbt plugin (globally or in a specific project):

```scala
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.5.0-M2")
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.5.0-M3")
```

run
Expand All @@ -26,12 +26,12 @@ sbt scalafix github:typelevel/cats/v1.0.0

- [x] traverse1_, intercalate1 and sequence1_ in Reducible were renamed to nonEmptyTraverse_, nonEmptyIntercalate and nonEmptySequence_ respectively.

- [x] cats.free.Inject is moved from cats-free to cats-core and renamed to cats.InjectK; cats.data.Prod is renamed to cats.data.Tuple2K; cats.data.Coproduct is renamed to cats.data.EitherK

# WIP

- [ ] cats no longer publishes the all-inclusive bundle package "org.typelevel" % "cats", use cats-core, cats-free, or cats-law accordingly instead. If you need cats.free, use "org.typelevel" % "cats-free", if you need cats-laws use "org.typelevel" % "cats-laws", if neither, use "org.typelevel" % "cats-core".

- [ ] cats.free.Inject is moved from cats-free to cats-core and renamed to cats.InjectK; cats.data.Prod is renamed to cats.data.Tuple2K; cats.data.Coproduct is renamed to cats.data.EitherK

- [ ] FunctorFilter, MonadCombine, MonadFilter, MonadReader, MonadState, MonadTrans, MonadWriter and TraverseFilter are no longer in cats, the functionalities they provided are inhereted by the new cats-mtl project. Please check here for migration guide.

- [ ] Apply syntax on tuple (e.g. (x, y, z).map3(...)) was moved from cats.syntax.tuple._ to cats.syntax.apply._ and renamed to mapN, contramapN and imapN respectively.
Expand Down
6 changes: 4 additions & 2 deletions scalafix/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ lazy val input = project.settings(
scalafixSourceroot := sourceDirectory.in(Compile).value,
libraryDependencies ++= Seq(
"org.typelevel" %% "cats" % "0.9.0"
)
),
scalacOptions += "-language:higherKinds"
)

lazy val output = project.settings(
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-core" % "1.0.0-MF",
"org.typelevel" %% "cats-free" % "1.0.0-MF"
)
),
scalacOptions += "-language:higherKinds"
)

lazy val tests = project
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
rewrite = "scala:fix.v1_0_0.RenameInjectProdAndCoproduct"
*/
package fix
package to1_0_0

import cats.free.Inject
import cats.data.{ Coproduct, Prod }

object RenameInjectProdAndCoproductTests {
def inject[F[_], G[_]](implicit inj: Inject[F, G]) = ???
def prod[F[_], G[_], A](implicit prod: Prod[F, G, A]) = ???
def coprod[F[_], G[_], A](implicit coprod: Coproduct[F, G, A]) = ???
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fix
package to1_0_0

import cats.InjectK
import cats.data.{ EitherK, Tuple2K }

object RenameInjectProdAndCoproductTests {
def inject[F[_], G[_]](implicit inj: InjectK[F, G]) = ???
def prod[F[_], G[_], A](implicit prod: Tuple2K[F, G, A]) = ???
def coprod[F[_], G[_], A](implicit coprod: EitherK[F, G, A]) = ???
}
19 changes: 17 additions & 2 deletions scalafix/rewrites/src/main/scala/fix/Cats_v1_0_0.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ case class RemoveCartesianBuilder(semanticCtx: SemanticCtx)
}

private[this] def wrapInParensIfNeeded(ctx: RewriteCtx, t: Term): Patch = {
if (t.tokens.head.is[Token.LeftParen] && t.tokens.last.is[Token.RightParen]) {
if (t.tokens.head.is[Token.LeftParen] && t.tokens.last
.is[Token.RightParen]) {
Patch.empty
} else {
ctx.addLeft(t.tokens.head, "(") + ctx.addRight(t.tokens.last, ")")
Expand All @@ -102,7 +103,7 @@ case class RemoveCartesianBuilder(semanticCtx: SemanticCtx)
case t: Term.Name => rename(ctx, t, renames)
case t @ q"import cats.syntax.cartesian._" =>
val usesPartialApplies = ctx.tree.collect {
case t: Term.Name if t.isOneOfSymbols(partialApplies) => ()
case t: Term.Name if t.isOneOfSymbols(partialApplies) => ()
}.length > 0
if (usesPartialApplies) {
ctx.addRight(t.tokens.last, "\n import cats.syntax.apply._")
Expand Down Expand Up @@ -216,3 +217,17 @@ case class SimplifyEitherTLift(semanticCtx: SemanticCtx) extends SemanticRewrite
}

}

// ref: https://github.com/typelevel/cats/pull/1589
// https://github.com/typelevel/cats/pull/1596
case class RenameInjectProdAndCoproduct(semanticCtx: SemanticCtx) extends SemanticRewrite(semanticCtx) {

def rewrite(ctx: RewriteCtx): Patch = {
ctx.replaceSymbols(
"_root_.cats.free.Inject." -> "_root_.cats.InjectK.",
"_root_.cats.data.Prod." -> "_root_.cats.data.Tuple2K.",
"_root_.cats.data.Coproduct." -> "_root_.cats.data.EitherK."
)
}

}

0 comments on commit 6fc2c6e

Please sign in to comment.