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 symbol-based type shortener #42

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion core/src/main/scala/scalafix/rewrite/ExplicitImplicit.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package scalafix.rewrite

import scala.meta._
import scala.{meta => m}
import scalafix.util.Patch
import scalafix.util.Whitespace
import scalafix.util.logger

case object ExplicitImplicit extends Rewrite {
// Don't explicitly annotate vals when the right-hand body is a single call
Expand All @@ -27,7 +29,10 @@ case object ExplicitImplicit extends Rewrite {
replace <- lhsTokens.reverseIterator.find(x =>
!x.is[Token.Equals] && !x.is[Whitespace])
typ <- semantic.typeSignature(defn)
} yield Patch(replace, replace, s"$replace: ${typ.syntax}")
} yield {
val shortenedTpe = semantic.shortenType(typ, defn)
Patch(replace, replace, s"$replace: ${shortenedTpe.syntax}")
}
}.toSeq
ast.collect {
case t @ m.Defn.Val(mods, _, None, body)
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/scala/scalafix/rewrite/SemanticApi.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package scalafix.rewrite

import scala.meta.Defn
import scala.meta.Type
import scala.meta._

/** A custom semantic api for scalafix rewrites.
*
Expand All @@ -18,4 +17,7 @@ trait SemanticApi {

/** Returns the type annotation for given val/def. */
def typeSignature(defn: Defn): Option[Type]

/** Returns the shortened type at a given location. */
def shortenType(toShorten: Type, atLocation: Tree): Type
}
183 changes: 180 additions & 3 deletions core/src/test/resources/ExplicitImplicit/basic.source
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,30 @@ class A {
}
>>>
class A {
implicit val x: scala.collection.immutable.Map[Int, String] =
implicit val x: Map[Int, String] =
Map(1 -> "STRING")
}
<<< renamed map
import scala.collection.immutable.{Map => MyMap}
class A {
implicit val x = MyMap(1 -> "STRING")
}
>>>
import scala.collection.immutable.{Map => MyMap}
class A {
implicit val x: MyMap[Int, String] =
Copy link
Contributor

Choose a reason for hiding this comment

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

Impressive! 😍

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed :)

Copy link
Author

Choose a reason for hiding this comment

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

Have a look at the other two tests checking renames.

