Skip to content

Commit 2934852

Browse files
Split Iterator and .to[CC] migrations
1 parent f7f8b3b commit 2934852

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ object ReplaceToSrc {
1111
xs.to
1212

1313
List[Int]() // unrelated matching brackets
14+
15+
def m2(xs: List[Int]): Iterable[Int] =
16+
xs.to
1417
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ object ReplaceToSrc {
1212
xs.to(scala.collection.immutable.List)
1313

1414
List[Int]() // unrelated matching brackets
15+
16+
def m2(xs: List[Int]): Iterable[Int] =
17+
xs.to(scala.collection.immutable.IndexedSeq)
1518
}

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
3131
val foldLeftSymbol = foldSymbol(isLeft = true)
3232
val foldRightSymbol = foldSymbol(isLeft = false)
3333

34-
val iterator = normalized("_root_.scala.collection.TraversableLike.toIterator.")
34+
3535
val toTpe = normalized(
3636
"_root_.scala.collection.TraversableLike.to.",
3737
"_root_.scala.collection.TraversableOnce.to.",
@@ -73,6 +73,7 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
7373
"_root_.scala.collection.immutable.TreeSet#until(Ljava/lang/Object;)Lscala/collection/immutable/TreeSet;."
7474
)
7575

76+
val `TraversableLike.toIterator` = normalized("_root_.scala.collection.TraversableLike.toIterator.")
7677
val traversableOnce = exact(
7778
"_root_.scala.collection.TraversableOnce#",
7879
"_root_.scala.package.TraversableOnce#"
@@ -98,6 +99,13 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
9899
}
99100

100101
def replaceTraversable(ctx: RuleCtx): Patch = {
102+
103+
val traversableIterator =
104+
ctx.tree.collect {
105+
case `TraversableLike.toIterator`(t: Name) =>
106+
ctx.replaceTree(t, "iterator")
107+
}.asPatch
108+
101109
val traversableToIterable =
102110
ctx.replaceSymbols(
103111
"scala.Traversable" -> "scala.Iterable",
@@ -122,10 +130,10 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
122130
}.asPatch
123131

124132
val compatImport =
125-
if (traversableOnceToIterableOnce.nonEmpty) addCompatImport(ctx)
133+
if (traversableOnceToIterableOnce.nonEmpty || traversableIterator.nonEmpty) addCompatImport(ctx)
126134
else Patch.empty
127135

128-
traversableOnceToIterableOnce + traversableToIterable + compatImport
136+
traversableOnceToIterableOnce + traversableToIterable + traversableIterator + compatImport
129137
}
130138

131139
def replaceSymbolicFold(ctx: RuleCtx): Patch = {
@@ -256,14 +264,8 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
256264
}
257265
}
258266

259-
def replaceToList(ctx: RuleCtx): Patch = {
260-
val replaceToIterator =
261-
ctx.tree.collect {
262-
case iterator(t: Name) =>
263-
ctx.replaceTree(t, "iterator")
264-
}.asPatch
265-
266-
val replaceTo =
267+
def replaceTo(ctx: RuleCtx): Patch = {
268+
val patch =
267269
ctx.tree.collect {
268270
case Term.ApplyType(Term.Select(_, t @ toTpe(n: Name)), _) if !handledTo.contains(n) =>
269271
trailingBrackets(n, ctx).map { case (open, close) =>
@@ -291,10 +293,10 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
291293
}.asPatch
292294

293295
val compatImport =
294-
if (replaceTo.nonEmpty) addCompatImport(ctx)
296+
if (patch.nonEmpty) addCompatImport(ctx)
295297
else Patch.empty
296298

297-
compatImport + replaceToIterator + replaceTo
299+
compatImport + patch
298300
}
299301

300302
def replaceFuture(ctx: RuleCtx): Patch = {
@@ -573,7 +575,7 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
573575
override def fix(ctx: RuleCtx): Patch = {
574576
replaceTraversable(ctx) +
575577
replaceCanBuildFrom(ctx) +
576-
replaceToList(ctx) +
578+
replaceTo(ctx) +
577579
replaceCopyToBuffer(ctx) +
578580
replaceSymbolicFold(ctx) +
579581
replaceSetMapPlus2(ctx) +

0 commit comments

Comments
 (0)