Skip to content

Commit

Permalink
refactor: split code for groupImportsByLocality
Browse files Browse the repository at this point in the history
  • Loading branch information
im3dabasia committed Feb 28, 2025
1 parent 721698c commit 6b92ec3
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions packages/eslint-plugin/rules/dependency-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,27 @@ module.exports = {
return false;
}

/**
* Groups imports by their locality type
* @param {Array<[Node, string]>} candidates
* @return {Record<WPPackageLocality, Array<[Node, string]>>} Grouped imports
*/
function groupImportsByLocality( candidates ) {
/** @type {Record<WPPackageLocality, Array<[Node, string]>>} */
const groups = {
WordPress: [],
External: [],
Internal: [],
};

for ( const [ importNode, source ] of candidates ) {
const locality = getPackageLocality( source );
groups[ locality ].push( [ importNode, source ] );
}

return groups;
}

return {
/**
* @param {import('estree').Program} node Program node.
Expand Down Expand Up @@ -311,18 +332,6 @@ module.exports = {
} );
}

/** @type {Record<WPPackageLocality, Array<[Node, string]>>} */
const groups = {
WordPress: [],
External: [],
Internal: [],
};

for ( const [ importNode, source ] of candidates ) {
const locality = getPackageLocality( source );
groups[ locality ].push( [ importNode, source ] );
}

const needsReordering = checkNeedsReordering( candidates );

if ( needsReordering && candidates.length > 0 ) {
Expand Down Expand Up @@ -350,6 +359,8 @@ module.exports = {
'Internal',
];

const groups = groupImportsByLocality( candidates );

for ( const locality of localities ) {
if ( groups[ locality ].length > 0 ) {
newText += `/**\n * ${ locality } dependencies\n */\n`;
Expand Down

0 comments on commit 6b92ec3

Please sign in to comment.