Skip to content

Commit 80548cc

Browse files
authored
Merge pull request #116 from MasseGuillaume/companion
Rewrite companion => iterableFactory + compat
2 parents 108f23d + e478c2a commit 80548cc

File tree

4 files changed

+185
-1
lines changed

4 files changed

+185
-1
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ private[compat] trait PackageShared {
115115
implicit def toIteratorExtensionMethods[A](self: Iterator[A]): IteratorExtensionMethods[A] =
116116
new IteratorExtensionMethods[A](self)
117117

118+
implicit def toTraversableExtensionMethods[A](self: Traversable[A]): TraversableExtensionMethods[A] =
119+
new TraversableExtensionMethods[A](self)
120+
118121
implicit def toTraversableOnceExtensionMethods[A](self: TraversableOnce[A]): TraversableOnceExtensionMethods[A] =
119122
new TraversableOnceExtensionMethods[A](self)
120123

@@ -194,3 +197,7 @@ class IteratorExtensionMethods[A](private val self: c.Iterator[A]) extends AnyVa
194197
class TraversableOnceExtensionMethods[A](private val self: c.TraversableOnce[A]) extends AnyVal {
195198
def iterator: Iterator[A] = self.toIterator
196199
}
200+
201+
class TraversableExtensionMethods[A](private val self: c.Traversable[A]) extends AnyVal {
202+
def iterableFactory: GenericCompanion[Traversable] = self.companion
203+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
rule = "scala:fix.CrossCompat"
3+
*/
4+
package fix
5+
6+
import scala.collection.{immutable => i, mutable => m}
7+
import scala.{collection => c}
8+
9+
object CompanionSrc {
10+
11+
(null: c.IndexedSeq[Int]).companion
12+
(null: c.Iterable[Int]).companion
13+
(null: c.Seq[Int]).companion
14+
(null: c.Traversable[Int]).companion
15+
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.Stack[Int]).companion
26+
(null: i.Stream[Int]).companion
27+
(null: i.Traversable[Int]).companion
28+
(null: i.Vector[Int]).companion
29+
30+
(null: m.ArrayBuffer[Int]).companion
31+
(null: m.ArraySeq[Int]).companion
32+
(null: m.ArrayStack[Int]).companion
33+
(null: m.Buffer[Int]).companion
34+
(null: m.DoubleLinkedList[Int]).companion
35+
(null: m.HashSet[Int]).companion
36+
(null: m.IndexedSeq[Int]).companion
37+
(null: m.Iterable[Int]).companion
38+
(null: m.LinearSeq[Int]).companion
39+
(null: m.LinkedHashSet[Int]).companion
40+
(null: m.LinkedList[Int]).companion
41+
(null: m.MutableList[Int]).companion
42+
(null: m.Queue[Int]).companion
43+
(null: m.ResizableArray[Int]).companion
44+
(null: m.Seq[Int]).companion
45+
(null: m.Set[Int]).companion
46+
(null: m.Traversable[Int]).companion
47+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
3+
4+
package fix
5+
6+
import scala.collection.{immutable => i, mutable => m}
7+
import scala.{collection => c}
8+
import scala.collection.compat._
9+
10+
object CompanionSrc {
11+
12+
(null: c.IndexedSeq[Int]).iterableFactory
13+
(null: c.Iterable[Int]).iterableFactory
14+
(null: c.Seq[Int]).iterableFactory
15+
(null: collection.Iterable[Int]).iterableFactory
16+
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.Stack[Int]).iterableFactory
27+
(null: i.Stream[Int]).iterableFactory
28+
(null: i.Traversable[Int]).iterableFactory
29+
(null: i.Vector[Int]).iterableFactory
30+
31+
(null: m.ArrayBuffer[Int]).iterableFactory
32+
(null: m.ArraySeq[Int]).iterableFactory
33+
(null: m.ArrayStack[Int]).iterableFactory
34+
(null: m.Buffer[Int]).iterableFactory
35+
(null: m.DoubleLinkedList[Int]).iterableFactory
36+
(null: m.HashSet[Int]).iterableFactory
37+
(null: m.IndexedSeq[Int]).iterableFactory
38+
(null: m.Iterable[Int]).iterableFactory
39+
(null: m.LinearSeq[Int]).iterableFactory
40+
(null: m.LinkedHashSet[Int]).iterableFactory
41+
(null: m.LinkedList[Int]).iterableFactory
42+
(null: m.MutableList[Int]).iterableFactory
43+
(null: m.Queue[Int]).iterableFactory
44+
(null: m.ResizableArray[Int]).iterableFactory
45+
(null: m.Seq[Int]).iterableFactory
46+
(null: m.Set[Int]).iterableFactory
47+
(null: m.Traversable[Int]).iterableFactory
48+
}

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

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,87 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
335335
replaced + compatImport
336336
}
337337

