Skip to content

Commit

Permalink
RepeatedRewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Dec 28, 2023
1 parent 7788eca commit 13b9f52
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
11 changes: 11 additions & 0 deletions input/src/main/scala/fix/RepeatedRewriteTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
rule = RepeatedRewrite
*/
package fix

class RepeatedRewriteTest {
val xs = 1 to 10

def f1: List[Int] = List(xs: _*)
def f2: Vector[Int] = Vector(xs ++ xs: _*)
}
8 changes: 8 additions & 0 deletions output/src/main/scala/fix/RepeatedRewriteTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fix

class RepeatedRewriteTest {
val xs = 1 to 10

def f1: List[Int] = List(xs *)
def f2: Vector[Int] = Vector((xs ++ xs) *)
}
33 changes: 33 additions & 0 deletions rules/src/main/scala/fix/RepeatedRewrite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package fix

import scala.meta.Term
import scalafix.Patch
import scalafix.v1.SyntacticDocument
import scalafix.v1.SyntacticRule
import scala.meta.tokens.Token

class RepeatedRewrite extends SyntacticRule("RepeatedRewrite") {
override def fix(implicit doc: SyntacticDocument): Patch = {
doc.tree.collect { case t @ Term.Repeated(x) =>
val a = t.tokens.dropWhile(!_.is[Token.Colon])
if (a.nonEmpty) {
a.dropWhile(!_.is[Token.Underscore]).drop(1).headOption match {
case Some(Token.Ident("*")) =>
Seq(
if (x.is[Term.ApplyInfix]) {
Patch.addAround(x, "(", ")")
} else {
Patch.empty
},
Patch.removeToken(a.head),
a.find(_.is[Token.Underscore]).map(Patch.removeToken).asPatch,
).asPatch
case _ =>
Patch.empty
}
} else {
Patch.empty
}
}.asPatch
}
}

0 comments on commit 13b9f52

Please sign in to comment.