Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Dec 20, 2023
1 parent 95f421c commit 22cea7a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 57 deletions.
4 changes: 2 additions & 2 deletions input/src/main/scala/fix/UnnecessarySortTest.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package fix

/*
rule = UnnecessarySort
*/
package fix

abstract class UnnecessarySortTest {
def seq: Seq[(Int, Int)]

Expand Down
1 change: 0 additions & 1 deletion output/src/main/scala/fix/UnnecessarySortTest.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fix


abstract class UnnecessarySortTest {
def seq: Seq[(Int, Int)]

Expand Down
5 changes: 2 additions & 3 deletions rules/src/main/scala/fix/DirectoryAndPackageName.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,11 @@ class DirectoryAndPackageName(config: DirectoryAndPackageNameConfig) extends Syn
{
for {
path <- scalaSourceOpt
// TODO Windows
if !scala.util.Properties.isWin
dirOpt = config.baseDirectory.find { dir =>
path.contains(dir)
}.map { dir =>
path.split(dir).last.split('/').init.mkString("/")
val c = java.io.File.separatorChar
path.split(dir).last.split(c).init.mkString(String.valueOf(c))
}
dir <- dirOpt
if packages.nonEmpty // TODO check if empty package
Expand Down
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

0 comments on commit 22cea7a

Please sign in to comment.