Skip to content

Commit 62d982c

Browse files
Rewrite collection.{Map, Set}.+ and collection.Set.-
This is incomplete since, it's blocked by scalameta/scalameta#1212 (Add support to query type of a term) iset: immutable.Set[Int] cset: collecion.Set[Int] both have +/- implemented via SetLike given iset + 1 cset + 1 we know that + is from SetLike, but it's not possible to get the type of iset/cset
1 parent 408c54d commit 62d982c

File tree

3 files changed

+75
-14
lines changed

3 files changed

+75
-14
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@ rule = "scala:fix.Scalacollectioncompat_newcollections"
33
*/
44
package fix
55

6-
class SetMapSrc(set: Set[Int], map: Map[Int, Int]) {
7-
set + (2, 3)
8-
map + (2 -> 3, 3 -> 4)
9-
(set + (2, 3)).map(x => x)
10-
set + (2, 3) - 4
11-
}
6+
import scala.collection
7+
import scala.collection.immutable
8+
import scala.collection.mutable.{Map, Set} // Challenge to make shure the scoping is correct
9+
10+
class SetMapSrc(iset: immutable.Set[Int],
11+
cset: collection.Set[Int],
12+
imap: immutable.Map[Int, Int],
13+
cmap: collection.Map[Int, Int]) {
14+
iset + (2, 3)
15+
imap + (2 -> 3, 3 -> 4)
16+
(iset + (2, 3)).toString
17+
iset + (2, 3) - 4
18+
iset + 1 - 2
19+
cset + 1 - 2
20+
cmap + (2 -> 3) + ((4, 5))
21+
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@
33

44
package fix
55

6-
class SetMapSrc(set: Set[Int], map: Map[Int, Int]) {
7-
set + 2 + 3
8-
map + (2 -> 3) + (3 -> 4)
9-
(set + 2 + 3).map(x => x)
10-
set + 2 + 3 - 4
6+
import scala.collection
7+
import scala.collection.immutable
8+
import scala.collection.mutable.{Map, Set} // Challenge to make shure the scoping is correct
9+
10+
class SetMapSrc(iset: immutable.Set[Int],
11+
cset: collection.Set[Int],
12+
imap: immutable.Map[Int, Int],
13+
cmap: collection.Map[Int, Int]) {
14+
iset + 2 + 3
15+
imap + (2 -> 3) + (3 -> 4)
16+
(iset + 2 + 3).toString
17+
iset + 2 + 3 - 4
18+
iset + 1 - 2
19+
cset ++ _root_.scala.collection.Set(1) -- _root_.scala.collection.Set(2)
20+
cmap ++ _root_.scala.collection.Map(2 -> 3) ++ _root_.scala.collection.Map((4, 5))
1121
}

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

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
3030
Symbol("_root_.scala.runtime.Tuple2Zipped.Ops.zipped."),
3131
Symbol("_root_.scala.runtime.Tuple3Zipped.Ops.zipped.")
3232
)
33+
val setPlus =
34+
SymbolMatcher.exact(
35+
Symbol("_root_.scala.collection.SetLike#`+`(Ljava/lang/Object;)Lscala/collection/Set;.")
36+
)
37+
val setMinus =
38+
SymbolMatcher.exact(
39+
Symbol("_root_.scala.collection.SetLike#`-`(Ljava/lang/Object;)Lscala/collection/Set;.")
40+
)
41+
val mapPlus =
42+
SymbolMatcher.exact(
43+
Symbol("_root_.scala.collection.MapLike#`+`(Lscala/Tuple2;)Lscala/collection/Map;.")
44+
)
3345
val setPlus2 = SymbolMatcher.exact(
3446
Symbol("_root_.scala.collection.SetLike#`+`(Ljava/lang/Object;Ljava/lang/Object;Lscala/collection/Seq;)Lscala/collection/Set;.")
3547
)
@@ -72,6 +84,9 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
7284
Symbol("_root_.scala.collection.mutable.SetLike.retain.")
7385
)
7486

87+
def startsWithParens(tree: Tree): Boolean =
88+
tree.tokens.headOption.map(_.is[Token.LeftParen]).getOrElse(false)
89+
7590
def replaceMutableSet(ctx: RuleCtx) =
7691
ctx.tree.collect {
7792
case retainSet(n: Name) =>
@@ -182,7 +197,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
182197
def replaceSetMapPlus2(ctx: RuleCtx): Patch = {
183198
def rewritePlus(ap: Term.ApplyInfix, lhs: Term, op: Term.Name, rhs1: Term, rhs2: Term): Patch = {
184199
val tokensToReplace =
185-
if(ap.tokens.headOption.map(_.is[Token.LeftParen]).getOrElse(false)) {
200+
if(startsWithParens(ap)) {
186201
// don't drop surrounding parens
187202
ap.tokens.slice(1, ap.tokens.size - 1)
188203
} else ap.tokens
@@ -207,6 +222,33 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
207222
}.asPatch
208223
}
209224

225+
def replaceSetMapPlusMinus(ctx: RuleCtx): Patch = {
226+
def rewriteOp(op: Tree, rhs: Tree, doubleOp: String, col0: String): Patch = {
227+
val col = "_root_.scala.collection." + col0
228+
val callSite =
229+
if (startsWithParens(rhs)) {
230+
ctx.addLeft(rhs, col)
231+
}
232+
else {
233+
ctx.addLeft(rhs, col + "(") +
234+
ctx.addRight(rhs, ")")
235+
}
236+
237+
ctx.addRight(op, doubleOp) + callSite
238+
}
239+
240+
ctx.tree.collect {
241+
case Term.ApplyInfix(_, op @ setPlus(_), Nil, List(rhs)) =>
242+
rewriteOp(op, rhs, "+", "Set")
243+
244+
case Term.ApplyInfix(lhs, op @ setMinus(_), Nil, List(rhs)) =>
245+
rewriteOp(op, rhs, "-", "Set")
246+
247+
case Term.ApplyInfix(lhs, op @ mapPlus(_), Nil, List(rhs)) =>
248+
rewriteOp(op, rhs, "+", "Map")
249+
}.asPatch
250+
}
251+
210252
def replaceMutSetMapPlus(ctx: RuleCtx): Patch = {
211253
def rewriteMutPlus(lhs: Term, op: Term.Name): Patch = {
212254
ctx.addRight(lhs, ".clone()") +
@@ -241,8 +283,6 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
241283

242284

243285
override def fix(ctx: RuleCtx): Patch = {
244-
// println(ctx.index.database)
245-
246286
replaceToList(ctx) +
247287
replaceSymbols(ctx) +
248288
replaceTupleZipped(ctx) +
@@ -252,6 +292,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
252292
replaceMutableSet(ctx) +
253293
replaceSymbolicFold(ctx) +
254294
replaceSetMapPlus2(ctx) +
295+
replaceSetMapPlusMinus(ctx) +
255296
replaceMutSetMapPlus(ctx) +
256297
replaceMutMapUpdated(ctx) +
257298
replaceIterableSameElements(ctx)

0 commit comments

Comments
 (0)