Skip to content

Commit 431d2fa

Browse files
authored
Merge pull request #34 from MasseGuillaume/feature/Map.retain
Rewrite `retain` to `filterInPlace`
2 parents 027a198 + 58c6f7c commit 431d2fa

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

scalafix/build.sbt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ lazy val tests = project
3737
sourceDirectory.in(output, Compile).value,
3838
"inputClassdirectory" ->
3939
classDirectory.in(input, Compile).value
40-
)
40+
),
41+
test in Test := (test in Test).dependsOn(compile in (output, Compile)).value
4142
)
4243
.dependsOn(input, rules)
4344
.enablePlugins(BuildInfoPlugin)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
rule = "scala:fix.Scalacollectioncompat_NewCollections"
3+
*/
4+
package fix
5+
6+
import scala.collection.mutable.Map
7+
8+
class MethodRenames(xs: Map[Int, Int]) {
9+
xs.retain((_, _) => true)
10+
xs.retain{case (x, y) => true}
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package fix
2+
3+
import scala.collection.mutable.Map
4+
5+
class MethodRenames(xs: Map[Int, Int]) {
6+
xs.filterInPlace{case (_, _) => true}
7+
xs.filterInPlace{case (x, y) => true}
8+
}

scalafix/project/plugins.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.5.7")
1+
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.5.10")
22
addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.0.0")
33
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.6.1")

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,28 @@ case class Scalacollectioncompat_NewCollections(index: SemanticdbIndex)
3030
Symbol("_root_.scala.runtime.Tuple3Zipped.Ops.zipped.")
3131
)
3232

33+
val retain =
34+
SymbolMatcher.normalized(
35+
Symbol("_root_.scala.collection.mutable.MapLike.retain.")
36+
)
37+
38+
def replaceMutableMap(ctx: RuleCtx) =
39+
ctx.tree.collect {
40+
case Term.Apply(Term.Select(_, retain(n: Name)), List(_: Term.PartialFunction)) =>
41+
ctx.replaceTree(n, "filterInPlace")
42+
43+
case Term.Apply(Term.Select(_, retain(n: Name)), List(_: Term.Function)) =>
44+
(for {
45+
name <- n.tokens.lastOption
46+
open <- ctx.tokenList.find(name)(t => t.is[Token.LeftParen])
47+
close <- ctx.matchingParens.close(open.asInstanceOf[Token.LeftParen])
48+
} yield
49+
ctx.replaceToken(open, "{case ") +
50+
ctx.replaceToken(close, "}") +
51+
ctx.replaceTree(n, "filterInPlace")
52+
).asPatch
53+
}.asPatch
54+
3355
def replaceToList(ctx: RuleCtx) =
3456
ctx.tree.collect {
3557
case iterator(t: Name) =>
@@ -110,6 +132,7 @@ case class Scalacollectioncompat_NewCollections(index: SemanticdbIndex)
110132
replaceSymbols(ctx) +
111133
replaceTupleZipped(ctx) +
112134
replaceCopyToBuffer(ctx) +
113-
replaceStreamAppend(ctx)
135+
replaceStreamAppend(ctx) +
136+
replaceMutableMap(ctx)
114137
}
115138
}

0 commit comments

Comments
 (0)