-
Notifications
You must be signed in to change notification settings - Fork 22
[Scala 3] Add support for given imports #187
Conversation
@tgodzik, thanks for this! Could you please also add some test cases? |
Codecov Report
@@ Coverage Diff @@
## master #187 +/- ##
==========================================
+ Coverage 91.04% 91.05% +0.01%
==========================================
Files 4 4
Lines 268 302 +34
Branches 10 15 +5
==========================================
+ Hits 244 275 +31
- Misses 24 27 +3
Continue to review full report at Codecov.
|
It's blocked by #179. I didn't have time to update the |
@@ -774,40 +777,55 @@ object OrganizeImports { | |||
List[Importee.Name], | |||
List[Importee.Rename], | |||
List[Importee.Unimport], | |||
List[Importee.Given], | |||
Option[Importee.GivenAll], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you please also update the comment on this method to reflect this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added!
config.coalesceToWildcardImportThreshold | ||
.filter(names.length > _) | ||
.filter(names.length + givens.length > _) | ||
.map(_ => importer.copy(importees = renames ++ unimports :+ Importee.Wildcard())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the optional Importee.GivenAll
instance also exist in the result importer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I handle it now separately with 4 different cases.
I'm not familiar with Scala 3 For example, what should be the result import if we merged the following two? import foo.{aGiven as _, given}
import foo.given TBH, I'm not even sure whether the above two imports are valid, but my intention is:
I think the result should be This example is derived from the following case in Scala 2: import foo.{x => _, _}
import foo._ The above Scala 2 imports should be merged into a single We may want to review all the cases handled in |
So from what I understand there is no way to unimport given instances, however I am not 100% sure. I will check out the situation when explicitly importing a given instead of by type |
I just checked and it seems that we can indeed unimport givens, I will fix it and add some tests as soon as #179 is merged. Or should we merge it beforehand in order to fix the Scala 3 support? |
2a06d9b
to
3ee2587
Compare
@liancheng I added a bunch of tests and added support for given inside I think it works correctly now, but my brain is having some issues figuring all the possible edge cases 😅 I haven't added any support for unused imports yet since there is no option for it in the Scala 3 compiler yet, so I am not sure even if unused given imports will be reported. Let me know what you think! |
3ee2587
to
b370d99
Compare
|
||
import fix.GivenImports._ | ||
import fix.GivenImports.{gamma => _, given Beta, given Zeta, given} | ||
import fix.GivenImports2.{alpha => _, beta => _} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! The actual implementation goes beyond my knowledge, but just as a note: you might want to remove/update https://github.com/liancheng/scalafix-organize-imports/blame/master/README.adoc#L92 since test coverage seems quite decent.
@tgodzik, thanks for the update! The For unused imports for Scala 3, it's OK to leave it out for now as long as:
I will find some time to do a more thorough review, hopefully later this week. |
@tgodzik, we also need to update the |
Sure, I will take a look next week! |
Previously, organize-imports-rule would throw an exception in case of given imports, now it should sort them correctly. Most of the existing rules should apply correctly to imports with givens, with an exception that grouped imports will be separated, so that in case of importing a type needed for given we will not get an error.
b370d99
to
a4dea13
Compare
It seems that we can actually skip I also added a bit in the readme. |
I'm merging this. There are a few minor places that can be cleaned up a little bit, but I can do it in a follow-up PR. Thanks again, @tgodzik! |
Thanks a lot! |
Previously, organize-imports-rule would throw an exception in case of given imports, now it should sort them correctly. Currently they will be sorted separately at the end, but we can add some more options. However I am not sure what will be the best practises here. We could possibly have an option to put them at the end, but I think the current situation is fine for now.
You cannot unimport given imports, so we don't have do do anything similar to wildcard improts when it comes to given wildcard imports.You can actuallyTests are currently blocked on #179 , I only checked it manually.We have tests now! 🎉@bjaglin should we wait for that?
I thought of raising an issue, but turned out faster to fix it 😅 If there are some other plans here, feel free to close the PR