Skip to content

Commit ba42911

Browse files
Scalafix rewrite for lazyZip
1 parent 362d648 commit ba42911

File tree

3 files changed

+118
-2
lines changed

3 files changed

+118
-2
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
rule = "scala:fix.Collectionstrawman_v0"
3+
*/
4+
package fix
5+
6+
import scala.language.postfixOps
7+
object Collectionstrawman_v0_Tuple2Zipped {
8+
def zipped(xs: List[Int], ys: List[Int]): Unit = {
9+
(xs, ys).zipped
10+
(xs,ys).zipped
11+
((xs, ys) zipped)
12+
(((xs) , (ys)).zipped)
13+
(xs, // foo
14+
ys).zipped
15+
/* a */(/* b */ xs /* c */, /* d */ ys /* e */)/* f */./* g */zipped/* h */
16+
(coll(1), coll(2)).zipped
17+
(List(1, 2, 3), Stream.from(1)).zipped
18+
}
19+
def coll(x: Int): List[Int] = ???
20+
}
21+
22+
object Collectionstrawman_v0_Tuple3Zipped {
23+
def zipped(xs: List[Int], ys: List[Int], zs: List[Int]): Unit = {
24+
(xs, ys, zs).zipped
25+
(xs,ys,zs).zipped
26+
((xs, ys, zs) zipped)
27+
(((xs) , (ys) , (zs)).zipped)
28+
(xs, // foo
29+
ys, // bar
30+
zs).zipped
31+
/* a */(/* b */ xs /* c */, /* d */ ys /* e */, /* f */ zs /* g */)/* h */./* i */zipped/* j */
32+
(coll(1), coll(2), coll(3)).zipped
33+
(List(1, 2, 3), Set(1, 2, 3), Stream.from(1)).zipped
34+
}
35+
def coll(x: Int): List[Int] = ???
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package fix
2+
3+
import scala.language.postfixOps
4+
import strawman.collection.immutable.{ LazyList, List, Set }
5+
object Collectionstrawman_v0_Tuple2Zipped {
6+
def zipped(xs: List[Int], ys: List[Int]): Unit = {
7+
xs.lazyZip(ys)
8+
xs.lazyZip(ys)
9+
(xs.lazyZip(ys) )
10+
((xs).lazyZip((ys)))
11+
xs.lazyZip(// foo
12+
ys)
13+
/* a *//* b */ xs /* c */.lazyZip(/* d */ ys /* e */)/* f *//* g *//* h */
14+
coll(1).lazyZip(coll(2))
15+
List(1, 2, 3).lazyZip(LazyList.from(1))
16+
}
17+
def coll(x: Int): List[Int] = ???
18+
}
19+
20+
object Collectionstrawman_v0_Tuple3Zipped {
21+
def zipped(xs: List[Int], ys: List[Int], zs: List[Int]): Unit = {
22+
xs.lazyZip(ys).lazyZip(zs)
23+
xs.lazyZip(ys).lazyZip(zs)
24+
(xs.lazyZip(ys).lazyZip(zs) )
25+
((xs).lazyZip((ys)).lazyZip((zs)))
26+
xs.lazyZip(// foo
27+
ys).lazyZip(// bar
28+
zs)
29+
/* a *//* b */ xs /* c */.lazyZip(/* d */ ys /* e */).lazyZip(/* f */ zs /* g */)/* h *//* i *//* j */
30+
coll(1).lazyZip(coll(2)).lazyZip(coll(3))
31+
List(1, 2, 3).lazyZip(Set(1, 2, 3)).lazyZip(LazyList.from(1))
32+
}
33+
def coll(x: Int): List[Int] = ???
34+
}

rules/src/main/scala/fix/Collectionstrawman_v0.scala

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ case class Collectionstrawman_v0(index: SemanticdbIndex)
116116
Symbol("_root_.scala.collection.LinearSeqLike.iterator."),
117117
Symbol("_root_.scala.collection.TraversableLike.toIterator.")
118118
)
119+
val tupleZipped = SymbolMatcher.normalized(
120+
Symbol("_root_.scala.runtime.Tuple2Zipped.Ops.zipped."),
121+
Symbol("_root_.scala.runtime.Tuple3Zipped.Ops.zipped.")
122+
)
119123

120124
def replaceToList(ctx: RuleCtx) =
121125
ctx.tree.collect {
@@ -132,13 +136,55 @@ case class Collectionstrawman_v0(index: SemanticdbIndex)
132136
close <- ctx.matchingParens.close(open.asInstanceOf[Token.LeftBracket])
133137
} yield
134138
ctx.replaceToken(open, "(") +
135-
ctx.replaceToken(close, ")")).getOrElse(Patch.empty)
139+
ctx.replaceToken(close, ")")
140+
).asPatch
141+
}.asPatch
142+
143+
def replaceTupleZipped(ctx: RuleCtx) =
144+
ctx.tree.collect {
145+
case tupleZipped(Term.Select(Term.Tuple(args), name)) =>
146+
val removeTokensPatch =
147+
(for {
148+
zipped <- name.tokens.headOption
149+
closeTuple <- ctx.tokenList.leading(zipped).find(_.is[Token.RightParen])
150+
openTuple <- ctx.matchingParens.open(closeTuple.asInstanceOf[Token.RightParen])
151+
maybeDot = ctx.tokenList.slice(closeTuple, zipped).find(_.is[Token.Dot])
152+
} yield {
153+
ctx.removeToken(openTuple) +
154+
maybeDot.map(ctx.removeToken).asPatch +
155+
ctx.removeToken(zipped)
156+
}).asPatch
157+
158+
def removeSurroundingWhiteSpaces(tk: Token) =
159+
(ctx.tokenList.trailing(tk).takeWhile(_.is[Token.Space]).map(ctx.removeToken) ++
160+
ctx.tokenList.leading(tk).takeWhile(_.is[Token.Space]).map(ctx.removeToken)).asPatch
161+
162+
val commas =
163+
for {
164+
(prev, next) <- args.zip(args.tail)
165+
tokensBetweenArgs = ctx.tokenList.slice(prev.tokens.last, next.tokens.head)
166+
comma <- tokensBetweenArgs.find(_.is[Token.Comma])
167+
} yield comma
168+
169+
val replaceCommasPatch = commas match {
170+
case head :: tail =>
171+
ctx.replaceToken(head, ".lazyZip(") +
172+
removeSurroundingWhiteSpaces(head) ++
173+
tail.map { comma =>
174+
ctx.replaceToken(comma, ").lazyZip(") +
175+
removeSurroundingWhiteSpaces(comma)
176+
}
177+
case _ => Patch.empty
178+
}
179+
180+
removeTokensPatch + replaceCommasPatch
136181
}.asPatch
137182

138183
override def fix(ctx: RuleCtx): Patch = {
139184
replaceToList(ctx) +
140185
replaceSymbols(ctx) +
141186
replaceExtensionMethods(ctx) +
142-
replaceRange(ctx)
187+
replaceRange(ctx) +
188+
replaceTupleZipped(ctx)
143189
}
144190
}

0 commit comments

Comments
 (0)