@@ -255,11 +255,6 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
255
255
" scala.collection.Traversable" -> " scala.collection.Iterable"
256
256
)
257
257
258
- val linearSeqToList =
259
- ctx.replaceSymbols(
260
- " scala.collection.LinearSeq" -> " scala.collection.immutable.List" ,
261
- )
262
-
263
258
import scala .meta .contrib ._
264
259
val hasTraversable =
265
260
ctx.tree.exists {
@@ -272,7 +267,7 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
272
267
if (hasTraversable) addCompatImport(ctx)
273
268
else Patch .empty
274
269
275
- traversableToIterable + linearSeqToList + compatImport
270
+ traversableToIterable + compatImport
276
271
}
277
272
278
273
def replaceSymbolicFold (ctx : RuleCtx ): Patch = {
@@ -362,15 +357,17 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
362
357
CanBuildFrom (paramss, body, ctx, collectionCanBuildFrom, nothing)
363
358
}.asPatch
364
359
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
370
366
371
- val compatImport = addCompatImport(ctx)
367
+ val compatImport = addCompatImport(ctx)
372
368
373
- if (useSites.nonEmpty) useSites + imports + compatImport
369
+ useSites + imports + compatImport
370
+ }
374
371
else Patch .empty
375
372
}
376
373
@@ -401,29 +398,44 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
401
398
}
402
399
403
400
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
407
406
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
412
422
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
421
428
}
422
429
430
+ private val compatImportAdded = mutable.Set [Input ]()
423
431
424
432
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
+ }
427
439
}
428
440
429
441
override def fix (ctx : RuleCtx ): Patch = {
0 commit comments