Skip to content

Commit 98e6300

Browse files
Remove LinearSeq rewrite & Add compat import when doing .to[X] rewrite
1 parent 34a7497 commit 98e6300

File tree

5 files changed

+52
-43
lines changed

5 files changed

+52
-43
lines changed

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

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
rule = "scala:fix.CrossCompat"
3+
*/
4+
package fix
5+
6+
object TraversableBug {
7+
Array(1).to[List]
8+
}

scalafix/output212/src/main/scala/fix/LinearSeqSrc.scala

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
4+
package fix
5+
6+
import scala.collection.compat._
7+
object TraversableBug {
8+
Array(1).to(List)
9+
}

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

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
6666
"scala.collection.Traversable" -> "scala.collection.Iterable"
6767
)
6868

69-
val linearSeqToList =
70-
ctx.replaceSymbols(
71-
"scala.collection.LinearSeq" -> "scala.collection.immutable.List",
72-
)
73-
7469
import scala.meta.contrib._
7570
val hasTraversable =
7671
ctx.tree.exists {
@@ -83,7 +78,7 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
8378
if (hasTraversable) addCompatImport(ctx)
8479
else Patch.empty
8580

86-
traversableToIterable + linearSeqToList + compatImport
81+
traversableToIterable + compatImport
8782
}
8883

8984
def replaceSymbolicFold(ctx: RuleCtx): Patch = {
@@ -173,34 +168,51 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
173168
CanBuildFrom(paramss, body, ctx, collectionCanBuildFrom, nothing)
174169
}.asPatch
175170

176-
val imports =
177-
ctx.tree.collect {
178-
case i: Importee if collectionCanBuildFromImport.matches(i) =>
179-
ctx.removeImportee(i)
180-
}.asPatch
171+
if (useSites.nonEmpty) {
172+
val imports =
173+
ctx.tree.collect {
174+
case i: Importee if collectionCanBuildFromImport.matches(i) =>
175+
ctx.removeImportee(i)
176+
}.asPatch
181177

182-
val compatImport = addCompatImport(ctx)
178+
val compatImport = addCompatImport(ctx)
183179

184-
if (useSites.nonEmpty) useSites + imports + compatImport
180+
useSites + imports + compatImport
181+
}
185182
else Patch.empty
186183
}
187184

188185
def replaceToList(ctx: RuleCtx): Patch = {
189-
ctx.tree.collect {
190-
case iterator(t: Name) =>
191-
ctx.replaceTree(t, "iterator")
186+
val replaceToIterator =
187+
ctx.tree.collect {
188+
case iterator(t: Name) =>
189+
ctx.replaceTree(t, "iterator")
190+
}.asPatch
192191

193-
case Term.ApplyType(Term.Select(_, t @ toTpe(n: Name)), _) if !handledTo.contains(n) =>
194-
trailingBrackets(n, ctx).map { case (open, close) =>
195-
ctx.replaceToken(open, "(") + ctx.replaceToken(close, ")")
196-
}.asPatch
197-
}.asPatch
192+
val replaceTo =
193+
ctx.tree.collect {
194+
case Term.ApplyType(Term.Select(_, t @ toTpe(n: Name)), _) if !handledTo.contains(n) =>
195+
trailingBrackets(n, ctx).map { case (open, close) =>
196+
ctx.replaceToken(open, "(") + ctx.replaceToken(close, ")")
197+
}.asPatch
198+
}.asPatch
199+
200+
val compatImport =
201+
if (replaceTo.nonEmpty) addCompatImport(ctx)
202+
else Patch.empty
203+
204+
compatImport + replaceToIterator + replaceTo
198205
}
199206

207+
private val compatImportAdded = mutable.Set[Input]()
200208

201209
def addCompatImport(ctx: RuleCtx): Patch = {
202-
if (isCrossCompatible) ctx.addGlobalImport(importer"scala.collection.compat._")
203-
else Patch.empty
210+
if (isCrossCompatible && !compatImportAdded.contains(ctx.input)) {
211+
compatImportAdded += ctx.input
212+
ctx.addGlobalImport(importer"scala.collection.compat._")
213+
} else {
214+
Patch.empty
215+
}
204216
}
205217

206218
override def fix(ctx: RuleCtx): Patch = {

0 commit comments

Comments
 (0)