Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Scalafix rewrites for 1.0.0 #1793

Merged
merged 20 commits into from
Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ as many breaking changes as possible in this release before we lock down the API
* `iteratorFoldM` was removed from `Foldable` due to #1716
* `Split` is removed, and the method `split` is moved to `Arrow`. Note that only under `CommutativeArrow` does it guarantee the non-interference between the effects. see #1567


If you feel adventurous you can try the experimental Scalafix rewrites.
See all the available rewrites and the instructions [here](/scalafix/README.md).
### Breaking Changes:

* [#1614](https://github.com/typelevel/cats/pull/1614): added `leftT` and improved existing lift API for `EitherT`. by @kailuowang
Expand Down
1 change: 1 addition & 0 deletions scalafix/.scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
align = none
45 changes: 45 additions & 0 deletions scalafix/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Scalafix rewrites for cats

## Try this!

Install the scalafix sbt plugin (globally or in a specific project):

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

run

```sh
sbt scalafix github:typelevel/cats/v1.0.0
```

## Available rewrites

- [x] All Unapply enabled methods, e.g. sequenceU, traverseU, etc. are removed. Unapply enabled syntax ops are also removed. Please use the partial unification SI-2712 fix instead. The easiest way might be this sbt-plugin.

- [x] The creation methods (left, right, apply, pure, etc.) in EitherT were improved to take less type arguments.

- [x] CartesianBuilder (i.e. |@|) syntax is deprecated, use the apply syntax on tuples instead. E.g. (x |@| y |@| z).map(...) should be replaced by (x, y, z).mapN(...)

- [x] Free.suspend is renamed to Free.defer for consistency.

- [x] traverse1_, intercalate1 and sequence1_ in Reducible were renamed to nonEmptyTraverse_, nonEmptyIntercalate and nonEmptySequence_ 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".

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

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

- [ ] iteratorFoldM was removed from Foldable due to #1716

- [ ] Split is removed, and the method split is moved to Arrow. Note that only under CommutativeArrow does it guarantee the non-interference between the effects. see #1567
36 changes: 36 additions & 0 deletions scalafix/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Use a scala version supported by scalafix.
scalaVersion in ThisBuild := org.scalameta.BuildInfo.supportedScalaVersions.last

lazy val rewrites = project.settings(
libraryDependencies += "ch.epfl.scala" %% "scalafix-core" % "0.5.0-M1"
)

lazy val input = project.settings(
scalametaSourceroot := sourceDirectory.in(Compile).value,
libraryDependencies ++= Seq(
"org.typelevel" %% "cats" % "0.9.0"
)
)

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

lazy val tests = project
.settings(
libraryDependencies += "ch.epfl.scala" % "scalafix-testkit" % "0.5.0-M1" % Test cross CrossVersion.full,
buildInfoPackage := "fix",
buildInfoKeys := Seq[BuildInfoKey](
"inputSourceroot" ->
sourceDirectory.in(input, Compile).value,
"outputSourceroot" ->
sourceDirectory.in(output, Compile).value,
"inputClassdirectory" ->
classDirectory.in(input, Compile).value
)
)
.dependsOn(input, rewrites)
.enablePlugins(BuildInfoPlugin)
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
rewrite = "scala:fix.v1_0_0.RemoveCartesianBuilder"
*/
package fix
package to1_0_0

object RemoveCartesianBuilderTests {
{
import cats.instances.option._
import cats.instances.int._
import cats.syntax.cartesian._
import cats.syntax.semigroup._
val o1: Option[Int] = Some(42)
val o2: Option[String] = Some("hello")
val o3: Option[Int] = Some(2)
(o1 |@| o2).map((i: Int, s: String) => i.toString ++ s)
(o1 |@| o2).tupled
(o1 |@| o2 |@| o3).map(_ + _ + _)
(o1 |+| o1 |@| o3).map(_ + _)
o1 |@| o2 |@| o3 map (_ + _ + _)
(o1 |+| o1 |@| o3) map (_ + _)
o1 |+| o1 |@| o3 map (_ + _)

(o1 |@|
o2 |@|
o3) map (_ + _ + _)
}

{
import cats.{Semigroup, Eq}
import cats.implicits._
case class Foo(a: String, c: List[Double])

(Semigroup[String] |@| Semigroup[List[Double]])
.imap(Foo.apply)(Function.unlift(Foo.unapply))

(Eq[Double] |@| Eq[String]).contramap { (a: Foo) =>
(2, "bar")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* ONLY
rewrite = "scala:fix.v1_0_0.RemoveCartesianBuilder"
*/
package fix
package to1_0_0

object RemoveCartesianBuilderTests2 {
import cats.instances.all._
import cats.syntax.cartesian._
val o1: Option[Int] = Some(2)
val o2: Option[Int] = Some(2)
o1 *> o2
(o1 |@| o2).map(_ + _)
}

44 changes: 44 additions & 0 deletions scalafix/input/src/main/scala/fix/v1_0_0/RemoveUnapply.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
rewrite = "scala:fix.v1_0_0.RemoveUnapply"
*/
package fix
package to1_0_0

object RemoveUnapplyTests {
import cats.implicits._
import cats.Foldable
def parseInt(s: String): Either[String, Int] =
Either.catchOnly[NumberFormatException](s.toInt).leftMap(_ => "no number")
val ns = List("1", "2", "3")
ns.traverseU(parseInt)
ns.traverseU_(parseInt)
Foldable[List].traverseU_(ns)(parseInt)

import cats.data.{Validated, ValidatedNel}
val x: List[ValidatedNel[String, Int]] =
List(Validated.valid(1), Validated.invalid("a"), Validated.invalid("b"))
.map(_.toValidatedNel)
x.sequenceU
x.sequenceU_
Foldable[List].sequenceU_(x)

import cats.data.Func.{appFuncU, appFunc}
import cats.data.State.{get, set}
import cats.data.Const
type Count[A] = Const[Int, A]
def liftInt(i: Int): Count[Unit] = Const(i)
def isSpace(c: Char): Boolean = (c == ' ' || c == '\n')
def testIf(b: Boolean): Int = if (b) 1 else 0
appFuncU { (c: Char) =>
for {
x <- get[Boolean]
y = !isSpace(c)
_ <- set(y)
} yield testIf(y && !x)
} andThen appFunc(liftInt)

import cats.free.FreeT
val a: Either[String, Int] = Right(42)
FreeT.liftTU(a)

}
14 changes: 14 additions & 0 deletions scalafix/input/src/main/scala/fix/v1_0_0/RenameFreeSuspend.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
rewrite = "scala:fix.v1_0_0.RenameFreeSuspend"
*/
package fix
package to1_0_0

object RenameFreeSuspendTests {
import cats.free.{Free, Trampoline}

val x = Free.pure[Option, Int](2)
Free.suspend(x)

Trampoline.suspend(Trampoline.done(2))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
rewrite = "scala:fix.v1_0_0.RenameReducibleMethods"
*/
package fix
package to1_0_0

object RenameReducibleMethodsTests {
import cats.data.NonEmptyList
import cats.Reducible
import cats.syntax.reducible._
import cats.instances.all._

val ns = NonEmptyList(1, List(2, 3))
Reducible[NonEmptyList].traverse1_(ns)(Option.apply)
ns.traverse1_(Option.apply)

Reducible[NonEmptyList].intercalate1(ns, 0)
ns.intercalate1(0)

Reducible[NonEmptyList].sequence1_(ns.map(Option.apply))
ns.map(Option.apply).sequence1_
}
14 changes: 14 additions & 0 deletions scalafix/input/src/main/scala/fix/v1_0_0/SimplifyEitherTLift.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
rewrite = "scala:fix.v1_0_0.SimplifyEitherTLift"
*/
package fix
package to1_0_0

object SimplifyEitherTLiftTests {
import cats.Id
import cats.data.EitherT
val eithert = EitherT.left[Id, String, Int]("eithert")
eithert.recoverWith {
case "eithert" => EitherT.right[Id, String, Int](5)
}.isRight
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package fix
package to1_0_0

object RemoveCartesianBuilderTests {
{
import cats.instances.option._
import cats.instances.int._
import cats.syntax.apply._
import cats.syntax.semigroup._
val o1: Option[Int] = Some(42)
val o2: Option[String] = Some("hello")
val o3: Option[Int] = Some(2)
(o1, o2).mapN((i: Int, s: String) => i.toString ++ s)
(o1, o2).tupled
(o1, o2, o3).mapN(_ + _ + _)
(o1 |+| o1, o3).mapN(_ + _)
(o1, o2, o3) mapN (_ + _ + _)
(o1 |+| o1, o3) mapN (_ + _)
(o1 |+| o1, o3) mapN (_ + _)

(o1,
o2,
o3) mapN (_ + _ + _)
}

{
import cats.{Semigroup, Eq}
import cats.implicits._
case class Foo(a: String, c: List[Double])

(Semigroup[String], Semigroup[List[Double]])
.imapN(Foo.apply)(Function.unlift(Foo.unapply))

(Eq[Double], Eq[String]).contramapN { (a: Foo) =>
(2, "bar")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fix
package to1_0_0

object RemoveCartesianBuilderTests2 {
import cats.instances.all._
import cats.syntax.cartesian._
import cats.syntax.apply._
val o1: Option[Int] = Some(2)
val o2: Option[Int] = Some(2)
o1 *> o2
(o1, o2).mapN(_ + _)
}

41 changes: 41 additions & 0 deletions scalafix/output/src/main/scala/fix/v1_0_0/RemoveUnapply.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package fix
package to1_0_0

object RemoveUnapplyTests {
import cats.implicits._
import cats.Foldable
def parseInt(s: String): Either[String, Int] =
Either.catchOnly[NumberFormatException](s.toInt).leftMap(_ => "no number")
val ns = List("1", "2", "3")
ns.traverse(parseInt)
ns.traverse_(parseInt)
Foldable[List].traverse_(ns)(parseInt)

import cats.data.{Validated, ValidatedNel}
val x: List[ValidatedNel[String, Int]] =
List(Validated.valid(1), Validated.invalid("a"), Validated.invalid("b"))
.map(_.toValidatedNel)
x.sequence
x.sequence_
Foldable[List].sequence_(x)

import cats.data.Func.appFunc
import cats.data.State.{get, set}
import cats.data.Const
type Count[A] = Const[Int, A]
def liftInt(i: Int): Count[Unit] = Const(i)
def isSpace(c: Char): Boolean = (c == ' ' || c == '\n')
def testIf(b: Boolean): Int = if (b) 1 else 0
appFunc { (c: Char) =>
for {
x <- get[Boolean]
y = !isSpace(c)
_ <- set(y)
} yield testIf(y && !x)
} andThen appFunc(liftInt)

import cats.free.FreeT
val a: Either[String, Int] = Right(42)
FreeT.liftT(a)

}
11 changes: 11 additions & 0 deletions scalafix/output/src/main/scala/fix/v1_0_0/RenameFreeSuspend.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fix
package to1_0_0

object RenameFreeSuspendTests {
import cats.free.{Free, Trampoline}

val x = Free.pure[Option, Int](2)
Free.defer(x)

Trampoline.defer(Trampoline.done(2))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package fix
package to1_0_0

object RenameReducibleMethodsTests {
import cats.data.NonEmptyList
import cats.Reducible
import cats.syntax.reducible._
import cats.instances.all._

val ns = NonEmptyList(1, List(2, 3))
Reducible[NonEmptyList].nonEmptyTraverse_(ns)(Option.apply)
ns.nonEmptyTraverse_(Option.apply)

Reducible[NonEmptyList].nonEmptyIntercalate(ns, 0)
ns.nonEmptyIntercalate(0)

Reducible[NonEmptyList].nonEmptySequence_(ns.map(Option.apply))
ns.map(Option.apply).nonEmptySequence_
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fix
package to1_0_0

object SimplifyEitherTLiftTests {
import cats.Id
import cats.data.EitherT
val eithert = EitherT.leftT[Id, Int]("eithert")
eithert.recoverWith {
case "eithert" => EitherT.pure[Id, String](5)
}.isRight
}
1 change: 1 addition & 0 deletions scalafix/project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=0.13.13
5 changes: 5 additions & 0 deletions scalafix/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.5.0-M1")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is repeated :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, thank you!

addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.5.0-M1")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once is good enough, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

most definitely! Good catch, thanks :)

addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.0.0-RC3")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.6.1")
addSbtPlugin("org.lyranthe.sbt" % "partial-unification" % "1.0.0")
Loading