From 62ee3f3c74acb185cf45fa501ffe619466814e34 Mon Sep 17 00:00:00 2001 From: xuwei-k <6b656e6a69@gmail.com> Date: Fri, 23 Aug 2024 07:36:16 +0900 Subject: [PATCH] fix SeparateEachFile --- .../scala/fix/SeparateEachFileWarnTest3.scala | 8 ++++ .../scala/fix/SeparateEachFileRewrite.scala | 6 +++ .../main/scala/fix/SeparateEachFileWarn.scala | 43 ++++++++++++------- 3 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 input/src/main/scala/fix/SeparateEachFileWarnTest3.scala diff --git a/input/src/main/scala/fix/SeparateEachFileWarnTest3.scala b/input/src/main/scala/fix/SeparateEachFileWarnTest3.scala new file mode 100644 index 0000000..3ba4c5f --- /dev/null +++ b/input/src/main/scala/fix/SeparateEachFileWarnTest3.scala @@ -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 diff --git a/rules/src/main/scala/fix/SeparateEachFileRewrite.scala b/rules/src/main/scala/fix/SeparateEachFileRewrite.scala index b151f63..990b524 100644 --- a/rules/src/main/scala/fix/SeparateEachFileRewrite.scala +++ b/rules/src/main/scala/fix/SeparateEachFileRewrite.scala @@ -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) diff --git a/rules/src/main/scala/fix/SeparateEachFileWarn.scala b/rules/src/main/scala/fix/SeparateEachFileWarn.scala index 3806ef9..cdfed34 100644 --- a/rules/src/main/scala/fix/SeparateEachFileWarn.scala +++ b/rules/src/main/scala/fix/SeparateEachFileWarn.scala @@ -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 }