Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package sbt
package internal
package fix
import scalafix.v1._
import scala.meta._
class Sbt0_13BuildSyntax extends SyntacticRule("Sbt0_13BuildSyntax") {
override def fix(implicit doc: SyntacticDocument): Patch = {
doc.tree.collect {
case t: Term.ApplyInfix if t.op.value == "in" && t.lhs.toString != "project" =>
slashify(t, t.lhs, t.args)
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
}
}
@SethTisue

This comment has been minimized.

Copy link

@SethTisue SethTisue commented Apr 13, 2021

The attempt to restrict what forms of in get rewritten didn't work in this example:

-lazy val `play-functional` = crossProject(JVMPlatform, JSPlatform)
-  .crossType(CrossType.Pure)
-  .in(file("play-functional"))
+lazy val `play-functional` = (file("play-functional") / crossProject(JVMPlatform, JSPlatform)
+  .crossType(CrossType.Pure))

no big deal, just something for users to watch out for.

@SethTisue

This comment has been minimized.

Copy link

@SethTisue SethTisue commented Apr 13, 2021

Other than that, the only shortcoming I've noticed is that the rewrite often introduces unnecessary parentheses. I have no idea whether Scalafix offers any way of avoiding that.

This rewrite has been super helpful to me in multiple repos. 🙇

@eed3si9n

This comment has been minimized.

Copy link
Owner Author

@eed3si9n eed3si9n commented Apr 13, 2021

@xuwei-k had an idea of putting parens only at the call site of .value.
https://twitter.com/xuwei_k/status/1361554682276057090

I haven't experimented with that yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment