From fc782218baba7e1cea5b7b658baaecedfa39989c Mon Sep 17 00:00:00 2001 From: xuwei-k <6b656e6a69@gmail.com> Date: Sun, 24 Dec 2023 09:02:13 +0900 Subject: [PATCH] RepeatedRewrite --- .../src/main/scala/fix/RepeatedRewrite.scala | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 rules/src/main/scala/fix/RepeatedRewrite.scala diff --git a/rules/src/main/scala/fix/RepeatedRewrite.scala b/rules/src/main/scala/fix/RepeatedRewrite.scala new file mode 100644 index 0000000..26d9282 --- /dev/null +++ b/rules/src/main/scala/fix/RepeatedRewrite.scala @@ -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 + } +}