Skip to content

Instantly share code, notes, and snippets.

@erikwj
Created November 3, 2017 07:42
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/f81c184820b45d91f02f90798fce2bdd to your computer and use it in GitHub Desktop.
Save erikwj/f81c184820b45d91f02f90798fce2bdd to your computer and use it in GitHub Desktop.
Scalafix rule to convert empty case class to object
/**
* 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