MyMap(1 -> "STRING")
}
<<< local map definition
class A {
case class Map[K](elem: (K, String))
implicit val x = Map(1 -> "STRING")
}
>>>
class A {
case class Map[K](elem: (K, String))
implicit val x: Map[Int] = Map(1 -> "STRING")
}
<<< def works
class A {
implicit def x = 2
Expand Down Expand Up @@ -52,6 +73,162 @@ class A {
implicit val x: B = new B
implicit val y: x.C = new x.C
}
<<< NOWRAP higher kinded cats
package cats {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we move this into src/test/scala/cats...?

Copy link
Contributor

Choose a reason for hiding this comment

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

to limit the test to where the rewrite happens

Copy link
Author

@jvican jvican Jan 24, 2017

Choose a reason for hiding this comment

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

This is just a reproduction of an intricate scenario. Even though it has the names cats, I would leave it here. If you disagree, please point to concrete file you'd like to have it.

Copy link
Contributor

Choose a reason for hiding this comment

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

No worries, let's keep it here. It's just such a big input, would be nice to have it smaller.

package laws {
package discipline {
trait CartesianTests[F[_]]
object CartesianTests {
trait Isomorphisms[F[_]]
object Isomorphisms {
def id[T[_]]: Isomorphisms[T] = ???
}
}
}
}
}
package cats {
trait Id[T]
}
package cats {
package tests {
import cats.laws.discipline._
class IdTests {
implicit val iso = CartesianTests.Isomorphisms.id[Id]
}
}
}
>>>
package cats {
package laws {
package discipline {
trait CartesianTests[F[_]]
object CartesianTests {
trait Isomorphisms[F[_]]
object Isomorphisms {
def id[T[_]]: Isomorphisms[T] = ???
}
}
}
}
}
package cats {
trait Id[T]
}
package cats {
package tests {
import cats.laws.discipline._
class IdTests {
implicit val iso: CartesianTests.Isomorphisms[Id] = CartesianTests.Isomorphisms.id[Id]
}
}
}
<<< higher kinded
import scala.concurrent.Future
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this line needed?

Copy link
Contributor

Choose a reason for hiding this comment

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

Remove?

Copy link
Author

@jvican jvican Jan 24, 2017

Choose a reason for hiding this comment

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

No, this line is needed. If you have a look at the shortened Future in the implicit val type result, you'll see that the import oracle correctly shortens the type because it finds it in the context.

Copy link
Contributor

Choose a reason for hiding this comment

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

Aha, gotcha. Maybe update the test name

package hello {
trait Id[F[_]]
object Id {
def ident[F[_]]: Id[F] = ???
}
}
object World {
import hello._
implicit val futureId = Id.ident[scala.concurrent.Future]
}
>>>
import scala.concurrent.Future
package hello {
trait Id[F[_]]
object Id {
def ident[F[_]]: Id[F] = ???
}
}
object World {
import hello._
implicit val futureId: Id[Future] = Id.ident[scala.concurrent.Future]
}
<<< path dependent type
class A {
trait C {
class B
}
val c = new C {}
implicit val x = new c.B
}
>>>
class A {
trait C {
class B
}
val c = new C {}
implicit val x: c.B = new c.B
}
<<< path dependent type II
class A {
object D {
val c = new C {}
}
trait C {
class B
}
implicit val x = new D.c.B
}
>>>
class A {
object D {
val c = new C {}
}
trait C {
class B
}
implicit val x: D.c.B = new D.c.B
Copy link
Contributor

Choose a reason for hiding this comment

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

Bravo 👏

Copy link
Author

Choose a reason for hiding this comment

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

💃

}
<<< renamed term in type selection
package E {
object D {
class C
}
}
class A {
import E.{D => d}
implicit val x = new d.C
}
>>>
package E {
object D {
class C
}
}
class A {
import E.{D => d}
implicit val x: d.C = new d.C
}
<<< renamed term in deeper type selection
package E {
object D {
object C {
class B
}
}
}
class A {
import E.{D => d}
import d.{C => c}
implicit val x = new c.B
}
>>>
package E {
object D {
object C {
class B
}
}
}
class A {
import E.{D => d}
import d.{C => c}
implicit val x: c.B = new c.B
}
<<< two classes
class A[T](e: T)
class B {
Expand Down Expand Up @@ -172,15 +349,15 @@ object A {
implicit val x: List[D.B] = List(new D.B)
}
}
<<< SKIP slick tuple
<<< slick tuple
object slick {
case class Supplier(id: Int, name: String)
implicit val supplierGetter = (arg: (Int, String)) => Supplier(arg._1, arg._2)
}
>>>
object slick {
case class Supplier(id: Int, name: String)
implicit val supplierGetter: ((Int, String)) => slick.Supplier = (arg: (Int, String)) => Supplier(arg._1, arg._2)
implicit val supplierGetter: ((Int, String)) => Supplier = (arg: (Int, String)) => Supplier(arg._1, arg._2)
}
<<< NOWRAP package import
package scala.concurrent {
Copy link
Contributor

Choose a reason for hiding this comment

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

Some tricky examples that come to mind:

Copy link
Contributor

Choose a reason for hiding this comment

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

package a {
  class C
}

package b {
  class C
}

package c {
  object M {
    import a._
    import b._
    implicit val c = new b.C
  }
}

Copy link
Contributor

Choose a reason for hiding this comment

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

trait T {
  class C
}

package object a extends T {
}

object M {
  import a._
  implicit val c = new C
}

Copy link
Contributor

Choose a reason for hiding this comment

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

package a {
  class C
}

package b {
  class C
}

package c {
  object M {
    import a._
    locally {
      import b._
      implicit val c = new C
    }
  }
}

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the code snippets, will add to tests. Forgot about our beloved locally 😄.

Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome tests! Very nice 👍

Expand Down
Loading