-
Notifications
You must be signed in to change notification settings - Fork 87
Rewrite for strict reverseMap and filterKeys (fix #85) #107
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
rule = "scala:fix.Roughly" | ||
Roughly = { | ||
strictMapValues = true | ||
strictFilterKeys = true | ||
} | ||
*/ | ||
package fix | ||
|
||
import scala.{collection => c} | ||
import scala.collection.{immutable => i, mutable => m} | ||
|
||
object Roughly212Src { | ||
|
||
def id[T](x: T): T = x | ||
def f[T](x: T): Boolean = true | ||
def f2[T](x: T): Int = 1 | ||
val props = new scala.sys.SystemProperties() | ||
val d = 1 -> 1 | ||
val d2 = 1L -> 1 | ||
val multi = new m.HashMap[Int, m.Set[Int]] with m.MultiMap[Int, Int] | ||
|
||
// i.SortedMap(d).mapValues(id): i.SortedMap[Int, Int] | ||
// m.SortedMap(d).mapValues(id): c.SortedMap[Int, Int] | ||
i.IntMap(d).mapValues(f2) : i.Map[Int, Int] | ||
i.LongMap(d2).mapValues(f2) : i.Map[Long, Int] | ||
i.Map(d).mapValues(id) : i.Map[Int, Int] | ||
m.LongMap(d2).mapValues(f2) : c.Map[Long, Int] | ||
m.Map(d).mapValues(id) : c.Map[Int, Int] | ||
m.OpenHashMap(d).mapValues(id) : c.Map[Int, Int] | ||
multi.mapValues(f2) : c.Map[Int, Int] | ||
props.mapValues(id) : c.Map[String, String] | ||
|
||
// i.SortedMap(d).filterKeys(f): i.SortedMap[Int, Int] | ||
// m.SortedMap(d).filterKeys(f): c.SortedMap[Int, Int] | ||
i.IntMap(d).filterKeys(f) : i.Map[Int, Int] | ||
i.LongMap(d2).filterKeys(f) : i.Map[Long, Int] | ||
i.Map(d).filterKeys(f) : i.Map[Int, Int] | ||
m.LongMap(d2).filterKeys(f) : c.Map[Long, Int] | ||
m.Map(d).filterKeys(f) : c.Map[Int, Int] | ||
m.Map(d).filterKeys(f) : c.Map[Int, Int] | ||
multi.filterKeys(f) : c.Map[Int, m.Set[Int]] | ||
props.filterKeys(f) : c.Map[String, String] | ||
|
||
} |
8 changes: 6 additions & 2 deletions
8
...cala/fix/RoughlyStreamToLazyListSrc.scala → ...ut/src/main/scala/fix/Roughly213Src.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
|
||
|
||
package fix | ||
|
||
import scala.{collection => c} | ||
import scala.collection.{immutable => i, mutable => m} | ||
|
||
object Roughly212Src { | ||
|
||
def id[T](x: T): T = x | ||
def f[T](x: T): Boolean = true | ||
def f2[T](x: T): Int = 1 | ||
val props = new scala.sys.SystemProperties() | ||
val d = 1 -> 1 | ||
val d2 = 1L -> 1 | ||
val multi = new m.HashMap[Int, m.Set[Int]] with m.MultiMap[Int, Int] | ||
|
||
// i.SortedMap(d).mapValues(id): i.SortedMap[Int, Int] | ||
// m.SortedMap(d).mapValues(id): c.SortedMap[Int, Int] | ||
i.IntMap(d).mapValues(f2).toMap : i.Map[Int, Int] | ||
i.LongMap(d2).mapValues(f2).toMap : i.Map[Long, Int] | ||
i.Map(d).mapValues(id).toMap : i.Map[Int, Int] | ||
m.LongMap(d2).mapValues(f2).toMap : c.Map[Long, Int] | ||
m.Map(d).mapValues(id).toMap : c.Map[Int, Int] | ||
m.OpenHashMap(d).mapValues(id).toMap : c.Map[Int, Int] | ||
multi.mapValues(f2).toMap : c.Map[Int, Int] | ||
props.mapValues(id).toMap : c.Map[String, String] | ||
|
||
// i.SortedMap(d).filterKeys(f): i.SortedMap[Int, Int] | ||
// m.SortedMap(d).filterKeys(f): c.SortedMap[Int, Int] | ||
i.IntMap(d).filterKeys(f).toMap : i.Map[Int, Int] | ||
i.LongMap(d2).filterKeys(f).toMap : i.Map[Long, Int] | ||
i.Map(d).filterKeys(f).toMap : i.Map[Int, Int] | ||
m.LongMap(d2).filterKeys(f).toMap : c.Map[Long, Int] | ||
m.Map(d).filterKeys(f).toMap : c.Map[Int, Int] | ||
m.Map(d).filterKeys(f).toMap : c.Map[Int, Int] | ||
multi.filterKeys(f).toMap : c.Map[Int, m.Set[Int]] | ||
props.filterKeys(f).toMap : c.Map[String, String] | ||
|
||
} |
8 changes: 0 additions & 8 deletions
8
scalafix/output212/src/main/scala/fix/RoughlyMapValuesSrc.scala
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package fix | ||
|
||
import scalafix._ | ||
import scalafix.util._ | ||
import scala.meta._ | ||
|
||
import metaconfig.{ConfDecoder, Conf, Configured} | ||
import metaconfig.annotation.Description | ||
import metaconfig.annotation.ExampleValue | ||
import metaconfig.generic | ||
import metaconfig.generic.Surface | ||
|
||
/* 2.12 Cross-Compatible | ||
* | ||
* This rules is *roughly* correct, they compile but might have a different runtime semantic | ||
* | ||
* Map.{mapValues, filterKeys}, Seq.reverseMap were lazy but with a strict interface | ||
* (ex returning Map where they should return MapView). | ||
* | ||
* LazyList has a lazy head, were Stream has a strict head | ||
* | ||
*/ | ||
final case class Roughly(index: SemanticdbIndex, config: RoughlyConfig) extends SemanticRule(index, "Roughly") { | ||
def this(index: SemanticdbIndex) = this(index, RoughlyConfig.default) | ||
|
||
val mapValues = | ||
SymbolMatcher.exact( | ||
Symbol("_root_.scala.collection.immutable.MapLike#mapValues(Lscala/Function1;)Lscala/collection/immutable/Map;."), | ||
Symbol("_root_.scala.collection.MapLike#filterKeys(Lscala/Function1;)Lscala/collection/Map;.") | ||
) | ||
|
||
val filterKeys = | ||
SymbolMatcher.exact( | ||
Symbol("_root_.scala.collection.immutable.MapLike#filterKeys(Lscala/Function1;)Lscala/collection/immutable/Map;."), | ||
Symbol("_root_.scala.collection.MapLike#mapValues(Lscala/Function1;)Lscala/collection/Map;.") | ||
) | ||
|
||
// Not supported: SortedMap | ||
// Symbol("_root_.scala.collection.immutable.SortedMap#mapValues(Lscala/Function1;)Lscala/collection/immutable/SortedMap;."), | ||
// Symbol("_root_.scala.collection.SortedMapLike#mapValues(Lscala/Function1;)Lscala/collection/SortedMap;.") | ||
// Symbol("_root_.scala.collection.immutable.SortedMap#filterKeys(Lscala/Function1;)Lscala/collection/immutable/SortedMap;.") | ||
// Symbol("_root_.scala.collection.SortedMapLike#filterKeys(Lscala/Function1;)Lscala/collection/SortedMap;.") | ||
|
||
val streamAppend = SymbolMatcher.normalized( | ||
Symbol("_root_.scala.collection.immutable.Stream.append.") | ||
) | ||
|
||
def replaceSymbols(ctx: RuleCtx): Patch = { | ||
if (config.withLazyList) { | ||
ctx.replaceSymbols( | ||
"scala.Stream" -> "scala.LazyList", | ||
"scala.collection.immutable.Stream" -> "scala.collection.immutable.LazyList" | ||
) | ||
} else Patch.empty | ||
} | ||
|
||
override def description: String = "" | ||
|
||
override def init(config: Conf): Configured[Rule] = | ||
config | ||
.getOrElse("roughly", "Roughly")(RoughlyConfig.default) | ||
.map(Roughly(index, _)) | ||
|
||
override def fix(ctx: RuleCtx): Patch = { | ||
import config._ | ||
|
||
val collectFixes = | ||
ctx.tree.collect { | ||
case ap @ Term.Apply(Term.Select(_, mapValues(_)), List(_)) if strictMapValues => | ||
ctx.addRight(ap, ".toMap") | ||
|
||
case ap @ Term.Apply(Term.Select(_, filterKeys(_)), List(_)) if strictFilterKeys => | ||
ctx.addRight(ap, ".toMap") | ||
|
||
case streamAppend(t: Name) if withLazyAppendedAll => | ||
ctx.replaceTree(t, "lazyAppendedAll") | ||
|
||
}.asPatch | ||
|
||
collectFixes + replaceSymbols(ctx) | ||
} | ||
} | ||
|
||
case class RoughlyConfig( | ||
strictMapValues: Boolean = false, | ||
strictFilterKeys: Boolean = false, | ||
withLazyAppendedAll: Boolean = false, | ||
withLazyList: Boolean = false | ||
) | ||
|
||
object RoughlyConfig { | ||
val default: RoughlyConfig = RoughlyConfig() | ||
implicit val surface: Surface[RoughlyConfig] = generic.deriveSurface[RoughlyConfig] | ||
implicit val decoder: ConfDecoder[RoughlyConfig] = generic.deriveDecoder[RoughlyConfig](default) | ||
} |
This file was deleted.
Oops, something went wrong.
35 changes: 0 additions & 35 deletions
35
scalafix/rules/src/main/scala/fix/RoughlyStreamToLazyListSrc.scala
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think users would typically expect a
m.LongMap[Int]
here, instead of upcasting toMap[Long, Int]
. If we rewritem.LongMap(d2).mapValues(f2)
tom.LongMap(d2).mapValues(f2).to(LongMap)
, does that still cross-compile?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.
this is the test input, I'm ascribing to make sure we preserve the same output type.
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.
It's typed this way in 2.12
def filterKeys(p: (Long) ⇒ Boolean): Map[Long, T]
https://static.javadoc.io/org.scala-lang/scala-library/2.12.6/scala/collection/immutable/LongMap.html#filterKeys(p:K=%3EBoolean):scala.collection.immutable.Map[K,V]
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.
Ah, indeed, the return type is
Map
. I thought that operation was overridden to refine the return type but that’s not the case. Alright, then!