forked from scalacenter/scalafix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experiment using desugared to rewrite xor
- Loading branch information
Showing
5 changed files
with
55 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,28 @@ | ||
package scalafix.rewrite | ||
|
||
import scala.meta.parsers.Parse | ||
import scala.{meta => m} | ||
import scalafix.util.Patch | ||
import scalafix.util.Whitespace | ||
import scalafix.util.logger | ||
|
||
case object Xor2Either extends Rewrite { | ||
override def rewrite(ast: m.Tree, ctx: RewriteCtx): Seq[Patch] = { | ||
import scala.meta._ | ||
val semantic = getSemanticApi(ctx) | ||
class Desugared[T <: Tree: Parse] { | ||
def unapply(original: T): Option[T] = semantic.desugared(original) | ||
} | ||
object DType extends Desugared[Type] | ||
object DTerm extends Desugared[Term] | ||
// NOTE. This approach is super inefficient, since we run semantic.desugar on | ||
// every case for every node in the tree. Ideally, we first match on the | ||
// syntax structure we want and then run semantic.desugar. | ||
ast.collect { | ||
case t: m.Type.Ref | ||
if semantic | ||
.desugared(t: m.Type)(m.parsers.Parse.parseType) | ||
.exists(_.syntax.contains("Xor")) => | ||
logger.elem(t.syntax, semantic.desugared(t: Type)) | ||
val tok = t.tokens.head | ||
Seq( | ||
Patch(tok, tok, tok.syntax + "BANANA") | ||
) | ||
}.flatten | ||
case t @ DType(t"cats.data.Xor") => | ||
Patch(t.tokens.head, t.tokens.last, s"Either") | ||
case t @ DTerm(q"cats.data.Xor.Right.apply[..$_]") => | ||
Patch(t.tokens.head, t.tokens.last, s"Right") | ||
case t @ DTerm(q"cats.data.Xor.Left.apply[..$_]") => | ||
Patch(t.tokens.head, t.tokens.last, s"Left") | ||
} | ||
} | ||
} |
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,10 +1,15 @@ | ||
rewrites = [Xor2Either] | ||
<<< ONLY xor 1 | ||
<<< xor 1 | ||
import cats.data.Xor | ||
trait A { | ||
val r: Xor[Int, String] | ||
val r: Xor[Int, String] = Xor.Right("") | ||
val s: Xor[Int, String] = Xor.Left(1 /* comment */) | ||
val nest: Seq[Xor[Int, cats.data.Xor[String, Int]]] | ||
} | ||
>>> | ||
import cats.data.Xor | ||
trait A { | ||
val r: Either[Int, String] | ||
val r: Either[Int, String] = Right("") | ||
val s: Either[Int, String] = Left(1 /* comment */) | ||
val nest: Seq[Either[Int, Either[String, Int]]] | ||
} |
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
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