Skip to content

Commit

Permalink
Test OrganizeImports.removeUnused in Scala3
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishiking committed Oct 26, 2023
1 parent 11fc77d commit b399b5a
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 12 deletions.
3 changes: 1 addition & 2 deletions project/ScalafixBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys {
scalaVersion.value.startsWith("2.12")
}
lazy val warnUnusedImports = Def.setting {
if (isScala3.value) Nil
else if (isScala213.value) Seq("-Wunused:imports")
if (isScala213.value || isScala3.value) Seq("-Wunused:imports")
else Seq("-Ywarn-unused-import")
}
lazy val warnUnused = Def.setting {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ class OrganizeImports(config: OrganizeImportsConfig)
// We could adjust the behavior of `positionOf` based on Scala version,
// but this implementation just checking the unusedImporteePosition includes the importee pos, for simplicity.
val pos = positionOf(importee)
unusedImporteePositions.exists(unused => unused.start <= pos.start && pos.end <= unused.end)
unusedImporteePositions.exists(unused =>
unused.start <= pos.start && pos.end <= unused.end
)
}

private def organizeGlobalImports(
Expand Down Expand Up @@ -720,11 +722,7 @@ object OrganizeImports {
scalaVersion: String
): Configured[Rule] = {
ScalaVersion.from(scalaVersion).map { v =>
v.isScala2 || (
v.isScala3 &&
v.minor.getOrElse(0) >= 3 &&
v.patch.getOrElse(0) >= 1
)
v.isScala2 || (v.isScala3 && v.minor.getOrElse(0) >= 4) // >=3.4.0
} match {
case Failure(exception) => Configured.error(exception.getMessage())
case Success(hasCompilerSupport) =>
Expand All @@ -742,17 +740,17 @@ object OrganizeImports {
Configured.error(
"The Scala compiler option \"-Ywarn-unused\" is required to use OrganizeImports with"
+ " \"OrganizeImports.removeUnused\" set to true. To fix this problem, update your"
+ " build to use at least one Scala compiler option like -Ywarn-unused-import (2.11"
+ " only), -Ywarn-unused, -Xlint:unused (2.12.2 or above) or -Wunused (2.13 only)."
+ " build to use at least one Scala compiler options like `-Ywarn-unused`(2.12.x) or"
+ " `-Wunused` (2.13.x or >=3.4.0)"
)
else
Configured.error(
"\"OrganizeImports.removeUnused\"" + s"is not supported on $scalaVersion as the compiler is"
+ " not providing enough information. Please upgrade Scala 3 version to 3.3.1 or greater."
+ " not providing enough information. Please upgrade Scala 3 version to 3.4.0 or greater."
+ " Otherwise, run the rule with \"OrganizeImports.removeUnused\" set to false"
+ " to organize imports while keeping potentially unused imports."
)
}
}
}

private def buildImportMatchers(
Expand Down

0 comments on commit b399b5a

Please sign in to comment.