338+
val companionSuffix = "#companion()Lscala/collection/generic/GenericCompanion;."
339+
val companion = {
340+
val cols =
341+
Set(
342+
"_root_.scala.collection." -> List(
343+
"IndexedSeq",
344+
"Iterable",
345+
"LinearSeq",
346+
"Seq",
347+
"Set",
348+
"Traversable"
349+
),
350+
"_root_.scala.collection.immutable." -> List(
351+
"HashSet",
352+
"IndexedSeq",
353+
"Iterable",
354+
"LinearSeq",
355+
"List",
356+
"ListSet",
357+
"Queue",
358+
"Seq",
359+
"Set",
360+
"Stack",
361+
"Stream",
362+
"Traversable",
363+
"Vector"
364+
),
365+
"_root_.scala.collection.mutable." -> List(
366+
"ArrayBuffer",
367+
"ArraySeq",
368+
"Buffer",
369+
"DoubleLinkedList",
370+
"HashSet",
371+
"IndexedSeq",
372+
"Iterable",
373+
"LinearSeq",
374+
"LinkedHashSet",
375+
"LinkedList",
376+
"ListBuffer",
377+
"MutableList",
378+
"Queue",
379+
"ResizableArray",
380+
"Seq",
381+
"Set",
382+
"Traversable",
383+
)
384+
).flatMap{ case (prefix, cols) =>
385+
cols.map(col => prefix + col + companionSuffix)
386+
}
387+
388+
val specific =
389+
Set(
390+
"_root_.scala.collection.mutable.Stack#companion()Lscala/collection/mutable/Stack;.",
391+
"_root_.scala.collection.mutable.ArrayStack#companion()Lscala/collection/mutable/ArrayStack;."
392+
)
393+
394+
exact((cols ++ specific).toSeq:_*)
395+
}
396+
397+
val classManifestCompanion = exact(
398+
"_root_.scala.collection.generic.GenericClassTagTraversableTemplate#classManifestCompanion()Lscala/collection/generic/GenericClassTagCompanion;."
399+
)
400+
401+
private def replaceCompanion(ctx: RuleCtx): Patch = {
402+
val replaced =
403+
ctx.tree.collect {
404+
case Term.Select(_, t @ companion(_)) => {
405+
ctx.replaceTree(t, "iterableFactory")
406+
}
407+
case Term.Select(_, t @ classManifestCompanion(_)) => {
408+
ctx.replaceTree(t, "classTagCompanion")
409+
}
410+
}.asPatch
411+
412+
val compatImport =
413+
if (replaced.nonEmpty) addCompatImport(ctx)
414+
else Patch.empty
415+
416+
replaced + compatImport
417+
}
418+
338419
private val compatImportAdded = mutable.Set[Input]()
339420

340421
def addCompatImport(ctx: RuleCtx): Patch = {
@@ -502,6 +583,7 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
502583
replaceBreakout(ctx) +
503584
replaceFuture(ctx) +
504585
replaceSorted(ctx) +
505-
replaceJavaConversions(ctx)
586+
replaceJavaConversions(ctx) +
587+
replaceCompanion(ctx)
506588
}
507589
}

0 commit comments

Comments
 (0)