Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable FileNameConsistent if windows #136

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 46 additions & 51 deletions rules/src/main/scala/fix/FileNameConsistent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
1 change: 0 additions & 1 deletion tests/src/test/scala/fix/RuleSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down