Skip to content

Instantly share code, notes, and snippets.

@erikwj
Last active November 25, 2017 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikwj/ba420859916a7823b86a85986fcd754f to your computer and use it in GitHub Desktop.
Save erikwj/ba420859916a7823b86a85986fcd754f to your computer and use it in GitHub Desktop.
Convert Case Class into Final Case Class
import scalafix._
import scala.meta._
import scala.meta.contrib._
import scalafix.Patch
import scalafix.SemanticdbIndex
import scalafix.rule.RuleCtx
import scalafix.rule.SemanticRule
case class FinalCaseClass(index: SemanticdbIndex) extends SemanticRule(index, "FinalCaseClass") {
def isNonFinalCaseClass = (clz: Defn.Class) => clz.hasMod(mod"case") && clz.mods.size == 1
override def fix(ctx: RuleCtx): Patch = {
ctx.tree.collect {
case cls @ Defn.Class(_) if isNonFinalCaseClass(cls) => {
val fcc = Defn.Class(Mod.Final() :: cls.mods,cls.name,cls.tparams,cls.ctor,cls.templ)
ctx.replaceTree(cls.tree,fcc.toString())
}
}.asPatch
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment