[New] order
: collapse excess spacing for aesthetically pleasing imports via consolidateIslands
#3129
+2,076
−189
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Depends on #3128
This PR implements in
import/order
: a new option enabling the manipulation/consolidation of newlines around imports.A demo package containing this feature is temporarily available for easy testing:
Collapse excess spacing for aesthetically pleasing imports
This is implemented via
consolidateIslands
. The proposed documentation corresponding to this new feature can be previewed here.Example
Given this code (which could be the output of a previous
--fix
pass):And the following settings, the rule check will pass:
However, when given the following instead, the rule check will fail:
{ "newlines-between": "always-and-inside-groups", + "consolidateIslands": "inside-groups" }
With
--fix
yielding:Note how the intragroup "islands" of grouped single-line imports, as well as multi-line imports, are surrounded by new lines.
Essentially, I was looking for a
newlines-between
-like setting somewhere between"never"
and"always-and-inside-groups"
. I want newlines separatinggroups
/pathGroups
imports from one another (like"always-and-inside-groups"
), newlines separating imports that span multiple lines from other imports (this is the new thing), and any remaining newlines deleted or "consolidated" (like"never"
). The example above demonstrates this use case.Right now, this is achievable with
newlines-between
set to"always-and-inside-groups"
if you add additional newlines around multi-line imports to every file by hand. The goal ofconsolidateIslands
is to alloweslint --fix
to take care of the tedium in a backward-compatible way.There was a slight complication with
consolidateIslands
though: while testing across a few mid-sized repos, I discovered my naive implementation caused a conflict when enabled alongsidesortTypesAmongThemselves: true
,newlines-between: "always-and-inside-groups"
, andnewlines-between-types: "never"
... and then only when a normal import was followed by a multi-line type-only import. This conflict makes sense, sincenewlines-between-types: "never"
wants no newlines ever and demands no newline separate type-only imports from normal imports (since, currently,newlines-between-types
governs that space), yetconsolidateIslands
demands a newline separate all multi-line imports from other imports.To solve this, the current implementation has
newlines-between-types
yield toconsolidateIslands
whenever they conflict. I've also added a test to catch any regressions around this edge case.