diff --git a/rules/src/main/scala/fix/DirectoryAndPackageName.scala b/rules/src/main/scala/fix/DirectoryAndPackageName.scala index 6a2a76a..1fa03f7 100644 --- a/rules/src/main/scala/fix/DirectoryAndPackageName.scala +++ b/rules/src/main/scala/fix/DirectoryAndPackageName.scala @@ -152,12 +152,6 @@ class DirectoryAndPackageName(config: DirectoryAndPackageNameConfig) extends Syn { for { path <- scalaSourceOpt - dirOpt = config.baseDirectory.find { dir => - path.contains(dir) - }.map { dir => - path.split(dir).last.split('/').init.mkString("/") - } - dir <- dirOpt if packages.nonEmpty // TODO check if empty package packageName = { val x = packages @@ -166,20 +160,26 @@ class DirectoryAndPackageName(config: DirectoryAndPackageNameConfig) extends Syn if (keywords(p)) { p.replace("`", "") } else p ) - .mkString("/") packageObjectOpt match { case Some(value) => - x + "/" + value.name.value + x :+ value.name.value case None => x } } - if packageName != dir + if config.baseDirectory.find { dir => + path.contains(dir) + }.exists { fullDir => + val splitedDir = path.split(fullDir).last.split('/').init.toList + packageName != splitedDir + } || { + path.split('/').init.toList.takeRight(packageName.size) != packageName + } } yield { Patch.lint( Diagnostic( id = "", - message = s"inconsistent package and directory\n${path}\n${packageName}", + message = s"inconsistent package and directory\n${path}\n${packageName.mkString("/")}", position = packages.last.pos, severity = config.severity ) diff --git a/sbt-test/test/DirectoryAndPackageName/.scalafix.conf b/sbt-test/DirectoryAndPackageName/test-1/.scalafix.conf similarity index 100% rename from sbt-test/test/DirectoryAndPackageName/.scalafix.conf rename to sbt-test/DirectoryAndPackageName/test-1/.scalafix.conf diff --git a/sbt-test/test/DirectoryAndPackageName/build.sbt b/sbt-test/DirectoryAndPackageName/test-1/build.sbt similarity index 100% rename from sbt-test/test/DirectoryAndPackageName/build.sbt rename to sbt-test/DirectoryAndPackageName/test-1/build.sbt diff --git a/sbt-test/test/DirectoryAndPackageName/project/plugins.sbt b/sbt-test/DirectoryAndPackageName/test-1/project/plugins.sbt similarity index 100% rename from sbt-test/test/DirectoryAndPackageName/project/plugins.sbt rename to sbt-test/DirectoryAndPackageName/test-1/project/plugins.sbt diff --git a/sbt-test/test/DirectoryAndPackageName/src/main/scala/a/X.scala b/sbt-test/DirectoryAndPackageName/test-1/src/main/scala/a/X.scala similarity index 100% rename from sbt-test/test/DirectoryAndPackageName/src/main/scala/a/X.scala rename to sbt-test/DirectoryAndPackageName/test-1/src/main/scala/a/X.scala diff --git a/sbt-test/test/DirectoryAndPackageName/test b/sbt-test/DirectoryAndPackageName/test-1/test similarity index 100% rename from sbt-test/test/DirectoryAndPackageName/test rename to sbt-test/DirectoryAndPackageName/test-1/test diff --git a/sbt-test/DirectoryAndPackageName/test-2/.scalafix.conf b/sbt-test/DirectoryAndPackageName/test-2/.scalafix.conf new file mode 100644 index 0000000..5a2fe83 --- /dev/null +++ b/sbt-test/DirectoryAndPackageName/test-2/.scalafix.conf @@ -0,0 +1,5 @@ +rules = [ + DirectoryAndPackageName +] + +DirectoryAndPackageName.severity = error diff --git a/sbt-test/DirectoryAndPackageName/test-2/build.sbt b/sbt-test/DirectoryAndPackageName/test-2/build.sbt new file mode 100644 index 0000000..624d9c3 --- /dev/null +++ b/sbt-test/DirectoryAndPackageName/test-2/build.sbt @@ -0,0 +1,3 @@ +ThisBuild / scalafixDependencies += "com.github.xuwei-k" %% "scalafix-rules" % sys.props("scalafix-rules.version") + +Compile / unmanagedSourceDirectories += baseDirectory.value / "my-src-dir" diff --git a/sbt-test/DirectoryAndPackageName/test-2/my-src-dir/a/X.scala b/sbt-test/DirectoryAndPackageName/test-2/my-src-dir/a/X.scala new file mode 100644 index 0000000..6193582 --- /dev/null +++ b/sbt-test/DirectoryAndPackageName/test-2/my-src-dir/a/X.scala @@ -0,0 +1,3 @@ +package b + +class X diff --git a/sbt-test/DirectoryAndPackageName/test-2/project/plugins.sbt b/sbt-test/DirectoryAndPackageName/test-2/project/plugins.sbt new file mode 100644 index 0000000..45b3184 --- /dev/null +++ b/sbt-test/DirectoryAndPackageName/test-2/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % sys.props("scalafix.version")) diff --git a/sbt-test/DirectoryAndPackageName/test-2/test b/sbt-test/DirectoryAndPackageName/test-2/test new file mode 100644 index 0000000..e9f1cce --- /dev/null +++ b/sbt-test/DirectoryAndPackageName/test-2/test @@ -0,0 +1,2 @@ +> Test/compile +-> scalafixAll