Created
November 3, 2017 07:42
-
-
Save erikwj/f81c184820b45d91f02f90798fce2bdd to your computer and use it in GitHub Desktop.
Scalafix rule to convert empty case class to object
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
/** | |
* Changes a case class Foo() into case object Foo | |
*/ | |
case class EmptyCaseClassToObject(index: SemanticdbIndex) extends SemanticRule(index, "EmptyCaseClassToObject") { | |
val isEmptyCaseClass = (c: Defn.Class) => c.ctor.paramss.forall(_.isEmpty) && c.mods.nonEmpty | |
override def fix(ctx: RuleCtx): Patch = { | |
ctx.tree.collect { | |
case c @ Defn.Class(_) if isEmptyCaseClass(c) => | |
val co = Defn.Object(c.mods,Term.Name(c.name.value),c.templ) | |
ctx.removeTokens(c.tokens) + ctx.addRight(c ,co.toString()) | |
}.asPatch | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment