From 4c1810ff7afa0251e16142586dc97bbdb4eea9c8 Mon Sep 17 00:00:00 2001 From: xuwei-k <6b656e6a69@gmail.com> Date: Wed, 20 Dec 2023 18:18:46 +0900 Subject: [PATCH] enable FileNameConsistent if windows --- .../main/scala/fix/FileNameConsistent.scala | 97 +++++++++---------- tests/src/test/scala/fix/RuleSuite.scala | 1 - 2 files changed, 46 insertions(+), 52 deletions(-) diff --git a/rules/src/main/scala/fix/FileNameConsistent.scala b/rules/src/main/scala/fix/FileNameConsistent.scala index dec8d26..2ab1eb6 100644 --- a/rules/src/main/scala/fix/FileNameConsistent.scala +++ b/rules/src/main/scala/fix/FileNameConsistent.scala @@ -16,64 +16,59 @@ class FileNameConsistent extends SyntacticRule("FileNameConsistent") { override def isLinter = true override def fix(implicit doc: SyntacticDocument): Patch = { - if (scala.util.Properties.isWin) { - // TODO https://github.com/xuwei-k/scalafix-rules/issues/34 - Patch.empty - } else { - val defs = doc.tree.collect { - case x: Defn.Trait if x.isTopLevel => - TemplateDef(x, x.name.value) - case x: Defn.Class if x.isTopLevel => - TemplateDef(x, x.name.value) - case x: Defn.Object if x.isTopLevel => - TemplateDef(x, x.name.value) - } - val packageObjects = doc.tree.collect { case x: Pkg.Object => + val defs = doc.tree.collect { + case x: Defn.Trait if x.isTopLevel => TemplateDef(x, x.name.value) - } - val scalaSourceOpt = PartialFunction.condOpt(doc.input) { - case f: Input.File => - ScalaSource( - fullPath = f.path.toString, - name = f.path.toFile.getName.replace(".scala", "") - ) - case f: Input.VirtualFile => - ScalaSource( - fullPath = f.path, - name = f.path.split('/').lastOption.getOrElse("").replace(".scala", "") - ) - } + case x: Defn.Class if x.isTopLevel => + TemplateDef(x, x.name.value) + case x: Defn.Object if x.isTopLevel => + TemplateDef(x, x.name.value) + } + val packageObjects = doc.tree.collect { case x: Pkg.Object => + TemplateDef(x, x.name.value) + } + val scalaSourceOpt = PartialFunction.condOpt(doc.input) { + case f: Input.File => + ScalaSource( + fullPath = f.path.toString, + name = f.path.toFile.getName.replace(".scala", "") + ) + case f: Input.VirtualFile => + ScalaSource( + fullPath = f.path, + name = f.path.split(java.io.File.separatorChar).lastOption.getOrElse("").replace(".scala", "") + ) + } - val names = defs.map(_.name).distinct - scalaSourceOpt match { - case Some(src) => - if ((src.name == "package") && (packageObjects.size == 1) && names.isEmpty) { + val names = defs.map(_.name).distinct + scalaSourceOpt match { + case Some(src) => + if ((src.name == "package") && (packageObjects.size == 1) && names.isEmpty) { + // correct + // - file name is "package.scala" + // - and only package object + Patch.empty + } else if ((names.size == 1) || (packageObjects.size == 1)) { + if (names.contains(src.name)) { // correct - // - file name is "package.scala" - // - and only package object Patch.empty - } else if ((names.size == 1) || (packageObjects.size == 1)) { - if (names.contains(src.name)) { - // correct - Patch.empty - } else { - Patch.lint( - FileNameConsistentWaring( - names = names, - path = src.fullPath, - position = defs.headOption.getOrElse(packageObjects.head).tree.pos - ) - ) - } } else { - // if there are some toplevel trait, class, object - // TODO - Patch.empty + Patch.lint( + FileNameConsistentWaring( + names = names, + path = src.fullPath, + position = defs.headOption.getOrElse(packageObjects.head).tree.pos + ) + ) } - case _ => - // another input type + } else { + // if there are some toplevel trait, class, object + // TODO Patch.empty - } + } + case _ => + // another input type + Patch.empty } } } diff --git a/tests/src/test/scala/fix/RuleSuite.scala b/tests/src/test/scala/fix/RuleSuite.scala index eedcf50..3f0daa3 100644 --- a/tests/src/test/scala/fix/RuleSuite.scala +++ b/tests/src/test/scala/fix/RuleSuite.scala @@ -6,7 +6,6 @@ import scala.util.Properties class RuleSuite extends AbstractSemanticRuleSuite with AnyFunSuiteLike { private[this] val excludeWindows: Set[String] = Set( - "FileNameConsistentTest", "FlatMapCollectTest", "DuplicateWildcardImportTest", "UnnecessarySortTest",