Skip to content

Commit 4b2d629

Browse files
committed
Prototype toX and to[X] conversions.
Kind of hacky, but it does mostly the right thing Remaining failing tests ```diff - xs.to(Set) - xs.to(Map) - xs.iterator + xs.to(strawman.collection.immutable.Set) + xs.to(strawman.collection.Map) + xs.iterator() ```
1 parent 3311594 commit 4b2d629

File tree

6 files changed

+92
-32
lines changed

6 files changed

+92
-32
lines changed

input/src/main/scala/fix/Collectionstrawman_v0.scala

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* ONLY
1+
/*
22
rewrite = "scala:fix.Collectionstrawman_v0"
33
*/
44
package fix
@@ -52,15 +52,3 @@ object Collectionstrawman_v0_Range {
5252
(0 until 10).map(_ + 3)
5353
}
5454

55-
object Collectionstrawman_v0_Traversable {
56-
def foo(xs: Traversable[(Int, String)], ys: List[Int]): Unit = {
57-
xs.toList
58-
xs.toSet
59-
ys.toSeq
60-
xs.to[List]
61-
xs.to[Set]
62-
xs.toMap
63-
xs.toIterator
64-
ys.iterator
65-
}
66-
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
rewrite = "scala:fix.Collectionstrawman_v0"
3+
*/
4+
package fix
5+
6+
object Collectionstrawman_v0_Traversable {
7+
def foo(xs: Traversable[(Int, String)], ys: List[Int]): Unit = {
8+
xs.toList
9+
xs.toSet
10+
ys.toSeq
11+
xs.to[List]
12+
xs.to[Set]
13+
xs.toMap
14+
xs.toIterator
15+
ys.iterator
16+
}
17+
}

output/src/main/scala/fix/Collectionstrawman_v0.scala

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package fix
22

33
import scala.Predef.{ augmentString => _, intArrayOps => _, wrapString => _, _ }
4-
import strawman.collection.{ Iterable, Set, arrayToArrayOps, stringToStringOps }
4+
import strawman.collection.{ arrayToArrayOps, stringToStringOps }
55
import strawman.collection.immutable.{ ::, HashMap, LazyList, List, Map, Nil, Range, Vector }
66
import strawman.collection.immutable.LazyList.#::
77
import strawman.collection.mutable.ArrayBuffer
@@ -52,15 +52,3 @@ object Collectionstrawman_v0_Range {
5252
Range(0, 10).map(_ + 3)
5353
}
5454

55-
object Collectionstrawman_v0_Traversable {
56-
def foo(xs: Iterable[(Int, String)], ys: List[Int]): Unit = {
57-
xs.to(List)
58-
xs.to(Set)
59-
ys.toSeq
60-
xs.to(List)
61-
xs.to(strawman.collection.immutable.Set)
62-
xs.to(strawman.collection.Map)
63-
xs.iterator()
64-
ys.iterator()
65-
}
66-
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package fix
2+
3+
import strawman.collection.Iterable
4+
import strawman.collection.immutable.List
5+
object Collectionstrawman_v0_Traversable {
6+
def foo(xs: Iterable[(Int, String)], ys: List[Int]): Unit = {
7+
xs.to(List)
8+
xs.to(Set)
9+
ys.toSeq
10+
xs.to(List)
11+
xs.to(strawman.collection.immutable.Set)
12+
xs.to(strawman.collection.Map)
13+
xs.iterator()
14+
ys.iterator()
15+
}
16+
}

output/src/main/scala/fix/ListTest.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fix
22

33
import scala.concurrent.Future
4+
import strawman.collection.immutable.List
45

56
class ListTest {
67
def foo(x: Future[List[Int]]) = ???

rewrites/src/main/scala/fix/Collectionstrawman_v0.scala

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ case class Collectionstrawman_v0(sctx: SemanticCtx)
2626
"wrapString" -> "augmentString"
2727
)
2828

29-
def ifSymbolFound(ctx: RewriteCtx): Patch = {
29+
def replaceExtensionMethods(ctx: RewriteCtx): Patch = {
3030
val toImport = for {
3131
r <- ctx.semanticCtx.names
3232
in = r.sym.normalized
@@ -51,7 +51,7 @@ case class Collectionstrawman_v0(sctx: SemanticCtx)
5151
"_root_.scala.runtime.RichInt#to(I)Lscala/collection/immutable/Range/Inclusive;.")
5252
val rangeSymbol = Symbol(
5353
"_root_.scala.runtime.RichInt#until(I)Lscala/collection/immutable/Range;.")
54-
def range(ctx: RewriteCtx): Patch = {
54+
def replaceRange(ctx: RewriteCtx): Patch = {
5555
ctx.tree.collect {
5656
case tree @ Term.ApplyInfix(lhs, op, targs, arg :: Nil)
5757
if op.symbol.contains(inclusiveRange) =>
@@ -64,7 +64,7 @@ case class Collectionstrawman_v0(sctx: SemanticCtx)
6464
}
6565
}.asPatch
6666

67-
def rewrite(ctx: RewriteCtx): Patch = {
67+
def replaceSymbols(ctx: RewriteCtx): Patch = {
6868
def p(name: String) =
6969
s"scala.Predef.$name" -> s"strawman.collection.immutable.$name"
7070
def s(name: String, rename: Option[String] = None) =
@@ -94,12 +94,62 @@ case class Collectionstrawman_v0(sctx: SemanticCtx)
9494
s("Traversable", Some("Iterable")),
9595
"scala.Iterable" -> "strawman.collection.Iterable",
9696
"scala.Traversable" -> "strawman.collection.Iterable",
97+
"scala.collection.TraversableLike.toIterator" -> "iterator",
9798
"scala.`#::`" -> "strawman.collection.immutable.LazyList.`#::`",
9899
s("Vector"),
99100
i("Vector"),
100101
m("ArrayBuffer")
101-
) +
102-
ifSymbolFound(ctx) +
103-
range(ctx)
102+
)
103+
}
104+
105+
case class SymbolMatcher(symbols: Symbol*) {
106+
def unapply(arg: Tree): Option[(Tree, Symbol)] =
107+
sctx.symbol(arg.pos).flatMap { sym =>
108+
if (symbols.exists(_.isSameNormalized(sym))) Some(arg -> sym)
109+
else None
110+
}
111+
}
112+
113+
object WithSymbol {
114+
def unapply(arg: Tree): Option[(Tree, Symbol)] =
115+
sctx.symbol(arg.pos).map(x => arg -> x)
116+
}
117+
118+
val toX = SymbolMatcher(
119+
Symbol("_root_.scala.collection.TraversableOnce.toMap."),
120+
Symbol("_root_.scala.collection.TraversableOnce.toList."),
121+
Symbol("_root_.scala.collection.TraversableOnce.toSet.")
122+
)
123+
val toTpe = SymbolMatcher(
124+
Symbol("_root_.scala.collection.TraversableLike.to.")
125+
)
126+
val iterator = SymbolMatcher(
127+
Symbol("_root_.scala.collection.LinearSeqLike.iterator.")
128+
)
129+
130+
def replaceToList(ctx: RewriteCtx) =
131+
ctx.tree.collect {
132+
case iterator(n: Name, _) =>
133+
ctx.addRight(n.tokens.last, "()")
134+
case toX(n: Name, s) =>
135+
ctx.replaceTree(n, s"to(${s.name.stripPrefix("to")})")
136+
case toTpe(n: Name, _) =>
137+
ctx.debug(n)
138+
(for {
139+
name <- n.tokens.lastOption
140+
_ = ctx.debug(name)
141+
open <- ctx.tokenList.find(name)(t => t.is[Token.LeftBracket])
142+
_ = ctx.debug(open)
143+
close <- ctx.matching.close(open.asInstanceOf[Token.LeftBracket])
144+
} yield
145+
ctx.replaceToken(open, "(") +
146+
ctx.replaceToken(close, ")")).getOrElse(Patch.empty)
147+
}.asPatch
148+
149+
def rewrite(ctx: RewriteCtx): Patch = {
150+
replaceToList(ctx) +
151+
replaceSymbols(ctx) +
152+
replaceExtensionMethods(ctx) +
153+
replaceRange(ctx)
104154
}
105155
}

0 commit comments

Comments
 (0)