Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for akka rewrite #100

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions scalafix/input/src/main/scala/fix/LinearSeqSrc.scala

This file was deleted.

8 changes: 8 additions & 0 deletions scalafix/input/src/main/scala/fix/TraversableBug.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
rule = "scala:fix.CrossCompat"
*/
package fix

object TraversableBug {
Array(1).to[List]
}
10 changes: 0 additions & 10 deletions scalafix/output212/src/main/scala/fix/LinearSeqSrc.scala

This file was deleted.

9 changes: 9 additions & 0 deletions scalafix/output212/src/main/scala/fix/TraversableBug.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@



package fix

import scala.collection.compat._
object TraversableBug {
Array(1).to(List)
}
77 changes: 46 additions & 31 deletions scalafix/rules/src/main/scala/fix/Stable212Base.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we remove these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use this to detect if we need to add the compat import for

     ctx.replaceSymbols(
        "scala.Traversable"            -> "scala.Iterable",
        "scala.collection.Traversable" -> "scala.collection.Iterable"
      )

It was incorrectly adding compat import.

)

// == 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
}


Expand All @@ -66,11 +70,6 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
"scala.collection.Traversable" -> "scala.collection.Iterable"
)

val linearSeqToList =
ctx.replaceSymbols(
"scala.collection.LinearSeq" -> "scala.collection.immutable.List",
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this rule?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


import scala.meta.contrib._
val hasTraversable =
ctx.tree.exists {
Expand All @@ -83,7 +82,7 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
if (hasTraversable) addCompatImport(ctx)
else Patch.empty

traversableToIterable + linearSeqToList + compatImport
traversableToIterable + compatImport
}

def replaceSymbolicFold(ctx: RuleCtx): Patch = {
Expand Down Expand Up @@ -173,15 +172,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
}

Expand Down Expand Up @@ -212,29 +213,43 @@ 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 =>
val Term.Apply(_, List(toCol)) = s.text.parse[Term].get
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 = {
Expand Down