@@ -26,6 +26,31 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
26
26
close <- ctx.matchingParens.close(open)
27
27
} yield (open, close)
28
28
29
+ // terms dont give us terms https://github.com/scalameta/scalameta/issues/1212
30
+ // WARNING: TOTAL HACK
31
+ // this is only to unblock us until Term.tpe is available: https://github.com/scalameta/scalameta/issues/1212
32
+ // if we have a simple identifier, we can look at his definition at query it's type
33
+ // this should be improved in future version of scalameta
34
+ object TypeMatcher {
35
+ def apply (symbols : Symbol * )(implicit index : SemanticdbIndex ): TypeMatcher =
36
+ new TypeMatcher (symbols : _* )(index)
37
+ }
38
+
39
+ final class TypeMatcher (symbols : Symbol * )(implicit index : SemanticdbIndex ) {
40
+ def unapply (tree : Tree ): Boolean = {
41
+ index.denotation(tree)
42
+ .exists(_.names.headOption.exists(n => symbols.exists(_ == n.symbol)))
43
+ }
44
+ }
45
+
46
+ val CollectionMap : TypeMatcher = TypeMatcher (
47
+ Symbol (" _root_.scala.collection.immutable.Map#" ),
48
+ Symbol (" _root_.scala.collection.mutable.Map#" ),
49
+ Symbol (" _root_.scala.Predef.Map#" )
50
+ )
51
+
52
+ val CollectionSet : TypeMatcher = TypeMatcher (Symbol (" _root_.scala.collection.Set#" ))
53
+
29
54
def replaceSymbols (ctx : RuleCtx ): Patch = {
30
55
ctx.replaceSymbols(
31
56
" scala.collection.LinearSeq" -> " scala.collection.immutable.List" ,
@@ -110,11 +135,15 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
110
135
Symbol (" _root_.scala.collection.mutable.SetLike.retain." )
111
136
)
112
137
138
+
113
139
val arrayBuilderMake =
114
140
SymbolMatcher .normalized(
115
141
Symbol (" _root_.scala.collection.mutable.ArrayBuilder.make(Lscala/reflect/ClassTag;)Lscala/collection/mutable/ArrayBuilder;." )
116
142
)
117
143
144
+ def startsWithParens (tree : Tree ): Boolean =
145
+ tree.tokens.headOption.map(_.is[Token .LeftParen ]).getOrElse(false )
146
+
118
147
def replaceMutableSet (ctx : RuleCtx ) =
119
148
ctx.tree.collect {
120
149
case retainSet(n : Name ) =>
@@ -216,7 +245,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
216
245
def replaceSetMapPlus2 (ctx : RuleCtx ): Patch = {
217
246
def rewritePlus (ap : Term .ApplyInfix , lhs : Term , op : Term .Name , rhs1 : Term , rhs2 : Term ): Patch = {
218
247
val tokensToReplace =
219
- if (ap.tokens.headOption.map(_.is[ Token . LeftParen ]).getOrElse( false )) {
248
+ if (startsWithParens(ap )) {
220
249
// don't drop surrounding parens
221
250
ap.tokens.slice(1 , ap.tokens.size - 1 )
222
251
} else ap.tokens
@@ -278,10 +307,11 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
278
307
case ap @ Term .Apply (at @ Term .ApplyType (Term .Select (lhs, arrayBuilderMake(_)), args), Nil ) =>
279
308
val extraParens =
280
309
ap.tokens.slice(at.tokens.size, ap.tokens.size)
281
-
282
310
ctx.removeTokens(extraParens)
283
311
}.asPatch
284
312
}
313
+
314
+
285
315
286
316
def replaceMapMapValues (ctx : RuleCtx ): Patch = {
287
317
ctx.tree.collect {
@@ -485,7 +515,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
485
515
replaceMutSetMapPlus(ctx) +
486
516
replaceMutMapUpdated(ctx) +
487
517
replaceArrayBuilderMake(ctx) +
488
- replaceMapMapValues (ctx) +
489
- replaceIterableSameElements (ctx)
518
+ replaceIterableSameElements (ctx) +
519
+ replaceMapMapValues (ctx)
490
520
}
491
521
}
0 commit comments