Skip to content

Commit 464fa71

Browse files
committed
feat: [import/order] collapse excess spacing for aesthetically pleasing imports via consolidateIslands
1 parent 8256230 commit 464fa71

File tree

2 files changed

+1031
-7
lines changed

2 files changed

+1031
-7
lines changed

src/rules/order.js

+34-5
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ function removeNewLineAfterImport(context, currentImport, previousImport) {
677677
return undefined;
678678
}
679679

680-
function makeNewlinesBetweenReport(context, imported, newlinesBetweenImports, newlinesBetweenTypeOnlyImports, distinctGroup, isSortingTypesGroup) {
680+
function makeNewlinesBetweenReport(context, imported, newlinesBetweenImports, newlinesBetweenTypeOnlyImports_, distinctGroup, isSortingTypesGroup, isConsolidatingSpaceBetweenImports) {
681681
const getNumberOfEmptyLinesBetween = (currentImport, previousImport) => {
682682
const linesBetweenImports = getSourceCode(context).lines.slice(
683683
previousImport.node.loc.end.line,
@@ -703,12 +703,24 @@ function makeNewlinesBetweenReport(context, imported, newlinesBetweenImports, ne
703703
const isTypeOnlyImport = currentImport.node.importKind === 'type';
704704
const isPreviousImportTypeOnlyImport = previousImport.node.importKind === 'type';
705705

706-
const isNormalImportFollowingTypeOnlyImportAndRelevant =
707-
!isTypeOnlyImport && isPreviousImportTypeOnlyImport && isSortingTypesGroup;
706+
const isNormalImportNextToTypeOnlyImportAndRelevant =
707+
isTypeOnlyImport !== isPreviousImportTypeOnlyImport && isSortingTypesGroup;
708708

709709
const isTypeOnlyImportAndRelevant =
710710
isTypeOnlyImport && isSortingTypesGroup;
711711

712+
// In the special case where newlinesBetweenTypeOnlyImports and
713+
// consolidateIslands want the opposite thing, consolidateIslands wins
714+
const newlinesBetweenTypeOnlyImports =
715+
newlinesBetweenTypeOnlyImports_ === 'never' &&
716+
isConsolidatingSpaceBetweenImports &&
717+
isSortingTypesGroup &&
718+
(isNormalImportNextToTypeOnlyImportAndRelevant ||
719+
previousImport.isMultiline ||
720+
currentImport.isMultiline)
721+
? 'always-and-inside-groups'
722+
: newlinesBetweenTypeOnlyImports_;
723+
712724
const isNotIgnored =
713725
(isTypeOnlyImportAndRelevant &&
714726
newlinesBetweenTypeOnlyImports !== 'ignore') ||
@@ -731,7 +743,7 @@ function makeNewlinesBetweenReport(context, imported, newlinesBetweenImports, ne
731743

732744
const shouldAssertNoNewlineBetweenGroup =
733745
!isSortingTypesGroup ||
734-
!isNormalImportFollowingTypeOnlyImportAndRelevant ||
746+
!isNormalImportNextToTypeOnlyImportAndRelevant ||
735747
newlinesBetweenTypeOnlyImports === 'never';
736748

737749
if (shouldAssertNewlineBetweenGroups) {
@@ -844,6 +856,12 @@ module.exports = {
844856
'never',
845857
],
846858
},
859+
consolidateIslands: {
860+
enum: [
861+
'inside-groups',
862+
'never',
863+
],
864+
},
847865
sortTypesGroup: {
848866
type: 'boolean',
849867
default: false,
@@ -906,6 +924,7 @@ module.exports = {
906924
const newlinesBetweenTypeOnlyImports = options['newlines-between-types'] || newlinesBetweenImports;
907925
const pathGroupsExcludedImportTypes = new Set(options.pathGroupsExcludedImportTypes || ['builtin', 'external', 'object']);
908926
const sortTypesGroup = options.sortTypesGroup;
927+
const consolidateIslands = options.consolidateIslands || 'never';
909928

910929
const named = {
911930
types: 'mixed',
@@ -1168,7 +1187,17 @@ module.exports = {
11681187
'Program:exit'() {
11691188
importMap.forEach((imported) => {
11701189
if (newlinesBetweenImports !== 'ignore' || newlinesBetweenTypeOnlyImports !== 'ignore') {
1171-
makeNewlinesBetweenReport(context, imported, newlinesBetweenImports, newlinesBetweenTypeOnlyImports, distinctGroup, isSortingTypesGroup);
1190+
makeNewlinesBetweenReport(
1191+
context,
1192+
imported,
1193+
newlinesBetweenImports,
1194+
newlinesBetweenTypeOnlyImports,
1195+
distinctGroup,
1196+
isSortingTypesGroup,
1197+
consolidateIslands === 'inside-groups' &&
1198+
(newlinesBetweenImports === 'always-and-inside-groups' ||
1199+
newlinesBetweenTypeOnlyImports === 'always-and-inside-groups')
1200+
);
11721201
}
11731202

11741203
if (alphabetize.order !== 'ignore') {

0 commit comments

Comments
 (0)