Skip to content

Commit

Permalink
feat: [import/order] collapse excess spacing for aesthetically pleasi…
Browse files Browse the repository at this point in the history
…ng imports via `consolidateIslands`
  • Loading branch information
Xunnamius committed Dec 31, 2024
1 parent 8256230 commit 464fa71
Show file tree
Hide file tree
Showing 2 changed files with 1,031 additions and 7 deletions.
39 changes: 34 additions & 5 deletions src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ function removeNewLineAfterImport(context, currentImport, previousImport) {
return undefined;
}

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

const isNormalImportFollowingTypeOnlyImportAndRelevant =
!isTypeOnlyImport && isPreviousImportTypeOnlyImport && isSortingTypesGroup;
const isNormalImportNextToTypeOnlyImportAndRelevant =
isTypeOnlyImport !== isPreviousImportTypeOnlyImport && isSortingTypesGroup;

const isTypeOnlyImportAndRelevant =
isTypeOnlyImport && isSortingTypesGroup;

// In the special case where newlinesBetweenTypeOnlyImports and
// consolidateIslands want the opposite thing, consolidateIslands wins
const newlinesBetweenTypeOnlyImports =
newlinesBetweenTypeOnlyImports_ === 'never' &&
isConsolidatingSpaceBetweenImports &&
isSortingTypesGroup &&
(isNormalImportNextToTypeOnlyImportAndRelevant ||
previousImport.isMultiline ||
currentImport.isMultiline)
? 'always-and-inside-groups'
: newlinesBetweenTypeOnlyImports_;

const isNotIgnored =
(isTypeOnlyImportAndRelevant &&
newlinesBetweenTypeOnlyImports !== 'ignore') ||
Expand All @@ -731,7 +743,7 @@ function makeNewlinesBetweenReport(context, imported, newlinesBetweenImports, ne

const shouldAssertNoNewlineBetweenGroup =
!isSortingTypesGroup ||
!isNormalImportFollowingTypeOnlyImportAndRelevant ||
!isNormalImportNextToTypeOnlyImportAndRelevant ||
newlinesBetweenTypeOnlyImports === 'never';

if (shouldAssertNewlineBetweenGroups) {
Expand Down Expand Up @@ -844,6 +856,12 @@ module.exports = {
'never',
],
},
consolidateIslands: {
enum: [
'inside-groups',
'never',
],
},
sortTypesGroup: {
type: 'boolean',
default: false,
Expand Down Expand Up @@ -906,6 +924,7 @@ module.exports = {
const newlinesBetweenTypeOnlyImports = options['newlines-between-types'] || newlinesBetweenImports;
const pathGroupsExcludedImportTypes = new Set(options.pathGroupsExcludedImportTypes || ['builtin', 'external', 'object']);
const sortTypesGroup = options.sortTypesGroup;
const consolidateIslands = options.consolidateIslands || 'never';

const named = {
types: 'mixed',
Expand Down Expand Up @@ -1168,7 +1187,17 @@ module.exports = {
'Program:exit'() {
importMap.forEach((imported) => {
if (newlinesBetweenImports !== 'ignore' || newlinesBetweenTypeOnlyImports !== 'ignore') {
makeNewlinesBetweenReport(context, imported, newlinesBetweenImports, newlinesBetweenTypeOnlyImports, distinctGroup, isSortingTypesGroup);
makeNewlinesBetweenReport(
context,
imported,
newlinesBetweenImports,
newlinesBetweenTypeOnlyImports,
distinctGroup,
isSortingTypesGroup,
consolidateIslands === 'inside-groups' &&
(newlinesBetweenImports === 'always-and-inside-groups' ||
newlinesBetweenTypeOnlyImports === 'always-and-inside-groups')
);
}

if (alphabetize.order !== 'ignore') {
Expand Down
Loading

0 comments on commit 464fa71

Please sign in to comment.