Skip to content

Commit

Permalink
Add RenameTupleApplySyntax Scalafix rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
gabro committed Aug 10, 2017
1 parent 6fc2c6e commit 52de404
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
4 changes: 2 additions & 2 deletions scalafix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ sbt scalafix github:typelevel/cats/v1.0.0

- [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

- [x] 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.

# 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".

- [ ] 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.

- [ ] Several cats-core type class instances for cats.kernel were moved from their companion objects to separate traits and thus require imports from cats.instances.xxx._ (or the recommended import cats.implicits._) now. See #1659 for more details.

- [ ] foldLeftM is removed from Free, use foldM on Foldable instead, see #1117 for detail.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
rewrite = "scala:fix.v1_0_0.RenameTupleApplySyntax"
*/
package fix
package to1_0_0

object RenameTupleApplySyntaxTests {
import cats.{Eq, Semigroup}
import cats.instances.option._
import cats.instances.int._
import cats.syntax.tuple._

(Option(1), Option(2)).map2(_ + _)
(Option(1), Option(2), Option(3)).map3(_ + _ + _)
(Option(1), Option(2), Option(3), Option(4)).map4(_ + _ + _ + _)

case class Foo2(a: Int, b: Int)
case class Foo3(a: Int, b: Int, c: Int)
case class Foo4(a: Int, b: Int, c: Int, d: Int)

(Eq[Int], Eq[Int]).contramap2((f: Foo2) => (f.a, f.b))
(Eq[Int], Eq[Int], Eq[Int]).contramap3((f: Foo3) => (f.a, f.b, f.c))
(Eq[Int], Eq[Int], Eq[Int], Eq[Int]).contramap4((f: Foo4) => (f.a, f.b, f.c, f.d))

(Semigroup[Int], Semigroup[Int])
.imap2(Foo2.apply)(Function.unlift(Foo2.unapply))
(Semigroup[Int], Semigroup[Int], Semigroup[Int])
.imap3(Foo3.apply)(Function.unlift(Foo3.unapply))
(Semigroup[Int], Semigroup[Int], Semigroup[Int], Semigroup[Int])
.imap4(Foo4.apply)(Function.unlift(Foo4.unapply))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fix
package to1_0_0

object RenameTupleApplySyntaxTests {
import cats.{Eq, Semigroup}
import cats.instances.option._
import cats.instances.int._
import cats.syntax.apply._

(Option(1), Option(2)).mapN(_ + _)
(Option(1), Option(2), Option(3)).mapN(_ + _ + _)
(Option(1), Option(2), Option(3), Option(4)).mapN(_ + _ + _ + _)

case class Foo2(a: Int, b: Int)
case class Foo3(a: Int, b: Int, c: Int)
case class Foo4(a: Int, b: Int, c: Int, d: Int)

(Eq[Int], Eq[Int]).contramapN((f: Foo2) => (f.a, f.b))
(Eq[Int], Eq[Int], Eq[Int]).contramapN((f: Foo3) => (f.a, f.b, f.c))
(Eq[Int], Eq[Int], Eq[Int], Eq[Int]).contramapN((f: Foo4) => (f.a, f.b, f.c, f.d))

(Semigroup[Int], Semigroup[Int])
.imapN(Foo2.apply)(Function.unlift(Foo2.unapply))
(Semigroup[Int], Semigroup[Int], Semigroup[Int])
.imapN(Foo3.apply)(Function.unlift(Foo3.unapply))
(Semigroup[Int], Semigroup[Int], Semigroup[Int], Semigroup[Int])
.imapN(Foo4.apply)(Function.unlift(Foo4.unapply))
}
23 changes: 23 additions & 0 deletions scalafix/rewrites/src/main/scala/fix/Cats_v1_0_0.scala
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,26 @@ case class RenameInjectProdAndCoproduct(semanticCtx: SemanticCtx) extends Semant
}

}

case class RenameTupleApplySyntax(semanticCtx: SemanticCtx) extends SemanticRewrite(semanticCtx) {

private[this] val renames: Map[String, String] =
(1 to 22)
.map { arity =>
Seq(
s"_root_.cats.syntax.Tuple${arity}CartesianOps.map$arity." -> "mapN",
s"_root_.cats.syntax.Tuple${arity}CartesianOps.contramap$arity." -> "contramapN",
s"_root_.cats.syntax.Tuple${arity}CartesianOps.imap$arity." -> "imapN"
)
}
.flatten
.toMap

def rewrite(ctx: RewriteCtx): Patch = {
ctx.tree.collect {
case t: Term.Name => rename(ctx, t, renames)
case t @ q"import cats.syntax.tuple._" =>
ctx.replaceTree(t, "import cats.syntax.apply._")
}.asPatch
}
}

0 comments on commit 52de404

Please sign in to comment.