Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Forked from eed3si9n/Sbt0_13BuildSyntax.scala
Last active February 16, 2021 05:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xuwei-k/d1ae2f6cdd960326648fe23b5c0385c6 to your computer and use it in GitHub Desktop.
Save xuwei-k/d1ae2f6cdd960326648fe23b5c0385c6 to your computer and use it in GitHub Desktop.
package sbt
package internal
package fix
import scalafix.v1._
import scala.meta._
class Sbt0_13BuildSyntax extends SyntacticRule("Sbt0_13BuildSyntax") {
private def maybeOldSyntax(tree: Tree): Boolean = {
tree match {
case t: Term.ApplyInfix if t.op.value == "in" && t.lhs.toString != "project" =>
true
case _ =>
false
}
}
override def fix(implicit doc: SyntacticDocument): Patch = {
doc.tree.collect {
case t: Term.ApplyInfix if maybeOldSyntax(t) =>
t.lhs match {
case t2: Term.ApplyInfix if maybeOldSyntax(t2) =>
(t.args, t2.args) match {
case (List(arg0), List(args1)) =>
Patch.replaceTree(t, s"($arg0 / $args1 / ${t2.lhs})")
case _ =>
Patch.empty
}
case _ if !t.parent.exists(maybeOldSyntax) =>
slashify(t, t.lhs, t.args)
case _ =>
Patch.empty
}
case t @ Term.Apply(Term.Select(qual, Term.Name("in")), args) if qual.toString != "project" =>
slashify(t, qual, args)
}.asPatch
}
def slashify(t: Tree, lhs: Term, args: Seq[Term]): Patch =
args match {
case List(arg0) =>
Patch.replaceTree(t, s"($arg0 / $lhs)")
case List(arg0, arg1) =>
Patch.replaceTree(t, s"($arg0 / $arg1 / $lhs)")
case List(arg0, arg1, arg2) =>
Patch.replaceTree(t, s"($arg0 / $arg1 / $arg2 / $lhs)")
case _ => Patch.empty
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment