Skip to content

Commit

Permalink
InfixRewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Dec 23, 2023
1 parent 5481434 commit 3cd8212
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions rules/src/main/scala/fix/InfixRewrite.scala
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 3cd8212

Please sign in to comment.