Skip to content

Instantly share code, notes, and snippets.

@erikwj
Last active November 14, 2018 16:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikwj/1c576d0f78a095dc7ff2e14ebb71c8a9 to your computer and use it in GitHub Desktop.
Save erikwj/1c576d0f78a095dc7ff2e14ebb71c8a9 to your computer and use it in GitHub Desktop.
Converts Seq to List
import scalafix._
import scalafix.Patch
import scalafix.SemanticdbIndex
import scalafix.rule.RuleCtx
import scalafix.rule.SemanticRule
import scala.meta._
import contrib._
import scalafix.lint.LintMessage
case class SeqToList(index: SemanticdbIndex) extends SemanticRule(index, "SeqToList") {
def isSeq = (n: Term.Name) => n.value == "Seq"
override def fix(ctx: RuleCtx): Patch = {
ctx.tree.collect {
case t @ Term.Name(_) if isSeq(t) =>
ctx.replaceTree (t,"List")
case op @ Term.Name(value) if value == "+:" =>
ctx.replaceTree (op,"::")
case tseq @ Term.Name(value) if value == "toSeq" =>
ctx.replaceTree (tseq,"toList")
case typ @ Type.Name(value) if value == "Seq" =>
ctx.replaceTree (typ,"List")
}
}.asPatch
//override def check(ctx: RuleCtx): Seq[LintMessage] = {
// ctx.tree.collect {
// case t@Term.Name(_) if isSeq(t) =>
// LintMessage(s"we only use List for collections", t.pos, LintCategory.error(s"Do not use Seq"))
// }
//}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment