-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from Dwolla/downstream-rewrite-rule
- Loading branch information
Showing
8 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
scalafix/input-dependency/src/main/scala/example/foo/FooService.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* this is a pared-down version of the code that used to be generated | ||
* by scrooge for a hypothetical "Foo" Thrift interface, containing | ||
* a single service named "Foo", with a method named "bar". | ||
* | ||
* It exists because we need the input for the AdaptHigherKindedThriftCode | ||
* rule to compile against the old structure. It's in a separate submodule | ||
* because we don't want any of our Scalafix rules to modify it, and we | ||
* don't want it to be available in this form when compiling the Scalafix | ||
* output module. | ||
*/ | ||
|
||
package example.foo | ||
|
||
import com.twitter.util.Future | ||
|
||
@javax.annotation.Generated(value = Array("com.twitter.scrooge.Compiler")) | ||
trait FooService[+MM[_]] extends _root_.com.twitter.finagle.thrift.ThriftService { | ||
def bar: MM[Unit] | ||
} | ||
|
||
object FooService { | ||
trait MethodPerEndpoint extends FooService[Future] | ||
|
||
implicit def FooServiceInReaderT[F[_]]: FooService[({type Λ[β0] = _root_.cats.data.ReaderT[F, FooService[F], β0]})#Λ] = | ||
_root_.cats.tagless.Derive.readerT[FooService, F] | ||
|
||
implicit val FooServiceFunctorK: _root_.cats.tagless.FunctorK[FooService] = _root_.cats.tagless.Derive.functorK[FooService] | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
scalafix/input/src/main/scala/example/dwolla/UsesFooService.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/*rule = AdaptHigherKindedThriftCode*/ | ||
package example.dwolla | ||
|
||
import cats.tagless.FunctorK | ||
import example.foo.FooService | ||
|
||
class UsesFooService[F[_]](val fooService: example.foo.FooService[F]) { | ||
implicitly[FunctorK[FooService]] | ||
} |
26 changes: 26 additions & 0 deletions
26
scalafix/output-dependency/src/main/scala/example/foo/FooService.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* this is a pared-down version of the code that will be generated | ||
* by scrooge, and then modified by our AddCatsTaglessInstances rule, | ||
* for a hypothetical "Foo" Thrift interface, containing a single | ||
* service named "Foo", with a method named "bar". | ||
*/ | ||
|
||
package example.foo | ||
|
||
import com.twitter.util.Future | ||
|
||
object FooService { | ||
trait FooService[F[_]] { | ||
def bar: F[Unit] | ||
} | ||
|
||
object FooService { | ||
implicit def FooServiceInReaderT[F[_]]: FooService[({type Λ[β0] = _root_.cats.data.ReaderT[F, FooService[F], β0]})#Λ] = | ||
_root_.cats.tagless.Derive.readerT[FooService, F] | ||
|
||
implicit val FooServiceFunctorK: _root_.cats.tagless.FunctorK[FooService] = _root_.cats.tagless.Derive.functorK[FooService] | ||
|
||
} | ||
|
||
trait MethodPerEndpoint extends FooService[Future] | ||
} |
8 changes: 8 additions & 0 deletions
8
scalafix/output/src/main/scala/example/dwolla/UsesFooService.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package example.dwolla | ||
|
||
import cats.tagless.FunctorK | ||
import example.foo.FooService | ||
|
||
class UsesFooService[F[_]](val fooService: example.foo.FooService.FooService[F]) { | ||
implicitly[FunctorK[FooService.FooService]] | ||
} |
1 change: 1 addition & 0 deletions
1
scalafix/rules/src/main/resources/META-INF/services/scalafix.v1.Rule
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
com.dwolla.scrooge.scalafix.AddCatsTaglessInstances | ||
com.dwolla.scrooge.scalafix.AdaptHigherKindedThriftCode |
49 changes: 49 additions & 0 deletions
49
scalafix/rules/src/main/scala/com/dwolla/scrooge/scalafix/AdaptHigherKindedThriftCode.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.dwolla.scrooge.scalafix | ||
|
||
import com.dwolla.scrooge.scalafix.AdaptHigherKindedThriftCode._ | ||
import scalafix.v1 | ||
import scalafix.v1._ | ||
|
||
import scala.meta._ | ||
|
||
class AdaptHigherKindedThriftCode extends SemanticRule("AdaptHigherKindedThriftCode") { | ||
override def fix(implicit doc: SemanticDocument): Patch = | ||
addObjectQualifierForThriftServiceTrait(doc.tree) | ||
} | ||
|
||
object AdaptHigherKindedThriftCode { | ||
private def isGeneratedThriftService(t: Type.Name) | ||
(implicit doc: SemanticDocument): Boolean = { | ||
val info = t.symbol.info | ||
|
||
val annotatedWithGenerated = info.exists(_.annotations.exists(_.tpe.toString() == "Generated")) | ||
|
||
val anyRef = v1.Symbol("scala/AnyRef#") | ||
val thriftService = Symbol("com/twitter/finagle/thrift/ThriftService#") | ||
|
||
val extendsThriftServiceAndIsHigherKinded = | ||
info | ||
.map(_.signature) | ||
.collect { | ||
|
||
// class that extends AnyRef and com.twitter.finagle.thrift.ThriftService, and has a single type parameter | ||
// e.g. FooService[F[_]] extends com.twitter.finagle.thrift.ThriftService | ||
case ClassSignature(List(singleTypeParameter), List(TypeRef(NoType, `anyRef`, Nil), TypeRef(NoType, `thriftService`, Nil)), _, _) => singleTypeParameter.signature | ||
} | ||
.collect { | ||
// type parameter that has a single hole, e.g. F[_] | ||
case TypeSignature(List(_), _, _) => true | ||
} | ||
.getOrElse(false) | ||
|
||
annotatedWithGenerated && extendsThriftServiceAndIsHigherKinded | ||
} | ||
|
||
def addObjectQualifierForThriftServiceTrait(tree: Tree) | ||
(implicit doc: SemanticDocument): Patch = | ||
tree.collect { | ||
case t@Type.Name(name) if isGeneratedThriftService(t) => | ||
Patch.addLeft(t, s"$name.") | ||
} | ||
.fold(Patch.empty)(_ + _) | ||
} |