Skip to content

Commit

Permalink
fix SeparateEachFile
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Aug 22, 2024
1 parent ea1f0f9 commit 62ee3f3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
8 changes: 8 additions & 0 deletions input/src/main/scala/fix/SeparateEachFileWarnTest3.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
rule = SeparateEachFileWarn
*/
package fix

sealed trait SeparateEachFileWarnTest3 extends Product
case object SeparateEachFileWarnTest3_A extends SeparateEachFileWarnTest3
case object SeparateEachFileWarnTest3_B extends SeparateEachFileWarnTest3
6 changes: 6 additions & 0 deletions rules/src/main/scala/fix/SeparateEachFileRewrite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class SeparateEachFileRewrite(config: SeparateEachFileConfig) extends SyntacticR
case f: Input.VirtualFile =>
new File(f.path)
}
.filter { _ =>
doc.tree.collect {
case t: Stat.WithMods if t.parent.forall(_.is[Pkg]) && t.mods.exists(_.is[Mod.Sealed]) =>
()
}.isEmpty
}
.foreach { input =>
val topLevelValues = doc.tree.collect {
case t: (Stat.WithTemplate & Member & Stat.WithMods)
Expand Down
43 changes: 27 additions & 16 deletions rules/src/main/scala/fix/SeparateEachFileWarn.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,35 @@ class SeparateEachFileWarn(config: SeparateEachFileConfig) extends SyntacticRule
config.conf.getOrElse("SeparateEachFileWarn")(this.config).map(newConfig => new SeparateEachFileWarn(newConfig))

override def fix(implicit doc: SyntacticDocument): Patch = {
val topLevelValues = doc.tree.collect {
case t: (Stat.WithTemplate & Stat.WithMods & Member)
if t.parent.forall(_.is[Pkg]) && t.templ.inits.isEmpty && !t.is[Defn.Object] =>
t
}
if (
doc.tree.collect {
case t: Stat.WithMods if t.parent.forall(_.is[Pkg]) && t.mods.exists(_.is[Mod.Sealed]) =>
()
}.isEmpty
) {
val topLevelValues = doc.tree.collect {
case t: (Stat.WithTemplate & Stat.WithMods & Member)
if t.parent.forall(_.is[Pkg]) && t.templ.inits.isEmpty && !t.is[Defn.Object] =>
t
}

if ((topLevelValues.lengthCompare(config.limit) >= 0) && topLevelValues.forall(_.mods.forall(!_.is[Mod.Sealed]))) {
Patch.lint(
Diagnostic(
id = "",
message = Seq(
s"too many top level classes. please separate file. ${topLevelValues.size} ",
topLevelValues.map(_.name.value).mkString("[", ", ", "]")
).mkString(""),
position = topLevelValues.head.pos,
severity = config.severity
if (
(topLevelValues.lengthCompare(config.limit) >= 0) && topLevelValues.forall(_.mods.forall(!_.is[Mod.Sealed]))
) {
Patch.lint(
Diagnostic(
id = "",
message = Seq(
s"too many top level classes. please separate file. ${topLevelValues.size} ",
topLevelValues.map(_.name.value).mkString("[", ", ", "]")
).mkString(""),
position = topLevelValues.head.pos,
severity = config.severity
)
)
)
} else {
Patch.empty
}
} else {
Patch.empty
}
Expand Down

0 comments on commit 62ee3f3

Please sign in to comment.