Skip to content

Commit f7f8b3b

Browse files
Rewrite all Traversable (immutable, mutable)
1 parent 38f8dce commit f7f8b3b

File tree

7 files changed

+108
-89
lines changed

7 files changed

+108
-89
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ lazy val `scalafix-tests` = project
223223
.settings(
224224
scalaVersion := scalafixScala212,
225225
libraryDependencies += "ch.epfl.scala" % "scalafix-testkit" % scalafixVersion % Test cross CrossVersion.full,
226-
buildInfoPackage := "fix",
226+
buildInfoPackage := "build",
227227
buildInfoKeys := Seq[BuildInfoKey](
228228
"inputSourceroot" -> sourceDirectory.in(`scalafix-input`, Compile).value,
229229
"outputSourceroot" -> (baseDirectory in ThisBuild).value / "scalafix/output/src/main",

scalafix/input/src/main/scala/fix/CompanionSrc.scala

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,40 @@ rule = "scala:fix.CrossCompat"
33
*/
44
package fix
55

6-
import scala.collection.{immutable => i, mutable => m}
7-
import scala.{collection => c}
6+
import scala.collection.immutable
7+
import scala.collection.mutable
88

99
object CompanionSrc {
1010

11-
(null: c.IndexedSeq[Int]).companion
12-
(null: c.Iterable[Int]).companion
13-
(null: c.Seq[Int]).companion
14-
(null: c.Traversable[Int]).companion
11+
(null: collection.IndexedSeq[Int]).companion
12+
(null: collection.Iterable[Int]).companion
13+
(null: collection.Seq[Int]).companion
14+
(null: collection.Traversable[Int]).companion
1515

16-
(null: i.HashSet[Int]).companion
17-
(null: i.IndexedSeq[Int]).companion
18-
(null: i.Iterable[Int]).companion
19-
(null: i.LinearSeq[Int]).companion
20-
(null: i.List[Int]).companion
21-
(null: i.ListSet[Int]).companion
22-
(null: i.Queue[Int]).companion
23-
(null: i.Seq[Int]).companion
24-
(null: i.Set[Int]).companion
25-
(null: i.Stream[Int]).companion
26-
(null: i.Traversable[Int]).companion
27-
(null: i.Vector[Int]).companion
16+
(null: immutable.HashSet[Int]).companion
17+
(null: immutable.IndexedSeq[Int]).companion
18+
(null: immutable.Iterable[Int]).companion
19+
(null: immutable.LinearSeq[Int]).companion
20+
(null: immutable.List[Int]).companion
21+
(null: immutable.ListSet[Int]).companion
22+
(null: immutable.Queue[Int]).companion
23+
(null: immutable.Seq[Int]).companion
24+
(null: immutable.Set[Int]).companion
25+
(null: immutable.Stream[Int]).companion
26+
(null: immutable.Traversable[Int]).companion
27+
(null: immutable.Vector[Int]).companion
2828

29-
(null: m.ArrayBuffer[Int]).companion
30-
(null: m.ArraySeq[Int]).companion
31-
(null: m.ArrayStack[Int]).companion
32-
(null: m.Buffer[Int]).companion
33-
(null: m.HashSet[Int]).companion
34-
(null: m.IndexedSeq[Int]).companion
35-
(null: m.Iterable[Int]).companion
36-
(null: m.LinearSeq[Int]).companion
37-
(null: m.LinkedHashSet[Int]).companion
38-
(null: m.Queue[Int]).companion
39-
(null: m.Seq[Int]).companion
40-
(null: m.Set[Int]).companion
41-
(null: m.Traversable[Int]).companion
29+
(null: mutable.ArrayBuffer[Int]).companion
30+
(null: mutable.ArraySeq[Int]).companion
31+
(null: mutable.ArrayStack[Int]).companion
32+
(null: mutable.Buffer[Int]).companion
33+
(null: mutable.HashSet[Int]).companion
34+
(null: mutable.IndexedSeq[Int]).companion
35+
(null: mutable.Iterable[Int]).companion
36+
(null: mutable.LinearSeq[Int]).companion
37+
(null: mutable.LinkedHashSet[Int]).companion
38+
(null: mutable.Queue[Int]).companion
39+
(null: mutable.Seq[Int]).companion
40+
(null: mutable.Set[Int]).companion
41+
(null: mutable.Traversable[Int]).companion
4242
}

scalafix/input/src/main/scala/fix/TraversableSrc.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ rule = "scala:fix.CrossCompat"
33
*/
44
package fix
55

6+
import scala.collection.immutable
7+
import scala.collection.mutable
8+
69
trait TraversableSrc {
7-
val t: Traversable[Int]
810
val to: TraversableOnce[Int]
11+
val cto: collection.TraversableOnce[Int]
12+
13+
val t: Traversable[Int]
14+
val ct: collection.Traversable[Int]
15+
val it: immutable.Traversable[Int]
16+
val mt: mutable.Traversable[Int]
917
}

scalafix/output/src/main/scala/fix/CompanionSrc.scala

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,41 @@
33

44
package fix
55

6-
import scala.collection.{immutable => i, mutable => m}
7-
import scala.{collection => c}
6+
import scala.collection.immutable
7+
import scala.collection.mutable
88
import scala.collection.compat._
99

1010
object CompanionSrc {
1111

12-
(null: c.IndexedSeq[Int]).iterableFactory
13-
(null: c.Iterable[Int]).iterableFactory
14-
(null: c.Seq[Int]).iterableFactory
12+
(null: collection.IndexedSeq[Int]).iterableFactory
13+
(null: collection.Iterable[Int]).iterableFactory
14+
(null: collection.Seq[Int]).iterableFactory
1515
(null: collection.Iterable[Int]).iterableFactory
1616

17-
(null: i.HashSet[Int]).iterableFactory
18-
(null: i.IndexedSeq[Int]).iterableFactory
19-
(null: i.Iterable[Int]).iterableFactory
20-
(null: i.LinearSeq[Int]).iterableFactory
21-
(null: i.List[Int]).iterableFactory
22-
(null: i.ListSet[Int]).iterableFactory
23-
(null: i.Queue[Int]).iterableFactory
24-
(null: i.Seq[Int]).iterableFactory
25-
(null: i.Set[Int]).iterableFactory
26-
(null: i.Stream[Int]).iterableFactory
27-
(null: i.Traversable[Int]).iterableFactory
28-
(null: i.Vector[Int]).iterableFactory
29-
30-
(null: m.ArrayBuffer[Int]).iterableFactory
31-
(null: m.ArraySeq[Int]).iterableFactory
32-
(null: m.ArrayStack[Int]).iterableFactory
33-
(null: m.Buffer[Int]).iterableFactory
34-
(null: m.HashSet[Int]).iterableFactory
35-
(null: m.IndexedSeq[Int]).iterableFactory
36-
(null: m.Iterable[Int]).iterableFactory
37-
(null: m.LinearSeq[Int]).iterableFactory
38-
(null: m.LinkedHashSet[Int]).iterableFactory
39-
(null: m.Queue[Int]).iterableFactory
40-
(null: m.Seq[Int]).iterableFactory
41-
(null: m.Set[Int]).iterableFactory
42-
(null: m.Traversable[Int]).iterableFactory
17+
(null: immutable.HashSet[Int]).iterableFactory
18+
(null: immutable.IndexedSeq[Int]).iterableFactory
19+
(null: immutable.Iterable[Int]).iterableFactory
20+
(null: immutable.LinearSeq[Int]).iterableFactory
21+
(null: immutable.List[Int]).iterableFactory
22+
(null: immutable.ListSet[Int]).iterableFactory
23+
(null: immutable.Queue[Int]).iterableFactory
24+
(null: immutable.Seq[Int]).iterableFactory
25+
(null: immutable.Set[Int]).iterableFactory
26+
(null: immutable.Stream[Int]).iterableFactory
27+
(null: immutable.Iterable[Int]).iterableFactory
28+
(null: immutable.Vector[Int]).iterableFactory
29+
30+
(null: mutable.ArrayBuffer[Int]).iterableFactory
31+
(null: mutable.ArraySeq[Int]).iterableFactory
32+
(null: mutable.ArrayStack[Int]).iterableFactory
33+
(null: mutable.Buffer[Int]).iterableFactory
34+
(null: mutable.HashSet[Int]).iterableFactory
35+
(null: mutable.IndexedSeq[Int]).iterableFactory
36+
(null: mutable.Iterable[Int]).iterableFactory
37+
(null: mutable.LinearSeq[Int]).iterableFactory
38+
(null: mutable.LinkedHashSet[Int]).iterableFactory
39+
(null: mutable.Queue[Int]).iterableFactory
40+
(null: mutable.Seq[Int]).iterableFactory
41+
(null: mutable.Set[Int]).iterableFactory
42+
(null: mutable.Iterable[Int]).iterableFactory
4343
}
44-
45-

scalafix/output/src/main/scala/fix/TraversableSrc.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33

44
package fix
55

6+
import scala.collection.immutable
7+
import scala.collection.mutable
68
import scala.collection.compat._
9+
710
trait TraversableSrc {
8-
val t: Iterable[Int]
911
val to: IterableOnce[Int]
12+
val cto: IterableOnce[Int]
13+
14+
val t: Iterable[Int]
15+
val ct: collection.Iterable[Int]
16+
val it: immutable.Iterable[Int]
17+
val mt: mutable.Iterable[Int]
1018
}

scalafix/rules/src/main/scala/fix/Stable212Base.scala

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,8 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
7373
"_root_.scala.collection.immutable.TreeSet#until(Ljava/lang/Object;)Lscala/collection/immutable/TreeSet;."
7474
)
7575

76-
val traversable = exact(
77-
"_root_.scala.collection.Traversable#",
76+
val traversableOnce = exact(
7877
"_root_.scala.collection.TraversableOnce#",
79-
"_root_.scala.package.Traversable#",
8078
"_root_.scala.package.TraversableOnce#"
8179
)
8280

@@ -102,25 +100,32 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
102100
def replaceTraversable(ctx: RuleCtx): Patch = {
103101
val traversableToIterable =
104102
ctx.replaceSymbols(
105-
"scala.Traversable" -> "scala.Iterable",
106-
"scala.collection.Traversable" -> "scala.collection.Iterable",
107-
"scala.TraversableOnce" -> "scala.IterableOnce",
108-
"scala.collection.TraversableOnce" -> "scala.collection.IterableOnce"
103+
"scala.Traversable" -> "scala.Iterable",
104+
"scala.collection.Traversable" -> "scala.collection.Iterable",
105+
"scala.collection.immutable.Traversable" -> "scala.collection.immutable.Iterable",
106+
"scala.collection.mutable.Traversable" -> "scala.collection.mutable.Iterable",
109107
)
110108

111-
import scala.meta.contrib._
112-
val hasTraversable =
113-
ctx.tree.exists {
114-
case traversable(_) => true
115-
case _ => false
109+
val traversableOnceToIterableOnce =
110+
ctx.tree.collect {
111+
case Type.Apply(sel @ Type.Select(chain, traversableOnce(n: Name)), _) =>
112+
val dot = chain.tokens.toList.reverse.drop(1)
116113

117-
}
114+
ctx.removeTokens(chain.tokens) +
115+
ctx.removeTokens(dot) +
116+
ctx.replaceTree(sel, "IterableOnce")
117+
118+
case Type.Apply(traversableOnce(n: Name), _) =>
119+
ctx.replaceTree(n, "IterableOnce")
120+
121+
122+
}.asPatch
118123

119124
val compatImport =
120-
if (hasTraversable) addCompatImport(ctx)
125+
if (traversableOnceToIterableOnce.nonEmpty) addCompatImport(ctx)
121126
else Patch.empty
122127

123-
traversableToIterable + compatImport
128+
traversableOnceToIterableOnce + traversableToIterable + compatImport
124129
}
125130

126131
def replaceSymbolicFold(ctx: RuleCtx): Patch = {

scalafix/tests/src/test/scala/fix/ScalafixTests.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import scalafix._
66

77
class ScalafixTests
88
extends SemanticRuleSuite(
9-
SemanticdbIndex.load(Classpath(AbsolutePath(BuildInfo.inputClassdirectory))),
10-
AbsolutePath(BuildInfo.inputSourceroot),
9+
SemanticdbIndex.load(Classpath(AbsolutePath(build.BuildInfo.inputClassdirectory))),
10+
AbsolutePath(build.BuildInfo.inputSourceroot),
1111
Seq(
12-
AbsolutePath(BuildInfo.outputSourceroot),
13-
AbsolutePath(BuildInfo.output212Sourceroot),
14-
AbsolutePath(BuildInfo.output213Sourceroot),
15-
AbsolutePath(BuildInfo.output212PlusSourceroot),
16-
AbsolutePath(BuildInfo.output213FailureSourceroot)
12+
AbsolutePath(build.BuildInfo.outputSourceroot),
13+
AbsolutePath(build.BuildInfo.output212Sourceroot),
14+
AbsolutePath(build.BuildInfo.output213Sourceroot),
15+
AbsolutePath(build.BuildInfo.output212PlusSourceroot),
16+
AbsolutePath(build.BuildInfo.output213FailureSourceroot)
1717
)
1818
) {
1919

0 commit comments

Comments
 (0)