Skip to content

Rule for symbolic foldLeft(/:) and foldRight(:/) (fix #27) #35

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
Jun 12, 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
12 changes: 12 additions & 0 deletions scalafix/input/src/main/scala/fix/FoldSrc.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
rule = "scala:fix.Scalacollectioncompat_NewCollections"
*/
package fix

class FoldSrc(xs: List[Int]){
val f: (Int, Int) => Int = (x, y) => x + y
val g: (Int, Int) => Int = (x, y) => x * y

(0 /: xs)(f)
(xs :\ 1)(g)
}
12 changes: 12 additions & 0 deletions scalafix/output/src/main/scala/fix/FoldSrc.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@



package fix

class FoldSrc(xs: List[Int]){
val f: (Int, Int) => Int = (x, y) => x + y
val g: (Int, Int) => Int = (x, y) => x * y

xs.foldLeft(0)(f)
xs.foldRight(1)(g)
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ case class Scalacollectioncompat_NewCollections(index: SemanticdbIndex)
Symbol("_root_.scala.runtime.Tuple2Zipped.Ops.zipped."),
Symbol("_root_.scala.runtime.Tuple3Zipped.Ops.zipped.")
)
def foldSymbol(isLeft: Boolean): SymbolMatcher = {
val op =
if (isLeft) "/:"
else ":\\"

SymbolMatcher.normalized(Symbol(s"_root_.scala.collection.TraversableOnce.`$op`."))
}
val foldLeftSymbol = foldSymbol(isLeft = true)
val foldRightSymbol = foldSymbol(isLeft = false)

val retainMap =
SymbolMatcher.normalized(
Expand Down Expand Up @@ -60,7 +69,16 @@ case class Scalacollectioncompat_NewCollections(index: SemanticdbIndex)
ctx.replaceToken(open, "{case ") +
ctx.replaceToken(close, "}") +
ctx.replaceTree(n, "filterInPlace")
).asPatch
).asPatch
}.asPatch

def replaceSymbolicFold(ctx: RuleCtx) =
ctx.tree.collect {
case Term.Apply(ap @ Term.ApplyInfix(rhs, foldRightSymbol(_), _, List(lhs)), _) =>
ctx.replaceTree(ap, s"$rhs.foldRight($lhs)")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@julienrf this would remove formatting and delete comments inside the parenthesis of the infix apply.

ex:

/*1*/(/*2*/ 0 /*3*/ /: /*4*/ xs /*5*/)/*6*/(f)
/*1*/xs.foldLeft(0)/*6*/(f)

the best we can achieve is to preserve one side, since this refactoring swap them.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don’t think lot of people write comments at those places anyway. If someone raises the issue we should address it, but for now let’s stick to this simple solution.


case Term.Apply(ap @ Term.ApplyInfix(lhs, foldLeftSymbol(_), _, List(rhs)), _) =>
ctx.replaceTree(ap, s"$rhs.foldLeft($lhs)")
}.asPatch

def replaceToList(ctx: RuleCtx) =
Expand Down Expand Up @@ -147,6 +165,7 @@ case class Scalacollectioncompat_NewCollections(index: SemanticdbIndex)
replaceCopyToBuffer(ctx) +
replaceStreamAppend(ctx) +
replaceMutableMap(ctx) +
replaceMutableSet(ctx)
replaceMutableSet(ctx) +
replaceSymbolicFold(ctx)
}
}