Skip to content

Commit 6a1d39c

Browse files
authored
Merge pull request #60 from MasseGuillaume/map-mapValues
Rewrite Map.mapValues -> Map.mapValues.toMap
2 parents 16d2d44 + dcc68d7 commit 6a1d39c

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ class SetMapSrc(set: Set[Int], map: Map[Int, Int]) {
88
map + (2 -> 3, 3 -> 4)
99
(set + (2, 3)).map(x => x)
1010
set + (2, 3) - 4
11+
map.mapValues(_ + 1)
1112
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ class SetMapSrc(set: Set[Int], map: Map[Int, Int]) {
88
map + (2 -> 3) + (3 -> 4)
99
(set + 2 + 3).map(x => x)
1010
set + 2 + 3 - 4
11+
map.mapValues(_ + 1).toMap
1112
}

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

+15-4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
6767
Symbol("_root_.scala.collection.mutable.MapLike.retain.")
6868
)
6969

70+
val mapMapValues =
71+
SymbolMatcher.exact(
72+
Symbol("_root_.scala.collection.immutable.MapLike#mapValues(Lscala/Function1;)Lscala/collection/immutable/Map;.")
73+
)
74+
7075
val retainSet =
7176
SymbolMatcher.normalized(
7277
Symbol("_root_.scala.collection.mutable.SetLike.retain.")
@@ -253,11 +258,15 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
253258
ctx.removeTokens(extraParens)
254259
}.asPatch
255260
}
256-
257261

262+
def replaceMapMapValues(ctx: RuleCtx): Patch = {
263+
ctx.tree.collect {
264+
case ap @ Term.Apply(Term.Select(_, mapMapValues(_)), List(_)) =>
265+
ctx.addRight(ap, ".toMap")
266+
}.asPatch
267+
}
268+
258269
override def fix(ctx: RuleCtx): Patch = {
259-
// println(ctx.index.database)
260-
261270
replaceToList(ctx) +
262271
replaceSymbols(ctx) +
263272
replaceTupleZipped(ctx) +
@@ -270,6 +279,8 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
270279
replaceMutSetMapPlus(ctx) +
271280
replaceMutMapUpdated(ctx) +
272281
replaceIterableSameElements(ctx) +
273-
replaceArrayBuilderMake(ctx)
282+
replaceArrayBuilderMake(ctx) +
283+
replaceMapMapValues(ctx)
284+
274285
}
275286
}

0 commit comments

Comments
 (0)