From 3cd8212a9b36cbee27894448c830e4a39dbe7672 Mon Sep 17 00:00:00 2001 From: xuwei-k <6b656e6a69@gmail.com> Date: Sat, 23 Dec 2023 09:53:14 +0900 Subject: [PATCH] InfixRewrite --- rules/src/main/scala/fix/InfixRewrite.scala | 39 +++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 rules/src/main/scala/fix/InfixRewrite.scala diff --git a/rules/src/main/scala/fix/InfixRewrite.scala b/rules/src/main/scala/fix/InfixRewrite.scala new file mode 100644 index 0000000..06a6041 --- /dev/null +++ b/rules/src/main/scala/fix/InfixRewrite.scala @@ -0,0 +1,39 @@ +package fix + +import scala.meta.Term +import scala.meta.Type +import scalafix.Patch +import scalafix.v1.SyntacticDocument +import scalafix.v1.SyntacticRule +import scala.meta.tokens.Token + +class InfixRewrite extends SyntacticRule("InfixRewrite") { + private val alphaChars: Set[Char] = (('a' to 'z') ++ ('A' to 'Z')).toSet + private val exclude: Set[String] = Set( + "eq" + ) + override def fix(implicit doc: SyntacticDocument): Patch = { + doc.tree.collect { + case t @ Term.ApplyInfix.After_4_6_0( + obj, + method: Term.Name, + Type.ArgClause(Nil), + Term.ArgClause( + arg :: Nil, + None + ) + ) if alphaChars(method.value.head) && !exclude(method.value) => + Seq( + Patch.removeTokens( + t.tokens.dropWhile(x => x.end <= obj.tokens.last.end).takeWhile(_.is[Token.Whitespace]) + ), + Patch.removeTokens( + t.tokens.dropWhile(x => x.end <= method.tokens.last.end).takeWhile(_.is[Token.Whitespace]) + ), + Patch.addRight(obj, "."), + Patch.addRight(method, "("), + Patch.addRight(arg, ")"), + ).asPatch + }.asPatch + } +}