-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cats.data.Xor/XorT to Either/EitherT rewrite (+7 squashed commits)
Squashed commits: [b5aaec5] Pushing a few experiments for feedback. [445b2cd] Experiment using desugared to rewrite xor [13935bf] WIP [40caaa3] Pick up configuration in sbt plugin. [ab34355] Use build-info for friendlier intellij setup [a5b666a] Setup hocon configuration. [c94fef1] Cross-build to 2.12 (#28) * Upgrade scalameta and cross-build to 2.12 * Gracefully handle 2.11 and 2.12 in sbt plugin. * Don't rely on sbt.ivy.home.
- Loading branch information
1 parent
efdbe17
commit 5d9bf0d
Showing
38 changed files
with
689 additions
and
163 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 was deleted.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package scalafix.rewrite | ||
|
||
import scala.collection.immutable.Seq | ||
import scala.{meta => m} | ||
import scalafix.util._ | ||
import scala.meta._ | ||
|
||
case object Xor2Either extends Rewrite { | ||
override def rewrite(ast: m.Tree, ctx: RewriteCtx): Seq[Patch] = { | ||
implicit val semanticApi: SemanticApi = getSemanticApi(ctx) | ||
|
||
//Create a sequence of type replacements | ||
val replacementTypes = List( | ||
ReplaceType(t"cats.data.XorT", t"cats.data.EitherT", "EitherT"), | ||
ReplaceType(t"cats.data.Xor", t"scala.util.Either", "Either"), | ||
ReplaceType(t"cats.data.Xor.Left", t"scala.util.Left", "Left"), | ||
ReplaceType(t"cats.data.Xor.Right", t"scala.util.Either.Right", "Right") | ||
) | ||
|
||
//Add in some method replacements | ||
val replacementTerms = List( | ||
ReplaceTerm(q"cats.data.Xor.Right.apply", | ||
q"scala.util.Right.apply", | ||
q"scala.util"), | ||
ReplaceTerm(q"cats.data.Xor.Left.apply", | ||
q"scala.util.Left.apply", | ||
q"scala.util") | ||
) | ||
|
||
//Then add needed imports. | ||
//todo - derive this from patches created, types | ||
//and terms replaced | ||
//Only add if they are not already imported | ||
val additionalImports = List( | ||
"cats.data.EitherT", | ||
"cats.implicits._", | ||
"scala.util.Either" | ||
) | ||
|
||
val typeReplacements = | ||
new ChangeType(ast).gatherPatches(replacementTypes) | ||
|
||
val termReplacements = | ||
new ChangeMethod(ast).gatherPatches(replacementTerms) | ||
|
||
//Make this additional imports, beyond what can be derived from the types | ||
val addedImports = | ||
if (typeReplacements.isEmpty && termReplacements.isEmpty) Seq[Patch]() | ||
else new AddImport(ast).gatherPatches(additionalImports) | ||
|
||
addedImports ++ typeReplacements ++ termReplacements | ||
} | ||
} |
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,37 @@ | ||
package scalafix.util | ||
|
||
import scala.collection.immutable.Seq | ||
import scala.{meta => m} | ||
import scala.meta._ | ||
import scalafix.rewrite._ | ||
|
||
class AddImport(ast: m.Tree)(implicit sApi: SemanticApi) { | ||
val allImports = ast.collect { | ||
case t @ q"import ..$importersnel" => t -> importersnel | ||
} | ||
|
||
val firstImport = allImports.headOption | ||
val firstImportFirstToken = firstImport.flatMap { | ||
case (importStatement, _) => importStatement.tokens.headOption | ||
} | ||
val tokenBeforeFirstImport = firstImportFirstToken.flatMap { stopAt => | ||
ast.tokens.takeWhile(_ != stopAt).lastOption | ||
} | ||
|
||
//This is currently a very dumb implementation. | ||
//It does no checking for existing imports and makes | ||
//no attempt to consolidate imports | ||
def addedImports(importString: String): Seq[Patch] = | ||
tokenBeforeFirstImport | ||
.map( | ||
beginImportsLocation => | ||
Patch | ||
.insertAfter(beginImportsLocation, importString) | ||
) | ||
.toList | ||
|
||
def gatherPatches(imports: Seq[String]): Seq[Patch] = { | ||
val importStrings = imports.map("import " + _).mkString("\n", "\n", "\n") | ||
addedImports(importStrings) | ||
} | ||
} |
Oops, something went wrong.