-
Notifications
You must be signed in to change notification settings - Fork 255
Order conditionally-compiled imports #1077
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Order conditionally-compiled imports #1077
Conversation
|
Some questions to consider:
|
|
Thanks for looking into this!
We have to avoid changing the default behavior for anyone who's using the existing default in something like a CI workflow (we've forgotten that before, and it causes some annoyance). One day I want to refactor the settings to make it easier to add new configuration options to existing rules. For now, you could introduce a small
It would be good to have a nested case as well. For example, import A
#if FOO
import D
#if BAR
import F
import E
#else
import H
import G
#endif
import C
#endif
import BI would expect the output to be: import A
import B
#if FOO
import C
import D
#if BAR
import E
import F
#else
import G
import H
#endif
#endif |
|
@allevato I've added an option for enabling ordering conditional imports and the nested conditional imports test. I found some issues with the original implementation related to leading trivia and file header which I fixed and added some test cases for. I also updated the documentation. Please recheck. 🙏🏼 |
| if let firstLine = fileHeader.first, firstLine.type == .blankLine { | ||
| fileHeader.removeFirst() | ||
| } |
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.
This handles the case in testPreservesHeaderCommentInConditionalCompilationBlock.
Previously, the newline separating the comment and #if FOO was printed as is as part of fileHeader resulting in an extra newline separating the comment and #if FOO.
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.
This is a breaking change for regular file headers if for some reason file header comments have a newline before them. I don't think this is a behavior we need to maintain. What do you think?
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.
It's probably not a big deal, but can we avoid the break by passing into formatAndAppend whether we're at the top-level of the file or we're inside an IfConfigDecl?
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.
@allevato Actually looking at this again, the pretty-printer will always remove that line anyway (and looks like that isn't configurable), so there should be no breaking changes here.
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.
Even better!
|
Hi @allevato! Have you checked the last changes here? 🙏🏼 |
|
LGTM, but now that we have #1090 in flight, let's wait for that to land, and that will be a good test to make sure there aren't any unintended side effects across some real code before we merge. |
36173f4 to
6f02f9c
Compare
|
@allevato Tests and real-world project compatibility tests are now green. |
allevato
left a comment
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.
Thanks for adding this!
Recursively sort conditionally-compiled imports the same way other imports are ordered.
Conditionally-compiled imports are always placed after normal non-conditional imports as before, they're only sorted relative to the same
#ifblock.Resolves #1027.