@@ -8,6 +8,24 @@ import scala.meta._
8
8
case class Scalacollectioncompat_newcollections (index : SemanticdbIndex )
9
9
extends SemanticRule (index, " Scalacollectioncompat_newcollections" ) {
10
10
11
+ // WARNING: TOTAL HACK
12
+ // this is only to unblock us until Term.tpe is available: https://github.com/scalameta/scalameta/issues/1212
13
+ // if we have a simple identifier, we can look at his definition at query it's type
14
+ // this should be improved in future version of scalameta
15
+ object TypeMatcher {
16
+ def apply (symbols : Symbol * )(implicit index : SemanticdbIndex ): TypeMatcher =
17
+ new TypeMatcher (symbols : _* )(index)
18
+ }
19
+
20
+ final class TypeMatcher (symbols : Symbol * )(implicit index : SemanticdbIndex ) {
21
+ def unapply (tree : Tree ): Boolean = {
22
+ index.denotation(tree)
23
+ .exists(_.names.headOption.exists(n => symbols.exists(_ == n.symbol)))
24
+ }
25
+ }
26
+
27
+ val CollectionSet : TypeMatcher = TypeMatcher (Symbol (" _root_.scala.collection.Set#" ))
28
+
11
29
def replaceSymbols (ctx : RuleCtx ): Patch = {
12
30
ctx.replaceSymbols(
13
31
" scala.collection.LinearSeq" -> " scala.collection.immutable.List" ,
@@ -30,6 +48,18 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
30
48
Symbol (" _root_.scala.runtime.Tuple2Zipped.Ops.zipped." ),
31
49
Symbol (" _root_.scala.runtime.Tuple3Zipped.Ops.zipped." )
32
50
)
51
+ val setPlus =
52
+ SymbolMatcher .exact(
53
+ Symbol (" _root_.scala.collection.SetLike#`+`(Ljava/lang/Object;)Lscala/collection/Set;." )
54
+ )
55
+ val setMinus =
56
+ SymbolMatcher .exact(
57
+ Symbol (" _root_.scala.collection.SetLike#`-`(Ljava/lang/Object;)Lscala/collection/Set;." )
58
+ )
59
+ val mapPlus =
60
+ SymbolMatcher .exact(
61
+ Symbol (" _root_.scala.collection.MapLike#`+`(Lscala/Tuple2;)Lscala/collection/Map;." )
62
+ )
33
63
val setPlus2 = SymbolMatcher .exact(
34
64
Symbol (" _root_.scala.collection.SetLike#`+`(Ljava/lang/Object;Ljava/lang/Object;Lscala/collection/Seq;)Lscala/collection/Set;." )
35
65
)
@@ -82,6 +112,9 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
82
112
Symbol (" _root_.scala.collection.mutable.ArrayBuilder.make(Lscala/reflect/ClassTag;)Lscala/collection/mutable/ArrayBuilder;." )
83
113
)
84
114
115
+ def startsWithParens (tree : Tree ): Boolean =
116
+ tree.tokens.headOption.map(_.is[Token .LeftParen ]).getOrElse(false )
117
+
85
118
def replaceMutableSet (ctx : RuleCtx ) =
86
119
ctx.tree.collect {
87
120
case retainSet(n : Name ) =>
@@ -192,7 +225,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
192
225
def replaceSetMapPlus2 (ctx : RuleCtx ): Patch = {
193
226
def rewritePlus (ap : Term .ApplyInfix , lhs : Term , op : Term .Name , rhs1 : Term , rhs2 : Term ): Patch = {
194
227
val tokensToReplace =
195
- if (ap.tokens.headOption.map(_.is[ Token . LeftParen ]).getOrElse( false )) {
228
+ if (startsWithParens(ap )) {
196
229
// don't drop surrounding parens
197
230
ap.tokens.slice(1 , ap.tokens.size - 1 )
198
231
} else ap.tokens
@@ -217,6 +250,34 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
217
250
}.asPatch
218
251
}
219
252
253
+ def replaceSetMapPlusMinus (ctx : RuleCtx ): Patch = {
254
+ def rewriteOp (op : Tree , rhs : Tree , doubleOp : String , col0 : String ): Patch = {
255
+ val col = " _root_.scala.collection." + col0
256
+ val callSite =
257
+ if (startsWithParens(rhs)) {
258
+ ctx.addLeft(rhs, col)
259
+ }
260
+ else {
261
+ ctx.addLeft(rhs, col + " (" ) +
262
+ ctx.addRight(rhs, " )" )
263
+ }
264
+
265
+ ctx.addRight(op, doubleOp) + callSite
266
+ }
267
+
268
+ ctx.tree.collect {
269
+ case Term .ApplyInfix (CollectionSet (), op @ setPlus(_), Nil , List (rhs)) =>
270
+ rewriteOp(op, rhs, " +" , " Set" )
271
+
272
+ case Term .ApplyInfix (CollectionSet (), op @ setMinus(_), Nil , List (rhs)) =>
273
+ rewriteOp(op, rhs, " -" , " Set" )
274
+
275
+ case Term .ApplyInfix (_, op @ mapPlus(_), Nil , List (rhs)) =>
276
+ rewriteOp(op, rhs, " +" , " Map" )
277
+ }.asPatch
278
+ }
279
+
280
+
220
281
def replaceMutSetMapPlus (ctx : RuleCtx ): Patch = {
221
282
def rewriteMutPlus (lhs : Term , op : Term .Name ): Patch = {
222
283
ctx.addRight(lhs, " .clone()" ) +
@@ -265,7 +326,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
265
326
ctx.addRight(ap, " .toMap" )
266
327
}.asPatch
267
328
}
268
-
329
+
269
330
override def fix (ctx : RuleCtx ): Patch = {
270
331
replaceToList(ctx) +
271
332
replaceSymbols(ctx) +
@@ -276,6 +337,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
276
337
replaceMutableSet(ctx) +
277
338
replaceSymbolicFold(ctx) +
278
339
replaceSetMapPlus2(ctx) +
340
+ replaceSetMapPlusMinus(ctx) +
279
341
replaceMutSetMapPlus(ctx) +
280
342
replaceMutMapUpdated(ctx) +
281
343
replaceIterableSameElements(ctx) +
0 commit comments