-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[New] order
: collapse excess spacing for aesthetically pleasing imports via consolidateIslands
#3129
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #3129 +/- ##
==========================================
+ Coverage 95.62% 95.66% +0.03%
==========================================
Files 83 83
Lines 3636 3689 +53
Branches 1284 1332 +48
==========================================
+ Hits 3477 3529 +52
- Misses 159 160 +1 ☔ View full report in Codecov by Sentry. |
02507ca
to
464fa71
Compare
16bdbf7
to
90cfd85
Compare
@@ -4228,6 +4228,172 @@ context('TypeScript', function () { | |||
}, | |||
], | |||
}), | |||
// Ensure consolidateIslands: 'inside-groups', newlines-between: 'never', and newlines-between-types: 'always-and-inside-groups' do not fight for dominance |
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.
These two tests also cover the uncovered line codecov reported from the previous commit
3057807
to
fc949cd
Compare
…oup sorting of type-only imports Closes import-js#2912 Closes import-js#2347 Closes import-js#2441 Subsumes import-js#2615
fc949cd
to
5afa43a
Compare
…tTypesGroup" in rule schema
…ng imports via `consolidateIslands`
…rent eslint versions
…wlines-between-types when in conflict
…s-between" and "consolidateIslands" in rule schema
5afa43a
to
3c4c3f2
Compare
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.