Skip to content

Commit 1e993e1

Browse files
Rewrite Map.zip. make shure we target Map
1 parent d25861f commit 1e993e1

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ class SetMapSrc(set: Set[Int], map: Map[Int, Int]) {
99
(set + (2, 3)).map(x => x)
1010
set + (2, 3) - 4
1111
map.zip(List())
12+
List().zip(List())
1213
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ class SetMapSrc(set: Set[Int], map: Map[Int, Int]) {
99
(set + 2 + 3).map(x => x)
1010
set + 2 + 3 - 4
1111
map.zip(List()).toMap
12+
List().zip(List())
1213
}

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

+23-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@ import scala.meta._
88
case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
99
extends SemanticRule(index, "Scalacollectioncompat_newcollections") {
1010

11+
// terms dont give us terms https://github.com/scalameta/scalameta/issues/1212
12+
// if we have a simple identifier, we can look at his definition at query it's type
13+
// this should be improved in future version of scalameta
14+
object TypeMatcher {
15+
def apply(symbols: Symbol*)(implicit index: SemanticdbIndex): TypeMatcher =
16+
new TypeMatcher(symbols: _*)(index)
17+
}
18+
19+
final class TypeMatcher(symbols: Symbol*)(implicit index: SemanticdbIndex) {
20+
def unapply(tree: Tree): Boolean = {
21+
index.denotation(tree)
22+
.map(_.names.headOption.exists(n => symbols.exists(_ == n.symbol)))
23+
.getOrElse(false)
24+
}
25+
}
26+
27+
val CollectionMap: TypeMatcher = TypeMatcher(
28+
Symbol("_root_.scala.collection.immutable.Map#"),
29+
Symbol("_root_.scala.collection.mutable.Map#"),
30+
Symbol("_root_.scala.Predef.Map#")
31+
)
32+
1133
def replaceSymbols(ctx: RuleCtx): Patch = {
1234
ctx.replaceSymbols(
1335
"scala.Stream" -> "scala.LazyList",
@@ -245,7 +267,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
245267

246268
def replaceMapZip(ctx: RuleCtx): Patch = {
247269
ctx.tree.collect {
248-
case ap @ Term.Apply(Term.Select(_, mapZip(_)), List(_)) =>
270+
case ap @ Term.Apply(Term.Select(CollectionMap(), mapZip(_)), List(_)) =>
249271
ctx.addRight(ap, ".toMap")
250272

251273
}.asPatch

0 commit comments

Comments
 (0)