Skip to content

Commit df2f4a2

Browse files
Make the breakOut rewrite rule cross compatible (fix scala#80) (fix scala#93)
1 parent 9f74678 commit df2f4a2

File tree

8 files changed

+615
-193
lines changed

8 files changed

+615
-193
lines changed

build.sbt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ lazy val compat = crossProject(JSPlatform, JVMPlatform)
2222
.jvmSettings(scalaModuleSettingsJVM)
2323
.settings(
2424
name := "scala-collection-compat",
25-
version := "0.1-SNAPSHOT",
25+
version := "0.2.0-SNAPSHOT",
2626
scalacOptions ++= Seq("-feature", "-language:higherKinds", "-language:implicitConversions"),
2727
unmanagedSourceDirectories in Compile += {
2828
val sharedSourceDir = baseDirectory.value.getParentFile / "src/main"
@@ -69,6 +69,7 @@ lazy val scalafixRules = project
6969
.in(file("scalafix/rules"))
7070
.settings(
7171
organization := (organization in compatJVM).value,
72+
version := (version in compatJVM).value,
7273
name := "scala-collection-migrations",
7374
scalaVersion := scalafixScala212,
7475
libraryDependencies += "ch.epfl.scala" %% "scalafix-core" % scalafixVersion

compat/src/main/scala-2.11_2.12/scala/collection/compat/package.scala

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,46 @@ package object compat {
4444
def fromSpecific(source: TraversableOnce[Int]): C = fact.apply(source.toSeq: _*)
4545
}
4646

47+
private def build[T, CC](builder: m.Builder[T, CC], source: TraversableOnce[T]): CC = {
48+
builder ++= source
49+
builder.result()
50+
}
51+
52+
implicit class ImmutableSortedMapExtensions(private val fact: i.SortedMap.type) extends AnyVal {
53+
def from[K: Ordering, V](source: TraversableOnce[(K, V)]): i.SortedMap[K, V] =
54+
build(i.SortedMap.newBuilder[K, V], source)
55+
}
56+
57+
implicit class ImmutableTreeMapExtensions(private val fact: i.TreeMap.type) extends AnyVal {
58+
def from[K: Ordering, V](source: TraversableOnce[(K, V)]): i.TreeMap[K, V] =
59+
build(i.TreeMap.newBuilder[K, V], source)
60+
}
61+
62+
implicit class ImmutableIntMapExtensions(private val fact: i.IntMap.type) extends AnyVal {
63+
def from[V](source: TraversableOnce[(Int, V)]): i.IntMap[V] =
64+
build(i.IntMap.canBuildFrom[Int, V](), source)
65+
}
66+
67+
implicit class ImmutableLongMapExtensions(private val fact: i.LongMap.type) extends AnyVal {
68+
def from[V](source: TraversableOnce[(Long, V)]): i.LongMap[V] =
69+
build(i.LongMap.canBuildFrom[Long, V](), source)
70+
}
71+
72+
implicit class MutableSortedMapExtensions(private val fact: m.SortedMap.type) extends AnyVal {
73+
def from[K: Ordering, V](source: TraversableOnce[(K, V)]): m.SortedMap[K, V] =
74+
build(m.SortedMap.newBuilder[K, V], source)
75+
}
76+
77+
implicit class MutableTreeMapExtensions(private val fact: m.TreeMap.type) extends AnyVal {
78+
def from[K: Ordering, V](source: TraversableOnce[(K, V)]): m.TreeMap[K, V] =
79+
build(m.TreeMap.newBuilder[K, V], source)
80+
}
81+
82+
implicit class MutableLongMapExtensions(private val fact: m.LongMap.type) extends AnyVal {
83+
def from[V](source: TraversableOnce[(Long, V)]): m.LongMap[V] =
84+
build(m.LongMap.canBuildFrom[Long, V](), source)
85+
}
86+
4787
implicit class StreamExtensionMethods[A](private val stream: Stream[A]) extends AnyVal {
4888
def lazyAppendedAll(as: => TraversableOnce[A]): Stream[A] = stream.append(as)
4989
}
@@ -58,6 +98,7 @@ package object compat {
5898
def sameElements[B >: A](that: IterableOnce[B]): Boolean = {
5999
self.sameElements(that.iterator)
60100
}
101+
def concat[B >: A](that: IterableOnce[B]): IterableOnce[B] = self ++ that
61102
}
62103

63104
implicit class TraversableOnceExtensionMethods[A](private val self: TraversableOnce[A]) extends AnyVal {
Lines changed: 101 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,108 @@
11
/*
2-
rule = "scala:fix.NewCollections"
2+
rule = "scala:fix.CrossCompat"
33
*/
44
package fix
55

66
import scala.collection.breakOut
7+
import scala.collection.{immutable, mutable}
8+
import scala.concurrent.Future
9+
import scala.concurrent.ExecutionContext.Implicits.global
710

8-
object BreakoutSrc {
9-
val xs = List(1, 2, 3)
10-
11-
xs.collect{ case x => x }(breakOut): Set[Int]
12-
xs.flatMap(x => List(x))(breakOut): collection.SortedSet[Int]
13-
xs.map(_ + 1)(breakOut): Set[Int]
14-
xs.reverseMap(_ + 1)(breakOut): Set[Int]
15-
xs.scanLeft(0)((a, b) => a + b)(breakOut): Set[Int]
16-
xs.union(xs)(breakOut): Set[Int]
17-
xs.updated(0, 1)(breakOut): Set[Int]
18-
xs.zip(xs)(breakOut): Map[Int, Int]
19-
xs.zipAll(xs, 0, 0)(breakOut): Array[(Int, Int)]
20-
21-
(xs ++ xs)(breakOut): Set[Int]
22-
(1 +: xs)(breakOut): Set[Int]
23-
(xs :+ 1)(breakOut): Set[Int]
24-
(xs ++: xs)(breakOut): Set[Int]
11+
class BreakoutSrc(ts: Traversable[Int], vec: Vector[Int], list: List[Int], seq: Seq[Int]) {
12+
13+
// `IndexedSeqOptimized.zip`
14+
vec.zip(vec)(breakOut): Map[Int, Int]
15+
16+
// `IterableLike.zip`
17+
seq.zip(seq)(breakOut): Map[Int, Int]
18+
19+
// `IterableLike.zipAll`
20+
seq.zipAll(seq, 0, 0)(breakOut): Array[(Int, Int)]
21+
22+
// `List ++`
23+
(list ++ list)(breakOut): Set[Int]
24+
25+
// `List +:`
26+
(1 +: list)(breakOut): Set[Int]
27+
28+
// `List.collect`
29+
list.collect{ case x => x}(breakOut): Set[Int]
30+
31+
// `List.flatMap`
32+
list.flatMap(x => List(x))(breakOut): Set[Int]
33+
34+
// `List.map`
35+
list.map(x => x)(breakOut): Set[Int]
36+
37+
// `SeqLike.reverseMap`
38+
seq.reverseMap(_ + 1)(breakOut): Set[Int]
39+
40+
// `SeqLike +:`
41+
(1 +: seq)(breakOut): List[Int]
42+
43+
// `SeqLike :+`
44+
(seq :+ 1)(breakOut): List[Int]
45+
46+
// `SeqLike.updated`
47+
(seq.updated(0, 0))(breakOut): List[Int]
48+
49+
// `SeqLike.union`
50+
seq.union(seq)(breakOut): List[Int]
51+
52+
53+
//`SetLike.map`
54+
Set(1).map(x => x)(breakOut): List[Int]
55+
56+
57+
// `TraversableLike ++`
58+
(ts ++ ts )(breakOut): Set[Int]
59+
60+
// `TraversableLike ++:`
61+
(ts ++: ts)(breakOut): Set[Int]
62+
63+
// `TraversableLike.collect`
64+
ts.collect{ case x => x }(breakOut): Set[Int]
65+
66+
// `TraversableLike.flatMap`
67+
ts.flatMap(x => List(x))(breakOut): collection.SortedSet[Int]
68+
69+
// `TraversableLike.map`
70+
ts.map(_ + 1)(breakOut): Set[Int]
71+
72+
// `TraversableLike.scanLeft`
73+
ts.scanLeft(0)((a, b) => a + b)(breakOut): Set[Int]
74+
75+
76+
// `Vector ++`
77+
(vec ++ List(1))(breakOut): List[Int]
78+
79+
// `Vector +:`
80+
(1 +: vec)(breakOut): List[Int]
81+
82+
// `Vector :+`
83+
(vec :+ 1)(breakOut): List[Int]
84+
85+
// `Vector.updated`
86+
(vec.updated(0, 0))(breakOut): List[Int]
87+
88+
// Future
89+
Future.sequence(List(Future(1)))(breakOut, global): Future[Seq[Int]]
90+
Future.traverse(List(1))(x => Future(x))(breakOut, global): Future[Seq[Int]]
91+
92+
// Iterable
93+
List(1).map(x => x)(breakOut): Iterator[Int]
94+
95+
// Specific collections
96+
List(1 -> "1").map(x => x)(breakOut): immutable.SortedMap[Int, String]
97+
List(1 -> "1").map(x => x)(breakOut): immutable.HashMap[Int, String]
98+
List(1 -> "1").map(x => x)(breakOut): immutable.ListMap[Int, String]
99+
List(1 -> "1").map(x => x)(breakOut): immutable.TreeMap[Int, String]
100+
List(1 -> "1").map(x => x)(breakOut): mutable.SortedMap[Int, String]
101+
List(1 -> "1").map(x => x)(breakOut): mutable.HashMap[Int, String]
102+
List(1 -> "1").map(x => x)(breakOut): mutable.ListMap[Int, String]
103+
List(1 -> "1").map(x => x)(breakOut): mutable.TreeMap[Int, String]
104+
List(1 -> "1").map(x => x)(breakOut): mutable.Map[Int, String]
105+
List(1 -> "1").map(x => x)(breakOut): immutable.IntMap[String]
106+
List(1L -> "1").map(x => x)(breakOut): immutable.LongMap[String]
107+
List(1L -> "1").map(x => x)(breakOut): mutable.LongMap[String]
25108
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
2+
3+
4+
package fix
5+
6+
import scala.collection.{immutable, mutable}
7+
import scala.concurrent.Future
8+
import scala.concurrent.ExecutionContext.Implicits.global
9+
import scala.collection.compat._
10+
11+
class BreakoutSrc(ts: Iterable[Int], vec: Vector[Int], list: List[Int], seq: Seq[Int]) {
12+
13+
// `IndexedSeqOptimized.zip`
14+
vec.iterator.zip(vec.iterator).toMap: Map[Int, Int]
15+
16+
// `IterableLike.zip`
17+
seq.iterator.zip(seq.iterator).toMap: Map[Int, Int]
18+
19+
// `IterableLike.zipAll`
20+
seq.iterator.zipAll(seq.iterator, 0, 0).to(scala.Array): Array[(Int, Int)]
21+
22+
// `List ++`
23+
(list.iterator ++ list).to(scala.collection.immutable.Set): Set[Int]
24+
25+
// `List +:`
26+
(1 +: list.view).to(scala.collection.immutable.Set): Set[Int]
27+
28+
// `List.collect`
29+
list.iterator.collect{ case x => x}.to(scala.collection.immutable.Set): Set[Int]
30+
31+
// `List.flatMap`
32+
list.iterator.flatMap(x => List(x)).to(scala.collection.immutable.Set): Set[Int]
33+
34+
// `List.map`
35+
list.iterator.map(x => x).to(scala.collection.immutable.Set): Set[Int]
36+
37+
// `SeqLike.reverseMap`
38+
seq.reverseIterator.map(_ + 1).to(scala.collection.immutable.Set): Set[Int]
39+
40+
// `SeqLike +:`
41+
(1 +: seq.view).to(scala.collection.immutable.List): List[Int]
42+
43+
// `SeqLike :+`
44+
(seq.view :+ 1).to(scala.collection.immutable.List): List[Int]
45+
46+
// `SeqLike.updated`
47+
(seq.view.updated(0, 0)).to(scala.collection.immutable.List): List[Int]
48+
49+
// `SeqLike.union`
50+
seq.iterator.concat(seq).to(scala.collection.immutable.List): List[Int]
51+
52+
53+
//`SetLike.map`
54+
Set(1).iterator.map(x => x).to(scala.collection.immutable.List): List[Int]
55+
56+
57+
// `TraversableLike ++`
58+
(ts.iterator ++ ts ).to(scala.collection.immutable.Set): Set[Int]
59+
60+
// `TraversableLike ++:`
61+
(ts ++: ts.view).to(scala.collection.immutable.Set): Set[Int]
62+
63+
// `TraversableLike.collect`
64+
ts.iterator.collect{ case x => x }.to(scala.collection.immutable.Set): Set[Int]
65+
66+
// `TraversableLike.flatMap`
67+
ts.iterator.flatMap(x => List(x)).to(scala.collection.SortedSet): collection.SortedSet[Int]
68+
69+
// `TraversableLike.map`
70+
ts.iterator.map(_ + 1).to(scala.collection.immutable.Set): Set[Int]
71+
72+
// `TraversableLike.scanLeft`
73+
ts.iterator.scanLeft(0)((a, b) => a + b).to(scala.collection.immutable.Set): Set[Int]
74+
75+
76+
// `Vector ++`
77+
(vec.iterator ++ List(1)).to(scala.collection.immutable.List): List[Int]
78+
79+
// `Vector +:`
80+
(1 +: vec.view).to(scala.collection.immutable.List): List[Int]
81+
82+
// `Vector :+`
83+
(vec.view :+ 1).to(scala.collection.immutable.List): List[Int]
84+
85+
// `Vector.updated`
86+
(vec.view.updated(0, 0)).to(scala.collection.immutable.List): List[Int]
87+
88+
// Future
89+
Future.sequence(List(Future(1)))(scala.collection.immutable.List, global): Future[Seq[Int]]
90+
Future.traverse(List(1))(x => Future(x))(scala.collection.immutable.List, global): Future[Seq[Int]]
91+
92+
// Iterable
93+
List(1).iterator.map(x => x): Iterator[Int]
94+
95+
// Specific collections
96+
scala.collection.immutable.SortedMap.from(List(1 -> "1").iterator.map(x => x)): immutable.SortedMap[Int, String]
97+
scala.collection.immutable.HashMap.from(List(1 -> "1").iterator.map(x => x)): immutable.HashMap[Int, String]
98+
scala.collection.immutable.ListMap.from(List(1 -> "1").iterator.map(x => x)): immutable.ListMap[Int, String]
99+
scala.collection.immutable.TreeMap.from(List(1 -> "1").iterator.map(x => x)): immutable.TreeMap[Int, String]
100+
scala.collection.mutable.SortedMap.from(List(1 -> "1").iterator.map(x => x)): mutable.SortedMap[Int, String]
101+
scala.collection.mutable.HashMap.from(List(1 -> "1").iterator.map(x => x)): mutable.HashMap[Int, String]
102+
scala.collection.mutable.ListMap.from(List(1 -> "1").iterator.map(x => x)): mutable.ListMap[Int, String]
103+
scala.collection.mutable.TreeMap.from(List(1 -> "1").iterator.map(x => x)): mutable.TreeMap[Int, String]
104+
scala.collection.mutable.Map.from(List(1 -> "1").iterator.map(x => x)): mutable.Map[Int, String]
105+
scala.collection.immutable.IntMap.from(List(1 -> "1").iterator.map(x => x)): immutable.IntMap[String]
106+
scala.collection.immutable.LongMap.from(List(1L -> "1").iterator.map(x => x)): immutable.LongMap[String]
107+
scala.collection.mutable.LongMap.from(List(1L -> "1").iterator.map(x => x)): mutable.LongMap[String]
108+
}

scalafix/output213/src/main/scala/fix/BreakoutSrc.scala

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)