Skip to content
This repository has been archived by the owner on Dec 22, 2021. It is now read-only.

Scalafix rewrite for lazyZip #291

Merged
merged 1 commit into from
Nov 10, 2017
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
rule = "scala:fix.Collectionstrawman_v0"
*/
package fix

import scala.language.postfixOps
object Collectionstrawman_v0_Tuple2Zipped {
def zipped(xs: List[Int], ys: List[Int]): Unit = {
(xs, ys).zipped
(xs,ys).zipped
((xs, ys) zipped)
(((xs) , (ys)).zipped)
(xs, // foo
ys).zipped
/* a */(/* b */ xs /* c */, /* d */ ys /* e */)/* f */./* g */zipped/* h */
(coll(1), coll(2)).zipped
(List(1, 2, 3), Stream.from(1)).zipped
}
def coll(x: Int): List[Int] = ???
}

object Collectionstrawman_v0_Tuple3Zipped {
def zipped(xs: List[Int], ys: List[Int], zs: List[Int]): Unit = {
(xs, ys, zs).zipped
(xs,ys,zs).zipped
((xs, ys, zs) zipped)
(((xs) , (ys) , (zs)).zipped)
(xs, // foo
ys, // bar
zs).zipped
/* a */(/* b */ xs /* c */, /* d */ ys /* e */, /* f */ zs /* g */)/* h */./* i */zipped/* j */
(coll(1), coll(2), coll(3)).zipped
(List(1, 2, 3), Set(1, 2, 3), Stream.from(1)).zipped
}
def coll(x: Int): List[Int] = ???
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package fix

import scala.language.postfixOps
import strawman.collection.immutable.{ LazyList, List, Set }
object Collectionstrawman_v0_Tuple2Zipped {
def zipped(xs: List[Int], ys: List[Int]): Unit = {
xs.lazyZip(ys)
xs.lazyZip(ys)
(xs.lazyZip(ys) )
((xs).lazyZip((ys)))
xs.lazyZip(// foo
ys)
/* a *//* b */ xs /* c */.lazyZip(/* d */ ys /* e */)/* f *//* g *//* h */
coll(1).lazyZip(coll(2))
List(1, 2, 3).lazyZip(LazyList.from(1))
}
def coll(x: Int): List[Int] = ???
}

object Collectionstrawman_v0_Tuple3Zipped {
def zipped(xs: List[Int], ys: List[Int], zs: List[Int]): Unit = {
xs.lazyZip(ys).lazyZip(zs)
xs.lazyZip(ys).lazyZip(zs)
(xs.lazyZip(ys).lazyZip(zs) )
((xs).lazyZip((ys)).lazyZip((zs)))
xs.lazyZip(// foo
ys).lazyZip(// bar
zs)
/* a *//* b */ xs /* c */.lazyZip(/* d */ ys /* e */).lazyZip(/* f */ zs /* g */)/* h *//* i *//* j */
coll(1).lazyZip(coll(2)).lazyZip(coll(3))
List(1, 2, 3).lazyZip(Set(1, 2, 3)).lazyZip(LazyList.from(1))
}
def coll(x: Int): List[Int] = ???
}
50 changes: 48 additions & 2 deletions scalafix/rules/src/main/scala/fix/Collectionstrawman_v0.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ case class Collectionstrawman_v0(index: SemanticdbIndex)
Symbol("_root_.scala.collection.LinearSeqLike.iterator."),
Symbol("_root_.scala.collection.TraversableLike.toIterator.")
)
val tupleZipped = SymbolMatcher.normalized(
Symbol("_root_.scala.runtime.Tuple2Zipped.Ops.zipped."),
Symbol("_root_.scala.runtime.Tuple3Zipped.Ops.zipped.")
)

def replaceToList(ctx: RuleCtx) =
ctx.tree.collect {
Expand All @@ -132,13 +136,55 @@ case class Collectionstrawman_v0(index: SemanticdbIndex)
close <- ctx.matchingParens.close(open.asInstanceOf[Token.LeftBracket])
} yield
ctx.replaceToken(open, "(") +
ctx.replaceToken(close, ")")).getOrElse(Patch.empty)
ctx.replaceToken(close, ")")
).asPatch
}.asPatch

def replaceTupleZipped(ctx: RuleCtx) =
ctx.tree.collect {
case tupleZipped(Term.Select(Term.Tuple(args), name)) =>
val removeTokensPatch =
(for {
zipped <- name.tokens.headOption
closeTuple <- ctx.tokenList.leading(zipped).find(_.is[Token.RightParen])
openTuple <- ctx.matchingParens.open(closeTuple.asInstanceOf[Token.RightParen])
maybeDot = ctx.tokenList.slice(closeTuple, zipped).find(_.is[Token.Dot])
Copy link
Contributor

Choose a reason for hiding this comment

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

Utilities to find dots is missing in scalafix, I've written something similar to this many times 😅

} yield {
ctx.removeToken(openTuple) +
maybeDot.map(ctx.removeToken).asPatch +
ctx.removeToken(zipped)
}).asPatch

def removeSurroundingWhiteSpaces(tk: Token) =
Copy link
Contributor

Choose a reason for hiding this comment

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

Utilities for trimming whitespace are severely missing in scalafix. I'd love to hear if you have idea how this can be generalized

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it would definitely be handy to have such utility in the api. Maybe something like ctx.trimLeft(token) / ctx.trimRight(token) or ctx.removeTokens(tokenList.spacesBefore(token)) / ctx.removeTokens(tokenList.spacesAfter(token)). I can put more thoughts on this and create a PR in scalafix with a proposal.

(ctx.tokenList.trailing(tk).takeWhile(_.is[Token.Space]).map(ctx.removeToken) ++
ctx.tokenList.leading(tk).takeWhile(_.is[Token.Space]).map(ctx.removeToken)).asPatch

val commas =
for {
(prev, next) <- args.zip(args.tail)
tokensBetweenArgs = ctx.tokenList.slice(prev.tokens.last, next.tokens.head)
comma <- tokensBetweenArgs.find(_.is[Token.Comma])
} yield comma

val replaceCommasPatch = commas match {
case head :: tail =>
ctx.replaceToken(head, ".lazyZip(") +
removeSurroundingWhiteSpaces(head) ++
tail.map { comma =>
ctx.replaceToken(comma, ").lazyZip(") +
removeSurroundingWhiteSpaces(comma)
}
case _ => Patch.empty
}

removeTokensPatch + replaceCommasPatch
}.asPatch

Copy link
Contributor Author

@marcelocenerine marcelocenerine Nov 9, 2017

Choose a reason for hiding this comment

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

Handling comments makes the rewrite a lot more complicated compared to the previous version 😅.
Note that in lines 153-155 I'm preserving the original closing ) in order to allow comments on the right side of the last tuple element to be enclosed by the correct parenthesis. Similar care needs to be taken in regards to comments on the right side of a middle triple element (that's the reason for the additional lines in line 174-175).

override def fix(ctx: RuleCtx): Patch = {
replaceToList(ctx) +
replaceSymbols(ctx) +
replaceExtensionMethods(ctx) +
replaceRange(ctx)
replaceRange(ctx) +
replaceTupleZipped(ctx)
}
}