Skip to content

Rewrite xs.to => xs.to(CC) (fix #106) #108

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

Merged
merged 1 commit into from
Jul 20, 2018
Merged
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
2 changes: 1 addition & 1 deletion scalafix/input/src/main/scala/fix/TraversableSrc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ object TraversableSrc {
def m1(xs: Traversable[Int]): List[Int] =
xs.to

def m2(xs: Traversable[Int]): List[Int] = xs.to
List[Int]() // unrelated matching brackets
}
4 changes: 2 additions & 2 deletions scalafix/output212/src/main/scala/fix/TraversableSrc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object TraversableSrc {
}

def m1(xs: Iterable[Int]): List[Int] =
xs.to
xs.to(scala.collection.immutable.List)

def m2(xs: Iterable[Int]): List[Int] = xs.to
List[Int]() // unrelated matching brackets
}
34 changes: 34 additions & 0 deletions scalafix/rules/src/main/scala/fix/Stable212Base.scala
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,32 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
else Patch.empty
}

def extractCollection(toCol: Tree): String = {
toCol match {
case Term.ApplyType(q"scala.Predef.fallbackStringCanBuildFrom", _) =>
"scala.collection.immutable.IndexedSeq"
case Term.ApplyType(Term.Select(coll,_), _) =>
coll.syntax
case Term.Apply(Term.ApplyType(Term.Select(coll, _), _), _) =>
coll.syntax
case Term.Select(coll,_) =>
coll.syntax
case _ => {
throw new Exception(
s"""|cannot extract collection from .to
|
|---------------------------------------------
|syntax:
|${toCol.syntax}
|
|---------------------------------------------
|structure:
|${toCol.structure}""".stripMargin
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we standardize the layout of error messages?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is from the breakout PR, I'm just going to reuse this function.

)
}
}
}

def replaceToList(ctx: RuleCtx): Patch = {
ctx.tree.collect {
case iterator(t: Name) =>
Expand All @@ -194,6 +220,14 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
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
}

Expand Down
2 changes: 1 addition & 1 deletion scalafix/tests/src/test/scala/fix/ScalafixTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ class ScalafixTests

runAllTests()
// to run only one test:
// testsToRun.filter(_.filename.toNIO.getFileName.toString == "IterableSrc.scala" ).foreach(runOn)
// testsToRun.filter(_.filename.toNIO.getFileName.toString == "Playground.scala" ).foreach(runOn)
}