Last active
November 25, 2017 21:10
-
-
Save erikwj/ba420859916a7823b86a85986fcd754f to your computer and use it in GitHub Desktop.
Convert Case Class into Final Case Class
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 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