Skip to content

Commit 67c8912

Browse files
authored
Merge pull request scala#108 from MasseGuillaume/to-synthetic
Rewrite xs.to => xs.to(CC) (fix scala#106)
2 parents ee152e2 + 51d4c57 commit 67c8912

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

scalafix/input/src/main/scala/fix/TraversableSrc.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ object TraversableSrc {
1414
def m1(xs: Traversable[Int]): List[Int] =
1515
xs.to
1616

17-
def m2(xs: Traversable[Int]): List[Int] = xs.to
17+
List[Int]() // unrelated matching brackets
1818
}

scalafix/output212/src/main/scala/fix/TraversableSrc.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object TraversableSrc {
1313
}
1414

1515
def m1(xs: Iterable[Int]): List[Int] =
16-
xs.to
16+
xs.to(scala.collection.immutable.List)
1717

18-
def m2(xs: Iterable[Int]): List[Int] = xs.to
18+
List[Int]() // unrelated matching brackets
1919
}

scalafix/rules/src/main/scala/fix/Stable212Base.scala

+34
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,32 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
185185
else Patch.empty
186186
}
187187

188+
def extractCollection(toCol: Tree): String = {
189+
toCol match {
190+
case Term.ApplyType(q"scala.Predef.fallbackStringCanBuildFrom", _) =>
191+
"scala.collection.immutable.IndexedSeq"
192+
case Term.ApplyType(Term.Select(coll,_), _) =>
193+
coll.syntax
194+
case Term.Apply(Term.ApplyType(Term.Select(coll, _), _), _) =>
195+
coll.syntax
196+
case Term.Select(coll,_) =>
197+
coll.syntax
198+
case _ => {
199+
throw new Exception(
200+
s"""|cannot extract collection from .to
201+
|
202+
|---------------------------------------------
203+
|syntax:
204+
|${toCol.syntax}
205+
|
206+
|---------------------------------------------
207+
|structure:
208+
|${toCol.structure}""".stripMargin
209+
)
210+
}
211+
}
212+
}
213+
188214
def replaceToList(ctx: RuleCtx): Patch = {
189215
ctx.tree.collect {
190216
case iterator(t: Name) =>
@@ -194,6 +220,14 @@ trait Stable212Base extends CrossCompatibility { self: SemanticRule =>
194220
trailingBrackets(n, ctx).map { case (open, close) =>
195221
ctx.replaceToken(open, "(") + ctx.replaceToken(close, ")")
196222
}.asPatch
223+
224+
case Term.Select(_, to @ toTpe(_)) =>
225+
val synth = ctx.index.synthetics.find(_.position.end == to.pos.end)
226+
synth.map{ s =>
227+
val Term.Apply(_, List(toCol)) = s.text.parse[Term].get
228+
val col = extractCollection(toCol)
229+
ctx.addRight(to, "(" + col + ")")
230+
}.getOrElse(Patch.empty)
197231
}.asPatch
198232
}
199233

0 commit comments

Comments
 (0)