Skip to content

Make the breakOut rewrite rule cross compatible (fix #80) (fix #93) #102

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 23, 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
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ lazy val compat = crossProject(JSPlatform, JVMPlatform)
.jvmSettings(scalaModuleSettingsJVM)
.settings(
name := "scala-collection-compat",
version := "0.1-SNAPSHOT",
version := "0.2.0-SNAPSHOT",
scalacOptions ++= Seq("-feature", "-language:higherKinds", "-language:implicitConversions"),
unmanagedSourceDirectories in Compile += {
val sharedSourceDir = baseDirectory.value.getParentFile / "src/main"
Expand Down Expand Up @@ -69,6 +69,7 @@ lazy val scalafixRules = project
.in(file("scalafix/rules"))
.settings(
organization := (organization in compatJVM).value,
version := (version in compatJVM).value,
name := "scala-collection-migrations",
scalaVersion := scalafixScala212,
libraryDependencies += "ch.epfl.scala" %% "scalafix-core" % scalafixVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,46 @@ package object compat {
def fromSpecific(source: TraversableOnce[Int]): C = fact.apply(source.toSeq: _*)
}

private def build[T, CC](builder: m.Builder[T, CC], source: TraversableOnce[T]): CC = {
builder ++= source
builder.result()
}

implicit class ImmutableSortedMapExtensions(private val fact: i.SortedMap.type) extends AnyVal {
def from[K: Ordering, V](source: TraversableOnce[(K, V)]): i.SortedMap[K, V] =
build(i.SortedMap.newBuilder[K, V], source)
}

implicit class ImmutableTreeMapExtensions(private val fact: i.TreeMap.type) extends AnyVal {
def from[K: Ordering, V](source: TraversableOnce[(K, V)]): i.TreeMap[K, V] =
build(i.TreeMap.newBuilder[K, V], source)
}

implicit class ImmutableIntMapExtensions(private val fact: i.IntMap.type) extends AnyVal {
def from[V](source: TraversableOnce[(Int, V)]): i.IntMap[V] =
build(i.IntMap.canBuildFrom[Int, V](), source)
}

implicit class ImmutableLongMapExtensions(private val fact: i.LongMap.type) extends AnyVal {
def from[V](source: TraversableOnce[(Long, V)]): i.LongMap[V] =
build(i.LongMap.canBuildFrom[Long, V](), source)
}

implicit class MutableSortedMapExtensions(private val fact: m.SortedMap.type) extends AnyVal {
def from[K: Ordering, V](source: TraversableOnce[(K, V)]): m.SortedMap[K, V] =
build(m.SortedMap.newBuilder[K, V], source)
}

implicit class MutableTreeMapExtensions(private val fact: m.TreeMap.type) extends AnyVal {
def from[K: Ordering, V](source: TraversableOnce[(K, V)]): m.TreeMap[K, V] =
build(m.TreeMap.newBuilder[K, V], source)
}

implicit class MutableLongMapExtensions(private val fact: m.LongMap.type) extends AnyVal {
def from[V](source: TraversableOnce[(Long, V)]): m.LongMap[V] =
build(m.LongMap.canBuildFrom[Long, V](), source)
}

implicit class StreamExtensionMethods[A](private val stream: Stream[A]) extends AnyVal {
def lazyAppendedAll(as: => TraversableOnce[A]): Stream[A] = stream.append(as)
}
Expand All @@ -58,6 +98,7 @@ package object compat {
def sameElements[B >: A](that: IterableOnce[B]): Boolean = {
self.sameElements(that.iterator)
}
def concat[B >: A](that: IterableOnce[B]): IterableOnce[B] = self ++ that
}

implicit class TraversableOnceExtensionMethods[A](private val self: TraversableOnce[A]) extends AnyVal {
Expand Down
119 changes: 101 additions & 18 deletions scalafix/input/src/main/scala/fix/BreakoutSrc.scala
Original file line number Diff line number Diff line change
@@ -1,25 +1,108 @@
/*
rule = "scala:fix.NewCollections"
rule = "scala:fix.CrossCompat"
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

*/
package fix

import scala.collection.breakOut
import scala.collection.{immutable, mutable}
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

object BreakoutSrc {
val xs = List(1, 2, 3)

xs.collect{ case x => x }(breakOut): Set[Int]
xs.flatMap(x => List(x))(breakOut): collection.SortedSet[Int]
xs.map(_ + 1)(breakOut): Set[Int]
xs.reverseMap(_ + 1)(breakOut): Set[Int]
xs.scanLeft(0)((a, b) => a + b)(breakOut): Set[Int]
xs.union(xs)(breakOut): Set[Int]
xs.updated(0, 1)(breakOut): Set[Int]
xs.zip(xs)(breakOut): Map[Int, Int]
xs.zipAll(xs, 0, 0)(breakOut): Array[(Int, Int)]

(xs ++ xs)(breakOut): Set[Int]
(1 +: xs)(breakOut): Set[Int]
(xs :+ 1)(breakOut): Set[Int]
(xs ++: xs)(breakOut): Set[Int]
class BreakoutSrc(ts: Traversable[Int], vec: Vector[Int], list: List[Int], seq: Seq[Int]) {

// `IndexedSeqOptimized.zip`
vec.zip(vec)(breakOut): Map[Int, Int]

// `IterableLike.zip`
seq.zip(seq)(breakOut): Map[Int, Int]

// `IterableLike.zipAll`
seq.zipAll(seq, 0, 0)(breakOut): Array[(Int, Int)]

// `List ++`
(list ++ list)(breakOut): Set[Int]

// `List +:`
(1 +: list)(breakOut): Set[Int]

// `List.collect`
list.collect{ case x => x}(breakOut): Set[Int]

// `List.flatMap`
list.flatMap(x => List(x))(breakOut): Set[Int]

// `List.map`
list.map(x => x)(breakOut): Set[Int]

// `SeqLike.reverseMap`
seq.reverseMap(_ + 1)(breakOut): Set[Int]

// `SeqLike +:`
(1 +: seq)(breakOut): List[Int]

// `SeqLike :+`
(seq :+ 1)(breakOut): List[Int]

// `SeqLike.updated`
(seq.updated(0, 0))(breakOut): List[Int]

// `SeqLike.union`
seq.union(seq)(breakOut): List[Int]


//`SetLike.map`
Set(1).map(x => x)(breakOut): List[Int]


// `TraversableLike ++`
(ts ++ ts )(breakOut): Set[Int]

// `TraversableLike ++:`
(ts ++: ts)(breakOut): Set[Int]

// `TraversableLike.collect`
ts.collect{ case x => x }(breakOut): Set[Int]

// `TraversableLike.flatMap`
ts.flatMap(x => List(x))(breakOut): collection.SortedSet[Int]

// `TraversableLike.map`
ts.map(_ + 1)(breakOut): Set[Int]

// `TraversableLike.scanLeft`
ts.scanLeft(0)((a, b) => a + b)(breakOut): Set[Int]


// `Vector ++`
(vec ++ List(1))(breakOut): List[Int]

// `Vector +:`
(1 +: vec)(breakOut): List[Int]

// `Vector :+`
(vec :+ 1)(breakOut): List[Int]

// `Vector.updated`
(vec.updated(0, 0))(breakOut): List[Int]

// Future
Future.sequence(List(Future(1)))(breakOut, global): Future[Seq[Int]]
Future.traverse(List(1))(x => Future(x))(breakOut, global): Future[Seq[Int]]

// Iterable
List(1).map(x => x)(breakOut): Iterator[Int]

// Specific collections
List(1 -> "1").map(x => x)(breakOut): immutable.SortedMap[Int, String]
List(1 -> "1").map(x => x)(breakOut): immutable.HashMap[Int, String]
List(1 -> "1").map(x => x)(breakOut): immutable.ListMap[Int, String]
List(1 -> "1").map(x => x)(breakOut): immutable.TreeMap[Int, String]
List(1 -> "1").map(x => x)(breakOut): mutable.SortedMap[Int, String]
List(1 -> "1").map(x => x)(breakOut): mutable.HashMap[Int, String]
List(1 -> "1").map(x => x)(breakOut): mutable.ListMap[Int, String]
List(1 -> "1").map(x => x)(breakOut): mutable.TreeMap[Int, String]
List(1 -> "1").map(x => x)(breakOut): mutable.Map[Int, String]
List(1 -> "1").map(x => x)(breakOut): immutable.IntMap[String]
List(1L -> "1").map(x => x)(breakOut): immutable.LongMap[String]
List(1L -> "1").map(x => x)(breakOut): mutable.LongMap[String]
}
108 changes: 108 additions & 0 deletions scalafix/output212/src/main/scala/fix/BreakoutSrc.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@



package fix

import scala.collection.{immutable, mutable}
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.collection.compat._

class BreakoutSrc(ts: Iterable[Int], vec: Vector[Int], list: List[Int], seq: Seq[Int]) {

// `IndexedSeqOptimized.zip`
vec.iterator.zip(vec.iterator).toMap: Map[Int, Int]

// `IterableLike.zip`
seq.iterator.zip(seq.iterator).toMap: Map[Int, Int]

// `IterableLike.zipAll`
seq.iterator.zipAll(seq.iterator, 0, 0).to(scala.Array): Array[(Int, Int)]

// `List ++`
(list.iterator ++ list).to(scala.collection.immutable.Set): Set[Int]

// `List +:`
(1 +: list.view).to(scala.collection.immutable.Set): Set[Int]

// `List.collect`
list.iterator.collect{ case x => x}.to(scala.collection.immutable.Set): Set[Int]

// `List.flatMap`
list.iterator.flatMap(x => List(x)).to(scala.collection.immutable.Set): Set[Int]

// `List.map`
list.iterator.map(x => x).to(scala.collection.immutable.Set): Set[Int]

// `SeqLike.reverseMap`
seq.reverseIterator.map(_ + 1).to(scala.collection.immutable.Set): Set[Int]

// `SeqLike +:`
(1 +: seq.view).to(scala.collection.immutable.List): List[Int]

// `SeqLike :+`
(seq.view :+ 1).to(scala.collection.immutable.List): List[Int]

// `SeqLike.updated`
(seq.view.updated(0, 0)).to(scala.collection.immutable.List): List[Int]

// `SeqLike.union`
seq.iterator.concat(seq).to(scala.collection.immutable.List): List[Int]


//`SetLike.map`
Set(1).iterator.map(x => x).to(scala.collection.immutable.List): List[Int]


// `TraversableLike ++`
(ts.iterator ++ ts ).to(scala.collection.immutable.Set): Set[Int]

// `TraversableLike ++:`
(ts ++: ts.view).to(scala.collection.immutable.Set): Set[Int]

// `TraversableLike.collect`
ts.iterator.collect{ case x => x }.to(scala.collection.immutable.Set): Set[Int]

// `TraversableLike.flatMap`
ts.iterator.flatMap(x => List(x)).to(scala.collection.SortedSet): collection.SortedSet[Int]

// `TraversableLike.map`
ts.iterator.map(_ + 1).to(scala.collection.immutable.Set): Set[Int]

// `TraversableLike.scanLeft`
ts.iterator.scanLeft(0)((a, b) => a + b).to(scala.collection.immutable.Set): Set[Int]


// `Vector ++`
(vec.iterator ++ List(1)).to(scala.collection.immutable.List): List[Int]

// `Vector +:`
(1 +: vec.view).to(scala.collection.immutable.List): List[Int]

// `Vector :+`
(vec.view :+ 1).to(scala.collection.immutable.List): List[Int]

// `Vector.updated`
(vec.view.updated(0, 0)).to(scala.collection.immutable.List): List[Int]

// Future
Future.sequence(List(Future(1)))(scala.collection.immutable.List, global): Future[Seq[Int]]
Future.traverse(List(1))(x => Future(x))(scala.collection.immutable.List, global): Future[Seq[Int]]

// Iterable
List(1).iterator.map(x => x): Iterator[Int]

// Specific collections
scala.collection.immutable.SortedMap.from(List(1 -> "1").iterator.map(x => x)): immutable.SortedMap[Int, String]
scala.collection.immutable.HashMap.from(List(1 -> "1").iterator.map(x => x)): immutable.HashMap[Int, String]
scala.collection.immutable.ListMap.from(List(1 -> "1").iterator.map(x => x)): immutable.ListMap[Int, String]
scala.collection.immutable.TreeMap.from(List(1 -> "1").iterator.map(x => x)): immutable.TreeMap[Int, String]
scala.collection.mutable.SortedMap.from(List(1 -> "1").iterator.map(x => x)): mutable.SortedMap[Int, String]
scala.collection.mutable.HashMap.from(List(1 -> "1").iterator.map(x => x)): mutable.HashMap[Int, String]
scala.collection.mutable.ListMap.from(List(1 -> "1").iterator.map(x => x)): mutable.ListMap[Int, String]
scala.collection.mutable.TreeMap.from(List(1 -> "1").iterator.map(x => x)): mutable.TreeMap[Int, String]
scala.collection.mutable.Map.from(List(1 -> "1").iterator.map(x => x)): mutable.Map[Int, String]
scala.collection.immutable.IntMap.from(List(1 -> "1").iterator.map(x => x)): immutable.IntMap[String]
scala.collection.immutable.LongMap.from(List(1L -> "1").iterator.map(x => x)): immutable.LongMap[String]
scala.collection.mutable.LongMap.from(List(1L -> "1").iterator.map(x => x)): mutable.LongMap[String]
}
24 changes: 0 additions & 24 deletions scalafix/output213/src/main/scala/fix/BreakoutSrc.scala

This file was deleted.

Loading