Skip to content

Commit 4c51cf3

Browse files
Rewrite immutable.Set/Map.+ corner case
1 parent 07db055 commit 4c51cf3

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

scalafix/input/src/main/scala/fix/SetMapSrc.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ package fix
66
class SetMapSrc(set: Set[Int], map: Map[Int, Int]) {
77
set + (2, 3)
88
map + (2 -> 3, 3 -> 4)
9+
(set + (2, 3)).map(x => x)
10+
set + (2, 3) - 4
911
}

scalafix/output/src/main/scala/fix/SetMapSrc.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ package fix
66
class SetMapSrc(set: Set[Int], map: Map[Int, Int]) {
77
set + 2 + 3
88
map + (2 -> 3) + (3 -> 4)
9+
(set + 2 + 3).map(x => x)
10+
set + 2 + 3 - 4
911
}

scalafix/rules/src/main/scala/fix/Scalacollectioncompat_newcollections.scala

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,23 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
165165

166166
def replaceSetMapPlus2(ctx: RuleCtx): Patch = {
167167
def rewritePlus(ap: Term.ApplyInfix, lhs: Term, op: Term.Name, rhs1: Term, rhs2: Term): Patch = {
168-
ctx.replaceTree(
169-
ap,
168+
169+
val tokensToReplace =
170+
if(ap.tokens.headOption.map(_.is[Token.LeftParen]).getOrElse(false)) {
171+
// don't drop surrounding parens
172+
ap.tokens.slice(1, ap.tokens.size - 1)
173+
} else ap.tokens
174+
175+
val newTree =
170176
Term.ApplyInfix(
171177
Term.ApplyInfix(lhs, op, Nil, List(rhs1)),
172178
op,
173179
Nil,
174180
List(rhs2)
175-
).toString
176-
)
181+
).syntax
182+
183+
ctx.removeTokens(tokensToReplace) +
184+
tokensToReplace.headOption.map(x => ctx.addRight(x, newTree))
177185
}
178186

179187
ctx.tree.collect {

0 commit comments

Comments
 (0)