From 6b92ec315352e64ebe8b712b5e266a23f1271f1a Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Thu, 27 Feb 2025 23:11:28 +0530 Subject: [PATCH] refactor: split code for groupImportsByLocality --- .../eslint-plugin/rules/dependency-group.js | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/packages/eslint-plugin/rules/dependency-group.js b/packages/eslint-plugin/rules/dependency-group.js index 87fdab786fe72..ba0b007ae2f2d 100644 --- a/packages/eslint-plugin/rules/dependency-group.js +++ b/packages/eslint-plugin/rules/dependency-group.js @@ -208,6 +208,27 @@ module.exports = { return false; } + /** + * Groups imports by their locality type + * @param {Array<[Node, string]>} candidates + * @return {Record>} Grouped imports + */ + function groupImportsByLocality( candidates ) { + /** @type {Record>} */ + 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. @@ -311,18 +332,6 @@ module.exports = { } ); } - /** @type {Record>} */ - 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 ) { @@ -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`;