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

[Scala 3] Add support for given imports #187

Merged
merged 1 commit into from
Jul 26, 2021
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
3 changes: 1 addition & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ Running the rule on source files compiled with Scala 3 is still experimental.
Known limitations:

. You must use Scalafix 0.9.28 or later
. The <<removeUnused, `removeUnused`>> option must be explicitly set to `false`
. Source files using new syntax introduced in Scala 3 such as https://docs.scala-lang.org/scala3/book/ca-given-imports.html[`given` imports] may not work properly
. The <<removeUnused, `removeUnused`>> option must be explicitly set to `false` - the rule currently doesn't remove unused imports as it's currently not supported by the compiler.
. Usage of http://dotty.epfl.ch/docs/reference/dropped-features/package-objects.html[deprecated package objects] may result in incorrect imports
. The <<groupExplicitlyImportedImplicitsSeparately, groupExplicitlyImportedImplicitsSeparately>> option has no effect

Expand Down
14 changes: 14 additions & 0 deletions input/src/main/scala-3/fix/CoalesceGivenImportees.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
rules = [OrganizeImports]
OrganizeImports {
groupedImports = Keep
coalesceToWildcardImportThreshold = 2
removeUnused = false
}
*/
package fix

import fix.GivenImports.{Alpha, Beta, Zeta}
import fix.GivenImports.{given Alpha, given Beta, given Zeta}

object CoalesceGivenImportees
13 changes: 13 additions & 0 deletions input/src/main/scala-3/fix/DeduplicateGivenImportees.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
*/
package fix

import fix.GivenImports._
import fix.GivenImports.{given Beta, given Alpha}
import fix.GivenImports.given Beta
import fix.GivenImports.given Alpha
import fix.GivenImports.given Alpha

object DeduplicateGivenImportees
12 changes: 12 additions & 0 deletions input/src/main/scala-3/fix/ExpandGiven.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
*/
package fix

import fix.GivenImports.Beta
import fix.GivenImports.Alpha
import fix.GivenImports.{given Beta, given Alpha}
import scala.util.Either

object ExpandGiven
14 changes: 14 additions & 0 deletions input/src/main/scala-3/fix/ExpandUnimportGiven.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
*/
package fix

import fix.GivenImports.Beta
import fix.GivenImports.Alpha
import fix.GivenImports.{given Alpha}
import fix.GivenImports.{alpha => _}
import fix.GivenImports.{beta => _, given}
import scala.util.Either

object ExpandUnimportGiven
19 changes: 19 additions & 0 deletions input/src/main/scala-3/fix/GroupedGivenImportsMergeUnimports.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Merge
*/
package fix

import fix.GivenImports._
import fix.GivenImports.{alpha => _, given}
import fix.GivenImports.{ given Beta }
import fix.GivenImports.{gamma => _, given}
import fix.GivenImports.{ given Zeta }

import fix.GivenImports2.{alpha => _}
import fix.GivenImports2.{beta => _}
import fix.GivenImports2.{ given Gamma }
import fix.GivenImports2.{ given Zeta }

object GroupedGivenImportsMergeUnimports
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = AggressiveMerge
*/
package fix

import fix.MergeImports.Wildcard1._
import fix.MergeImports.Wildcard1.{a => _, _}
import fix.MergeImports.Wildcard1.{b => B}
import fix.MergeImports.Wildcard1.{c => _, _}
import fix.MergeImports.Wildcard1.d

import fix.GivenImports._
import fix.GivenImports.{Alpha, Beta, Zeta}
import fix.GivenImports.{given Alpha, given Beta, given Zeta}
import fix.GivenImports.given

import fix.MergeImports.Wildcard2._
import fix.MergeImports.Wildcard2.{a, b}


object GroupedImportsAggressiveMergeGivenAll
14 changes: 14 additions & 0 deletions input/src/main/scala-3/fix/MergeGiven.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
rules = [OrganizeImports]
OrganizeImports.removeUnused = false
OrganizeImports.groupedImports = Merge
*/
package fix

import fix.GivenImports.Beta
import fix.GivenImports.Alpha
import fix.GivenImports.{given Beta}
import fix.GivenImports.{given Alpha}
import scala.util.Either

object MergeGiven
6 changes: 6 additions & 0 deletions output/src/main/scala-3/fix/CoalesceGivenImportees.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package fix

import fix.GivenImports._
import fix.GivenImports.given

object CoalesceGivenImportees
7 changes: 7 additions & 0 deletions output/src/main/scala-3/fix/DeduplicateGivenImportees.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fix

import fix.GivenImports._
import fix.GivenImports.{ given Alpha }
import fix.GivenImports.{ given Beta }

object DeduplicateGivenImportees
10 changes: 10 additions & 0 deletions output/src/main/scala-3/fix/ExpandGiven.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package fix

import fix.GivenImports.Alpha
import fix.GivenImports.Beta
import fix.GivenImports.{ given Alpha }
import fix.GivenImports.{ given Beta }

import scala.util.Either

object ExpandGiven
11 changes: 11 additions & 0 deletions output/src/main/scala-3/fix/ExpandUnimportGiven.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fix

import fix.GivenImports.Alpha
import fix.GivenImports.Beta
import fix.GivenImports.{given Alpha}
import fix.GivenImports.{alpha => _}
import fix.GivenImports.{beta => _, given}

import scala.util.Either

object ExpandUnimportGiven
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fix

import fix.GivenImports._
import fix.GivenImports.{gamma => _, given Beta, given Zeta, given}
import fix.GivenImports2.{alpha => _, beta => _}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you can see the imports are separated into givens and normal ones aside from the situation, where the unimports were together with given imports. Not sure if we can do better. Maybe we could check if the the types of the givens are not imported in the previous imports and join them in that case?

import fix.GivenImports2.{given Gamma, given Zeta}

object GroupedGivenImportsMergeUnimports
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package fix

import fix.GivenImports._
import fix.GivenImports.given
import fix.MergeImports.Wildcard1._
import fix.MergeImports.Wildcard1.{b => B}
import fix.MergeImports.Wildcard2._


object GroupedImportsAggressiveMergeGivenAll
8 changes: 8 additions & 0 deletions output/src/main/scala-3/fix/MergeGiven.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fix

import fix.GivenImports.{Alpha, Beta}
import fix.GivenImports.{given Alpha, given Beta}

import scala.util.Either

object MergeGiven
Loading