Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion scalafix/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ lazy val tests = project
sourceDirectory.in(output, Compile).value,
"inputClassdirectory" ->
classDirectory.in(input, Compile).value
)
),
test in Test := (test in Test).dependsOn(compile in (output, Compile)).value
)
.dependsOn(input, rules)
.enablePlugins(BuildInfoPlugin)
11 changes: 11 additions & 0 deletions scalafix/input/src/main/scala/fix/RetainSrc.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
rule = "scala:fix.Scalacollectioncompat_NewCollections"
*/
package fix

import scala.collection.mutable.Map

class MethodRenames(xs: Map[Int, Int]) {
xs.retain((_, _) => true)
xs.retain{case (x, y) => true}
}
8 changes: 8 additions & 0 deletions scalafix/output/src/main/scala/fix/RetainSrc.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fix

import scala.collection.mutable.Map

class MethodRenames(xs: Map[Int, Int]) {
xs.filterInPlace{case (_, _) => true}
xs.filterInPlace{case (x, y) => true}
}
2 changes: 1 addition & 1 deletion scalafix/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.5.7")
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.5.10")
addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.0.0")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.6.1")
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ case class Scalacollectioncompat_NewCollections(index: SemanticdbIndex)
Symbol("_root_.scala.runtime.Tuple3Zipped.Ops.zipped.")
)

val retain =
SymbolMatcher.normalized(
Symbol("_root_.scala.collection.mutable.MapLike.retain.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I’ve just noticed that this operation is also on mutable.Set. We should support that use case too…

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#36

)

def replaceMutableMap(ctx: RuleCtx) =
ctx.tree.collect {
case Term.Apply(Term.Select(_, retain(n: Name)), List(_: Term.PartialFunction)) =>
ctx.replaceTree(n, "filterInPlace")

case Term.Apply(Term.Select(_, retain(n: Name)), List(_: Term.Function)) =>
(for {
name <- n.tokens.lastOption
open <- ctx.tokenList.find(name)(t => t.is[Token.LeftParen])
close <- ctx.matchingParens.close(open.asInstanceOf[Token.LeftParen])
} yield
ctx.replaceToken(open, "{case ") +
ctx.replaceToken(close, "}") +
ctx.replaceTree(n, "filterInPlace")
).asPatch
}.asPatch

def replaceToList(ctx: RuleCtx) =
ctx.tree.collect {
case iterator(t: Name) =>
Expand Down Expand Up @@ -110,6 +132,7 @@ case class Scalacollectioncompat_NewCollections(index: SemanticdbIndex)
replaceSymbols(ctx) +
replaceTupleZipped(ctx) +
replaceCopyToBuffer(ctx) +
replaceStreamAppend(ctx)
replaceStreamAppend(ctx) +
replaceMutableMap(ctx)
}
}