Skip to content

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 1 commit into from
Jul 20, 2018
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
45 changes: 45 additions & 0 deletions scalafix/input/src/main/scala/fix/Roughly212Src.scala
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]
Copy link
Contributor

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 to Map[Long, Int]. If we rewrite m.LongMap(d2).mapValues(f2) to m.LongMap(d2).mapValues(f2).to(LongMap), does that still cross-compile?

Copy link
Contributor Author

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.

Copy link
Contributor Author

@MasseGuillaume MasseGuillaume Jul 20, 2018

Choose a reason for hiding this comment

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

Copy link
Contributor

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!

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]

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/*
rule = "scala:fix.RoughlyStreamToLazyList"
rule = "scala:fix.Roughly"
Roughly = {
withLazyAppendedAll = true
withLazyList = true
}
*/
package fix

class RoughlyStreamToLazyListSrc() {
class Roughly213Src() {
val s = Stream(1, 2, 3)
s.append(List(4, 5, 6))
1 #:: 2 #:: 3 #:: Stream.Empty
Expand Down
8 changes: 0 additions & 8 deletions scalafix/input/src/main/scala/fix/RoughlyMapValuesSrc.scala

This file was deleted.

41 changes: 41 additions & 0 deletions scalafix/output212/src/main/scala/fix/Roughly212Src.scala
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]

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package fix

class RoughlyStreamToLazyListSrc() {
class Roughly213Src() {
val s = LazyList(1, 2, 3)
s.lazyAppendedAll(List(4, 5, 6))
1 #:: 2 #:: 3 #:: LazyList.Empty
Expand Down
95 changes: 95 additions & 0 deletions scalafix/rules/src/main/scala/fix/Roughly.scala
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)
}
24 changes: 0 additions & 24 deletions scalafix/rules/src/main/scala/fix/RoughlyMapValues.scala

This file was deleted.

35 changes: 0 additions & 35 deletions scalafix/rules/src/main/scala/fix/RoughlyStreamToLazyListSrc.scala

This file was deleted.

2 changes: 1 addition & 1 deletion scalafix/tests/src/test/scala/fix/ScalafixTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ class ScalafixTests

runAllTests()
// to run only one test:
// testsToRun.filter(_.filename.toNIO.getFileName.toString == "IterableSrc.scala" ).foreach(runOn)
// testsToRun.filter(_.filename.toNIO.getFileName.toString == "Playground.scala" ).foreach(runOn)
}