Skip to content

Commit

Permalink
NestedClassTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Dec 5, 2023
1 parent 701edea commit a3c2841
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions rules/src/main/scala/fix/NestedClassTrait.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package fix

import scala.meta.Defn
import scala.meta.Mod
import scala.meta.Template
import scala.meta.Tree
import scala.meta.Type
import scalafix.Patch
import scalafix.lint.Diagnostic
import scalafix.lint.LintSeverity
import scalafix.v1.SyntacticDocument
import scalafix.v1.SyntacticRule

object NestedClassTrait {
private object ClassOrTrait {
def unapply(t: Tree): Option[(Type.Name, Template)] = PartialFunction.condOpt(t) {
case x: Defn.Trait if !x.mods.exists(_.is[Mod.Implicit]) =>
(x.name, x.templ)
case x: Defn.Class if !x.mods.exists(_.is[Mod.Implicit]) =>
(x.name, x.templ)
}
}
}

class NestedClassTrait extends SyntacticRule("NestedClassTrait") {
import NestedClassTrait.ClassOrTrait
override def fix(implicit doc: SyntacticDocument): Patch = {
doc.tree.collect { case ClassOrTrait(_, template) =>
template.stats.collect { case ClassOrTrait(typeName, _) =>
Patch.lint(
Diagnostic(
id = "",
message = "don't define nested class or trait",
position = typeName.pos,
severity = LintSeverity.Warning
)
)
}.asPatch
}.asPatch
}
}

0 comments on commit a3c2841

Please sign in to comment.