From e8bcd2061f733c96d279e5437dbf29eedbc8695a Mon Sep 17 00:00:00 2001 From: Brice Jaglin Date: Fri, 15 Jul 2022 16:00:40 +0200 Subject: [PATCH] handle scalameta change in ApplyInfix RHS positions https://github.com/scalameta/scalameta/pull/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. --- README.md | 2 +- .../Collection213Experimental.scala | 33 +++++++++---------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index fd3fa5af..bce7b776 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/scalafix/rules/src/main/scala/scala/fix/collection/Collection213Experimental.scala b/scalafix/rules/src/main/scala/scala/fix/collection/Collection213Experimental.scala index 60df8b84..c1104ecc 100644 --- a/scalafix/rules/src/main/scala/scala/fix/collection/Collection213Experimental.scala +++ b/scalafix/rules/src/main/scala/scala/fix/collection/Collection213Experimental.scala @@ -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 }