From 75e38810a5c52cdb3c28d0a2f4b4515a440c1c7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Mass=C3=A9?= Date: Tue, 17 Jul 2018 14:38:53 +0200 Subject: [PATCH 1/2] Remove LinearSeq rewrite & Add compat import when doing .to[X] rewrite --- .../src/main/scala/fix/LinearSeqSrc.scala | 10 ---- .../src/main/scala/fix/TraversableBug.scala | 8 +++ .../src/main/scala/fix/LinearSeqSrc.scala | 10 ---- .../src/main/scala/fix/TraversableBug.scala | 9 +++ .../src/main/scala/fix/Stable212Base.scala | 59 +++++++++++-------- 5 files changed, 52 insertions(+), 44 deletions(-) delete mode 100644 scalafix/input/src/main/scala/fix/LinearSeqSrc.scala create mode 100644 scalafix/input/src/main/scala/fix/TraversableBug.scala delete mode 100644 scalafix/output212/src/main/scala/fix/LinearSeqSrc.scala create mode 100644 scalafix/output212/src/main/scala/fix/TraversableBug.scala diff --git a/scalafix/input/src/main/scala/fix/LinearSeqSrc.scala b/scalafix/input/src/main/scala/fix/LinearSeqSrc.scala deleted file mode 100644 index d60f5072..00000000 --- a/scalafix/input/src/main/scala/fix/LinearSeqSrc.scala +++ /dev/null @@ -1,10 +0,0 @@ -/* -rule = "scala:fix.CrossCompat" - */ -package fix - -import scala.collection.LinearSeq - -object LinearSeqSrc { - LinearSeq(1) -} diff --git a/scalafix/input/src/main/scala/fix/TraversableBug.scala b/scalafix/input/src/main/scala/fix/TraversableBug.scala new file mode 100644 index 00000000..07d557bb --- /dev/null +++ b/scalafix/input/src/main/scala/fix/TraversableBug.scala @@ -0,0 +1,8 @@ +/* +rule = "scala:fix.CrossCompat" + */ +package fix + +object TraversableBug { + Array(1).to[List] +} diff --git a/scalafix/output212/src/main/scala/fix/LinearSeqSrc.scala b/scalafix/output212/src/main/scala/fix/LinearSeqSrc.scala deleted file mode 100644 index 654a1fc0..00000000 --- a/scalafix/output212/src/main/scala/fix/LinearSeqSrc.scala +++ /dev/null @@ -1,10 +0,0 @@ - - - -package fix - -import scala.collection.immutable.List - -object LinearSeqSrc { - List(1) -} diff --git a/scalafix/output212/src/main/scala/fix/TraversableBug.scala b/scalafix/output212/src/main/scala/fix/TraversableBug.scala new file mode 100644 index 00000000..8a337b37 --- /dev/null +++ b/scalafix/output212/src/main/scala/fix/TraversableBug.scala @@ -0,0 +1,9 @@ + + + +package fix + +import scala.collection.compat._ +object TraversableBug { + Array(1).to(List) +} diff --git a/scalafix/rules/src/main/scala/fix/Stable212Base.scala b/scalafix/rules/src/main/scala/fix/Stable212Base.scala index e2cf141e..dce30849 100644 --- a/scalafix/rules/src/main/scala/fix/Stable212Base.scala +++ b/scalafix/rules/src/main/scala/fix/Stable212Base.scala @@ -66,11 +66,6 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule => "scala.collection.Traversable" -> "scala.collection.Iterable" ) - val linearSeqToList = - ctx.replaceSymbols( - "scala.collection.LinearSeq" -> "scala.collection.immutable.List", - ) - import scala.meta.contrib._ val hasTraversable = ctx.tree.exists { @@ -83,7 +78,7 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule => if (hasTraversable) addCompatImport(ctx) else Patch.empty - traversableToIterable + linearSeqToList + compatImport + traversableToIterable + compatImport } def replaceSymbolicFold(ctx: RuleCtx): Patch = { @@ -173,15 +168,17 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule => CanBuildFrom(paramss, body, ctx, collectionCanBuildFrom, nothing) }.asPatch - val imports = - ctx.tree.collect { - case i: Importee if collectionCanBuildFromImport.matches(i) => - ctx.removeImportee(i) - }.asPatch + if (useSites.nonEmpty) { + val imports = + ctx.tree.collect { + case i: Importee if collectionCanBuildFromImport.matches(i) => + ctx.removeImportee(i) + }.asPatch - val compatImport = addCompatImport(ctx) + val compatImport = addCompatImport(ctx) - if (useSites.nonEmpty) useSites + imports + compatImport + useSites + imports + compatImport + } else Patch.empty } @@ -212,15 +209,18 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule => } def replaceToList(ctx: RuleCtx): Patch = { - ctx.tree.collect { - case iterator(t: Name) => - ctx.replaceTree(t, "iterator") - - case Term.ApplyType(Term.Select(_, t @ toTpe(n: Name)), _) if !handledTo.contains(n) => - trailingBrackets(n, ctx).map { case (open, close) => - ctx.replaceToken(open, "(") + ctx.replaceToken(close, ")") - }.asPatch + val replaceToIterator = + ctx.tree.collect { + case iterator(t: Name) => + ctx.replaceTree(t, "iterator") + }.asPatch + val replaceTo = + ctx.tree.collect { + case Term.ApplyType(Term.Select(_, t @ toTpe(n: Name)), _) if !handledTo.contains(n) => + trailingBrackets(n, ctx).map { case (open, close) => + ctx.replaceToken(open, "(") + ctx.replaceToken(close, ")") + }.asPatch case Term.Select(_, to @ toTpe(_)) => val synth = ctx.index.synthetics.find(_.position.end == to.pos.end) synth.map{ s => @@ -228,13 +228,24 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule => val col = extractCollection(toCol) ctx.addRight(to, "(" + col + ")") }.getOrElse(Patch.empty) - }.asPatch + }.asPatch + + val compatImport = + if (replaceTo.nonEmpty) addCompatImport(ctx) + else Patch.empty + + compatImport + replaceToIterator + replaceTo } + private val compatImportAdded = mutable.Set[Input]() def addCompatImport(ctx: RuleCtx): Patch = { - if (isCrossCompatible) ctx.addGlobalImport(importer"scala.collection.compat._") - else Patch.empty + if (isCrossCompatible && !compatImportAdded.contains(ctx.input)) { + compatImportAdded += ctx.input + ctx.addGlobalImport(importer"scala.collection.compat._") + } else { + Patch.empty + } } override def fix(ctx: RuleCtx): Patch = { From a70886d661b2940b78ef423ffc1ab11cee54f392 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Mass=C3=A9?= Date: Tue, 17 Jul 2018 15:49:10 +0200 Subject: [PATCH 2/2] Dont insert compat for iterable --- .../src/main/scala/fix/Stable212Base.scala | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/scalafix/rules/src/main/scala/fix/Stable212Base.scala b/scalafix/rules/src/main/scala/fix/Stable212Base.scala index dce30849..dc4cc418 100644 --- a/scalafix/rules/src/main/scala/fix/Stable212Base.scala +++ b/scalafix/rules/src/main/scala/fix/Stable212Base.scala @@ -45,17 +45,21 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule => val traversable = exact( "_root_.scala.package.Traversable#", "_root_.scala.collection.Traversable#", - "_root_.scala.package.Iterable#", - "_root_.scala.collection.Iterable#" ) // == Rules == - def replaceIterableSameElements(ctx: RuleCtx): Patch = { - ctx.tree.collect { - case Term.Apply(Term.Select(lhs, iterableSameElement(_)), List(_)) => - ctx.addRight(lhs, ".iterator") - }.asPatch + val sameElements = + ctx.tree.collect { + case Term.Apply(Term.Select(lhs, iterableSameElement(_)), List(_)) => + ctx.addRight(lhs, ".iterator") + }.asPatch + + val compatImport = + if(sameElements.nonEmpty) addCompatImport(ctx) + else Patch.empty + + sameElements + compatImport }