Skip to content

Rewrite Map.zip -> Map.zip.toMap #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 2 additions & 0 deletions scalafix/input/src/main/scala/fix/SetMapSrc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ class SetMapSrc(set: Set[Int], map: Map[Int, Int]) {
(set + (2, 3)).map(x => x)
set + (2, 3) - 4
map.mapValues(_ + 1)
map.zip(List())
List().zip(List())
}
2 changes: 2 additions & 0 deletions scalafix/output/src/main/scala/fix/SetMapSrc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ class SetMapSrc(set: Set[Int], map: Map[Int, Int]) {
(set + 2 + 3).map(x => x)
set + 2 + 3 - 4
map.mapValues(_ + 1).toMap
map.zip(List()).toMap
List().zip(List())
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@ import scala.meta._
case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
extends SemanticRule(index, "Scalacollectioncompat_newcollections") {

// WARNING: TOTAL HACK
// this is only to unblock us until Term.tpe is available: https://github.com/scalameta/scalameta/issues/1212
// if we have a simple identifier, we can look at his definition at query it's type
// this should be improved in future version of scalameta
object TypeMatcher {
def apply(symbols: Symbol*)(implicit index: SemanticdbIndex): TypeMatcher =
new TypeMatcher(symbols: _*)(index)
}

final class TypeMatcher(symbols: Symbol*)(implicit index: SemanticdbIndex) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mind adding some documentation?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes please, in particular highlight in caps lock that this is not The Real Deal. This is only to unblock tests, I would in fact prefer if this was not merged into master because users should not run this.

def unapply(tree: Tree): Boolean = {
index.denotation(tree)
.exists(_.names.headOption.exists(n => symbols.exists(_ == n.symbol)))
}
}

val CollectionMap: TypeMatcher = TypeMatcher(
Symbol("_root_.scala.collection.immutable.Map#"),
Symbol("_root_.scala.collection.mutable.Map#"),
Symbol("_root_.scala.Predef.Map#")
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add HashMap and TreeMap as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would rather not dig that hole too deep. The idea is to document what is the expected type, not to have a working rule.

)

def replaceSymbols(ctx: RuleCtx): Patch = {
ctx.replaceSymbols(
"scala.collection.LinearSeq" -> "scala.collection.immutable.List",
Expand Down Expand Up @@ -77,11 +99,17 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
Symbol("_root_.scala.collection.mutable.SetLike.retain.")
)


val arrayBuilderMake =
SymbolMatcher.normalized(
Symbol("_root_.scala.collection.mutable.ArrayBuilder.make(Lscala/reflect/ClassTag;)Lscala/collection/mutable/ArrayBuilder;.")
)

val mapZip =
SymbolMatcher.exact(
Symbol("_root_.scala.collection.IterableLike#zip(Lscala/collection/GenIterable;Lscala/collection/generic/CanBuildFrom;)Ljava/lang/Object;.")
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like it’s going to match any call to zip, even if the receiver is not of type Map[_, _].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

then we are blocked by scalameta/scalameta#1212

)

def replaceMutableSet(ctx: RuleCtx) =
ctx.tree.collect {
case retainSet(n: Name) =>
Expand Down Expand Up @@ -254,10 +282,16 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
case ap @ Term.Apply(at @ Term.ApplyType(Term.Select(lhs, arrayBuilderMake(_)), args), Nil) =>
val extraParens =
ap.tokens.slice(at.tokens.size, ap.tokens.size)

ctx.removeTokens(extraParens)
}.asPatch
}

def replaceMapZip(ctx: RuleCtx): Patch = {
ctx.tree.collect {
case ap @ Term.Apply(Term.Select(CollectionMap(), mapZip(_)), List(_)) =>
ctx.addRight(ap, ".toMap")
}.asPatch
}

def replaceMapMapValues(ctx: RuleCtx): Patch = {
ctx.tree.collect {
Expand All @@ -280,7 +314,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
replaceMutMapUpdated(ctx) +
replaceIterableSameElements(ctx) +
replaceArrayBuilderMake(ctx) +
replaceMapZip(ctx) +
replaceMapMapValues(ctx)

}
}