Created
August 25, 2025 06:55
-
-
Save xuwei-k/d480c4025bf51ed972b22e0bc54946c6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.meta.* | |
import scala.meta.tokens.Token | |
import scalafix.v1.* | |
class ScalikejdbcContextFunction extends SyntacticRule("ScalikejdbcContextFunction") { | |
override def fix(implicit doc: SyntacticDocument): Patch = { | |
doc.tree.collect { | |
case Term.ApplyInfix.After_4_6_0( | |
Term.Name("DB"), | |
readOnly @ Term.Name("localTx"), | |
_, | |
Term.ArgClause( | |
List( | |
p @ Term.PartialFunction( | |
List( | |
c @ Case( | |
Pat.Given(session @ Type.Name("DBSession")), | |
None, | |
_ | |
) | |
) | |
) | |
), | |
None | |
) | |
) => | |
Seq( | |
Patch.replaceTree(readOnly, ".withLocalTxSession"), | |
Patch.removeTokens( | |
session.tokens ++ Seq( | |
c.tokens.find(_.is[Token.KwCase]), | |
c.tokens.find(_.is[Token.KwGiven]), | |
c.tokens.find(_.is[Token.FunctionArrow]), | |
p.tokens.find(_.is[Token.LF]) | |
).flatten | |
) | |
).asPatch | |
}.asPatch | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fix | |
import scala.meta.* | |
import scala.meta.tokens.Token | |
import scalafix.v1.* | |
class ScalikejdbcContextFunction extends SyntacticRule("ScalikejdbcContextFunction") { | |
override def fix(implicit doc: SyntacticDocument): Patch = { | |
doc.tree.collect { | |
case Term.ApplyInfix.After_4_6_0( | |
Term.Name("DB"), | |
readOnly @ Term.Name("readOnly"), | |
_, | |
Term.ArgClause( | |
List( | |
p @ Term.PartialFunction( | |
List( | |
c @ Case( | |
Pat.Given(session @ Type.Name("DBSession")), | |
None, | |
_ | |
) | |
) | |
) | |
), | |
None | |
) | |
) => | |
Seq( | |
Patch.replaceTree(readOnly, ".withReadOnlySession"), | |
Patch.removeTokens( | |
session.tokens ++ Seq( | |
c.tokens.find(_.is[Token.KwCase]), | |
c.tokens.find(_.is[Token.KwGiven]), | |
c.tokens.find(_.is[Token.FunctionArrow]), | |
p.tokens.find(_.is[Token.LF]) | |
).flatten | |
) | |
).asPatch | |
}.asPatch | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment