Skip to content
This repository has been archived by the owner on Jul 14, 2023. It is now read-only.

Commit

Permalink
Scala 3: RemoveUnused cannot work as compiler support is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
github-brice-jaglin committed May 15, 2021
1 parent ed1daa1 commit 795ff11
Show file tree
Hide file tree
Showing 46 changed files with 79 additions and 13 deletions.
1 change: 1 addition & 0 deletions input/src/main/scala/ExpandRelativeRootPackage.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports {
expandRelative = true
groupedImports = Explode
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/OrganizeImportsRootPackage.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groups = ["re:javax?\\.", "*", "scala."]
*/
import java.time.Clock
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/AlreadyOrganized.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Merge
*/
package fix
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/BlankLinesManual.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports {
blankLines = Manual
groups = [
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/CoalesceImportees.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports {
groupedImports = Keep
coalesceToWildcardImportThreshold = 3
Expand Down
5 changes: 4 additions & 1 deletion input/src/main/scala/fix/CurlyBracedSingleImportee.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* rules = [OrganizeImports] */
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
*/
package fix

import scala.collection.{Map}
Expand Down
5 changes: 4 additions & 1 deletion input/src/main/scala/fix/DeduplicateImportees.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* rules = [OrganizeImports] */
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
*/
package fix

import scala.collection.immutable.Vector
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/ExpandRelative.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.expandRelative = true
*/
package fix
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/ExpandRelativeMultiGroups.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.expandRelative = true
*/
package fix
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/ExpandRelativeQuotedIdent.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.expandRelative = true
*/
package fix
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/ExpandRelativeRootPackage.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.expandRelative = true
*/
package fix
Expand Down
5 changes: 4 additions & 1 deletion input/src/main/scala/fix/GlobalImportsOnly.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* rules = [OrganizeImports] */
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
*/
package fix

import scala.collection.mutable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = AggressiveMerge
*/
package fix
Expand Down
5 changes: 4 additions & 1 deletion input/src/main/scala/fix/GroupedImportsExplode.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* rules = [OrganizeImports] */
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
*/
package fix

import scala.collection.mutable.{ArrayBuffer, Buffer, StringBuilder}
Expand Down
5 changes: 4 additions & 1 deletion input/src/main/scala/fix/GroupedImportsExplodeMixed.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* rules = [OrganizeImports] */
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
*/
package fix

import scala.collection.immutable._
Expand Down
5 changes: 4 additions & 1 deletion input/src/main/scala/fix/GroupedImportsExplodeUnimport.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* rules = [OrganizeImports] */
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
*/
package fix

import scala.collection.{Seq => _, _}
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/GroupedImportsKeep.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Keep
*/
package fix
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/GroupedImportsMerge.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Merge
*/
package fix
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/GroupedImportsMergeDedup.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Merge
*/
package fix
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/GroupedImportsMergeRenames.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Merge
*/
package fix
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Merge
*/
package fix
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/GroupedImportsMergeWildcard.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Merge
*/
package fix
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/Groups.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groups = ["re:javax?\\.", "*", "scala."]
*/
package fix
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/GroupsLongestMatch.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groups = ["re:javax?\\.", "scala.", "scala.util.", "*"]
*/
package fix
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Keep
*/
package fix
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/ImportsOrderKeep.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports {
groupedImports = Keep
importSelectorsOrder = Keep
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/ImportsOrderSymbolsFirst.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports {
groupedImports = Keep
importSelectorsOrder = Keep
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports {
groupedImports = Keep
importsOrder = SymbolsFirst
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/Inheritance.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.expandRelative = true
*/
package fix
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Merge
*/

Expand Down
5 changes: 4 additions & 1 deletion input/src/main/scala/fix/NoImports.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* rules = OrganizeImports */
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
*/
package fix

object NoImports
1 change: 1 addition & 0 deletions input/src/main/scala/fix/PresetDefault.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports {
preset = DEFAULT
groupedImports = Merge
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/PresetIntelliJ_2020_3.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports {
preset = INTELLIJ_2020_3
groupedImports = Explode
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/RelativeImports.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groups = ["scala.", "*"]
*/
package fix
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/SortImportSelectorsAscii.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Keep
*/
package fix
Expand Down
3 changes: 2 additions & 1 deletion input/src/main/scala/fix/SortImportSelectorsKeep.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = OrganizeImports
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports {
importSelectorsOrder = Keep
groupedImports = Keep
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports {
importSelectorsOrder = SymbolsFirst
groupedImports = Keep
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/Suppression.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groups = ["re:javax?\\.", "*", "scala."]
*/
package fix
Expand Down
1 change: 1 addition & 0 deletions input/src/main/scala/fix/nested/NestedPackage.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groups = ["re:javax?\\.", "*", "scala."]
*/
package fix
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* rules = [OrganizeImports] */
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
*/
package fix {
package nested {
import java.time.Clock
Expand Down
18 changes: 14 additions & 4 deletions rules/src/main/scala/fix/OrganizeImports.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class OrganizeImports(config: OrganizeImportsConfig) extends SemanticRule("Organ
config.conf
.getOrElse("OrganizeImports")(OrganizeImportsConfig())
.andThen(patchPreset(_, config.conf))
.andThen(checkScalacOptions(_, config.scalacOptions))
.andThen(checkScalacOptions(_, config.scalacOptions, config.scalaVersion))

override def fix(implicit doc: SemanticDocument): Patch = {
unusedImporteePositions ++= doc.diagnostics.collect {
Expand Down Expand Up @@ -614,9 +614,12 @@ object OrganizeImports {

private def checkScalacOptions(
conf: OrganizeImportsConfig,
scalacOptions: List[String]
scalacOptions: List[String],
scalaVersion: String
): Configured[Rule] = {
val hasWarnUnused = {
val hasCompilerSupport = scalaVersion.startsWith("2")

val hasWarnUnused = hasCompilerSupport && {
val warnUnusedPrefix = Set("-Wunused", "-Ywarn-unused")
val warnUnusedString = Set("-Xlint", "-Xlint:unused")
scalacOptions exists { option =>
Expand All @@ -626,13 +629,20 @@ object OrganizeImports {

if (!conf.removeUnused || hasWarnUnused)
Configured.ok(new OrganizeImports(conf))
else
else if (hasCompilerSupport)
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)."
)
else
Configured.error(
"\"OrganizeImports.removeUnused\" is not supported on Scala 3 as the compiler is"
+ " not providing enough information. Run the rule with"
+ " \"OrganizeImports.removeUnused\" set to false to organize imports while keeping"
+ " potentially unused imports."
)
}

private def buildImportMatchers(config: OrganizeImportsConfig): Seq[ImportMatcher] = {
Expand Down

0 comments on commit 795ff11

Please sign in to comment.