-
Notifications
You must be signed in to change notification settings - Fork 87
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
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#") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
|
@@ -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;.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like it’s going to match any call to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) => | ||
|
@@ -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 { | ||
|
@@ -280,7 +314,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex) | |
replaceMutMapUpdated(ctx) + | ||
replaceIterableSameElements(ctx) + | ||
replaceArrayBuilderMake(ctx) + | ||
replaceMapZip(ctx) + | ||
replaceMapMapValues(ctx) | ||
|
||
} | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.