Skip to content

Commit 878ab35

Browse files
Rewrite Map.zip. make sure we target Map
1 parent f1a0885 commit 878ab35

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ class SetMapSrc(set: Set[Int], map: Map[Int, Int]) {
1010
set + (2, 3) - 4
1111
map.mapValues(_ + 1)
1212
map.zip(List())
13+
List().zip(List())
1314
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ class SetMapSrc(set: Set[Int], map: Map[Int, Int]) {
1010
set + 2 + 3 - 4
1111
map.mapValues(_ + 1).toMap
1212
map.zip(List()).toMap
13+
List().zip(List())
1314
}

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
2626
close <- ctx.matchingParens.close(open)
2727
} yield (open, close)
2828

29+
// terms dont give us terms https://github.com/scalameta/scalameta/issues/1212
30+
// if we have a simple identifier, we can look at his definition at query it's type
31+
// this should be improved in future version of scalameta
32+
object TypeMatcher {
33+
def apply(symbols: Symbol*)(implicit index: SemanticdbIndex): TypeMatcher =
34+
new TypeMatcher(symbols: _*)(index)
35+
}
36+
37+
final class TypeMatcher(symbols: Symbol*)(implicit index: SemanticdbIndex) {
38+
def unapply(tree: Tree): Boolean = {
39+
index.denotation(tree)
40+
.map(_.names.headOption.exists(n => symbols.exists(_ == n.symbol)))
41+
.getOrElse(false)
42+
}
43+
}
44+
45+
val CollectionMap: TypeMatcher = TypeMatcher(
46+
Symbol("_root_.scala.collection.immutable.Map#"),
47+
Symbol("_root_.scala.collection.mutable.Map#"),
48+
Symbol("_root_.scala.Predef.Map#")
49+
)
50+
2951
def replaceSymbols(ctx: RuleCtx): Patch = {
3052
ctx.replaceSymbols(
3153
"scala.collection.LinearSeq" -> "scala.collection.immutable.List",
@@ -290,7 +312,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
290312

291313
def replaceMapZip(ctx: RuleCtx): Patch = {
292314
ctx.tree.collect {
293-
case ap @ Term.Apply(Term.Select(_, mapZip(_)), List(_)) =>
315+
case ap @ Term.Apply(Term.Select(CollectionMap(), mapZip(_)), List(_)) =>
294316
ctx.addRight(ap, ".toMap")
295317
}.asPatch
296318
}

0 commit comments

Comments
 (0)