Skip to content

Commit 89cc1dd

Browse files
Remove LinearSeq rewrite & Add compat import when doing .to[X] rewrite
1 parent 93fe8ca commit 89cc1dd

File tree

5 files changed

+59
-50
lines changed

5 files changed

+59
-50
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: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,6 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
255255
"scala.collection.Traversable" -> "scala.collection.Iterable"
256256
)
257257

258-
val linearSeqToList =
259-
ctx.replaceSymbols(
260-
"scala.collection.LinearSeq" -> "scala.collection.immutable.List",
261-
)
262-
263258
import scala.meta.contrib._
264259
val hasTraversable =
265260
ctx.tree.exists {
@@ -272,7 +267,7 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
272267
if (hasTraversable) addCompatImport(ctx)
273268
else Patch.empty
274269

275-
traversableToIterable + linearSeqToList + compatImport
270+
traversableToIterable + compatImport
276271
}
277272

278273
def replaceSymbolicFold(ctx: RuleCtx): Patch = {
@@ -362,15 +357,17 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
362357
CanBuildFrom(paramss, body, ctx, collectionCanBuildFrom, nothing)
363358
}.asPatch
364359

365-
val imports =
366-
ctx.tree.collect {
367-
case i: Importee if collectionCanBuildFromImport.matches(i) =>
368-
ctx.removeImportee(i)
369-
}.asPatch
360+
if (useSites.nonEmpty) {
361+
val imports =
362+
ctx.tree.collect {
363+
case i: Importee if collectionCanBuildFromImport.matches(i) =>
364+
ctx.removeImportee(i)
365+
}.asPatch
370366

371-
val compatImport = addCompatImport(ctx)
367+
val compatImport = addCompatImport(ctx)
372368

373-
if (useSites.nonEmpty) useSites + imports + compatImport
369+
useSites + imports + compatImport
370+
}
374371
else Patch.empty
375372
}
376373

@@ -401,29 +398,44 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
401398
}
402399

403400
def replaceToList(ctx: RuleCtx): Patch = {
404-
ctx.tree.collect {
405-
case iterator(t: Name) =>
406-
ctx.replaceTree(t, "iterator")
401+
val replaceToIterator =
402+
ctx.tree.collect {
403+
case iterator(t: Name) =>
404+
ctx.replaceTree(t, "iterator")
405+
}.asPatch
407406

408-
case Term.ApplyType(Term.Select(_, t @ toTpe(n: Name)), _) if !handledTo.contains(n) =>
409-
trailingBrackets(n, ctx).map { case (open, close) =>
410-
ctx.replaceToken(open, "(") + ctx.replaceToken(close, ")")
411-
}.asPatch
407+
val replaceTo =
408+
ctx.tree.collect {
409+
case Term.ApplyType(Term.Select(_, t @ toTpe(n: Name)), _) if !handledTo.contains(n) =>
410+
trailingBrackets(n, ctx).map { case (open, close) =>
411+
ctx.replaceToken(open, "(") + ctx.replaceToken(close, ")")
412+
}.asPatch
413+
414+
case Term.Select(_, to @ toTpe(_)) =>
415+
val synth = ctx.index.synthetics.find(_.position.end == to.pos.end)
416+
synth.map{ s =>
417+
val Term.Apply(_, List(toCol)) = s.text.parse[Term].get
418+
val col = extractCollection(toCol)
419+
ctx.addRight(to, "(" + col + ")")
420+
}.getOrElse(Patch.empty)
421+
}.asPatch
412422

413-
case Term.Select(_, to @ toTpe(_)) =>
414-
val synth = ctx.index.synthetics.find(_.position.end == to.pos.end)
415-
synth.map{ s =>
416-
val Term.Apply(_, List(toCol)) = s.text.parse[Term].get
417-
val col = extractCollection(toCol)
418-
ctx.addRight(to, "(" + col + ")")
419-
}.getOrElse(Patch.empty)
420-
}.asPatch
423+
val compatImport =
424+
if (replaceTo.nonEmpty) addCompatImport(ctx)
425+
else Patch.empty
426+
427+
compatImport + replaceToIterator + replaceTo
421428
}
422429

430+
private val compatImportAdded = mutable.Set[Input]()
423431

424432
def addCompatImport(ctx: RuleCtx): Patch = {
425-
if (isCrossCompatible) ctx.addGlobalImport(importer"scala.collection.compat._")
426-
else Patch.empty
433+
if (isCrossCompatible && !compatImportAdded.contains(ctx.input)) {
434+
compatImportAdded += ctx.input
435+
ctx.addGlobalImport(importer"scala.collection.compat._")
436+
} else {
437+
Patch.empty
438+
}
427439
}
428440

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

0 commit comments

Comments
 (0)