Skip to content

Commit

Permalink
handle scalameta change in ApplyInfix RHS positions
Browse files Browse the repository at this point in the history
scalameta/scalameta#2684, first released in
scalameta 4.5.1 (scalafix 0.10.0), changed a behavior, so this takes a
more intrusive but also more robust approach so that the rule works
with scalafix 0.10.x.
  • Loading branch information
bjaglin committed Jul 15, 2022
1 parent a68f614 commit e8bcd20
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ The migration rules use scalafix. Please see the [official installation instruct

```scala
// project/plugins.sbt
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.34")
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.10.1")
```

### Collection213Upgrade
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,27 @@ case class Collection213ExperimentalV0(index: SemanticdbIndex)
}

def replaceSetMapPlusMinus(ctx: RuleCtx): Patch = {
def rewriteOp(op: Tree, rhs: Tree, doubleOp: String, col0: String): Patch = {
val col = "_root_.scala.collection." + col0
val callSite =
if (startsWithParens(rhs)) {
ctx.addLeft(rhs, col)
} else {
ctx.addLeft(rhs, col + "(") +
ctx.addRight(rhs, ")")
}

ctx.addRight(op, doubleOp) + callSite
def rewriteOp(ap: Term.ApplyInfix, doubleOp: String, col0: String): Patch = {
val col = col0 match {
case "Set" => q"_root_.scala.collection.Set"
case "Map" => q"_root_.scala.collection.Map"
}
val newAp = ap.copy(
args = Term.Apply(col, ap.args) :: Nil,
op = Term.Name(doubleOp*2)
).toString()
ctx.replaceTree(ap, newAp)
}

ctx.tree.collect {
case ap @ Term.ApplyInfix(CollectionSet(), op @ setPlus(_), Nil, List(rhs)) =>
rewriteOp(op, rhs, "+", "Set")
case ap @ Term.ApplyInfix(CollectionSet(), setPlus(_), Nil, _) =>
rewriteOp(ap, "+", "Set")

case Term.ApplyInfix(CollectionSet(), op @ setMinus(_), Nil, List(rhs)) =>
rewriteOp(op, rhs, "-", "Set")
case ap @ Term.ApplyInfix(CollectionSet(), setMinus(_), Nil, _) =>
rewriteOp(ap, "-", "Set")

case Term.ApplyInfix(_, op @ mapPlus(_), Nil, List(rhs)) =>
rewriteOp(op, rhs, "+", "Map")
case ap @ Term.ApplyInfix(_, op @ mapPlus(_), Nil, _) =>
rewriteOp(ap, "+", "Map")
}.asPatch
}

Expand Down

0 comments on commit e8bcd20

Please sign in to comment.