Skip to content

Instantly share code, notes, and snippets.

@erikwj
Last active November 4, 2017 11:11
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/fc54dcdc93e450ab3ce1d8670c5e3ef6 to your computer and use it in GitHub Desktop.
Save erikwj/fc54dcdc93e450ab3ce1d8670c5e3ef6 to your computer and use it in GitHub Desktop.
Changes a case class Foo(_type: String) into case class Foo(`type`: String)
/**
* Changes a case class Foo(_type: String) into case class Foo(`type`: String)
*/
case class UseBackTicks(index: SemanticdbIndex) extends SemanticRule(index, "UseBackTicks") {
override def fix(ctx: RuleCtx): Patch = {
val reservedWords = Set(
// Scala
"abstract", "case", "catch", "class", "def", "do", "else", "extends", "false", "final", "finally", "for", "forSome", "if", "implicit", "import", "lazy", "match", "new", "null", "object", "override", "package", "private", "protected", "return", "sealed", "super", "this", "throw", "trait", "try", "true", "type", "val", "var", "while", "with", "yield",
// Scala-interop languages keywords
"abstract", "continue", "switch", "assert", "default", "synchronized", "goto", "break", "double", "implements", "byte", "public", "throws", "enum", "instanceof", "transient", "int", "short", "char", "interface", "static", "void", "finally", "long", "strictfp", "volatile", "const", "float", "native")
ctx.tree.collect {
case t @ Term.Name(value) if value.startsWith("_") && reservedWords.contains(value.replaceAll("_","")) =>
ctx.replaceTree (t, s"`${value.replaceAll("_","")}`")
}.asPatch
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment