From b6f4e910c9b1a089af6b6fe0352eca0762b5cc76 Mon Sep 17 00:00:00 2001 From: Wondermarin <33459274+Wondermarin@users.noreply.github.com> Date: Wed, 13 Sep 2023 12:23:34 +0300 Subject: [PATCH] fix: side-effect import with an internal pattern are defined as internal module in sort-imports rule --- rules/sort-imports.ts | 44 +++++++++++++++++------------------ test/sort-imports.test.ts | 48 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 22 deletions(-) diff --git a/rules/sort-imports.ts b/rules/sort-imports.ts index e60af0f9..90cec3e9 100644 --- a/rules/sort-imports.ts +++ b/rules/sort-imports.ts @@ -222,24 +222,24 @@ export default createEslintRule, MESSAGE_ID>({ if (node.type === 'ImportDeclaration') { setCustomGroups(options['custom-groups'].type, node.source.value) - if (isCoreModule(node.source.value)) { - defineGroup('builtin-type') - } - - if (isInternal(node)) { - defineGroup('internal-type') - } - if (isIndex(node.source.value)) { defineGroup('index-type') } + if (isSibling(node.source.value)) { + defineGroup('sibling-type') + } + if (isParent(node.source.value)) { defineGroup('parent-type') } - if (isSibling(node.source.value)) { - defineGroup('sibling-type') + if (isInternal(node)) { + defineGroup('internal-type') + } + + if (isCoreModule(node.source.value)) { + defineGroup('builtin-type') } } @@ -250,32 +250,32 @@ export default createEslintRule, MESSAGE_ID>({ if (node.type === 'ImportDeclaration') { setCustomGroups(options['custom-groups'].value, node.source.value) - if (isCoreModule(node.source.value)) { - defineGroup('builtin') - } - - if (isInternal(node)) { - defineGroup('internal') + if (isSideEffectImport(node)) { + defineGroup('side-effect') } if (isStyle(node.source.value)) { defineGroup('style') } - if (isSideEffectImport(node)) { - defineGroup('side-effect') - } - if (isIndex(node.source.value)) { defineGroup('index') } + if (isSibling(node.source.value)) { + defineGroup('sibling') + } + if (isParent(node.source.value)) { defineGroup('parent') } - if (isSibling(node.source.value)) { - defineGroup('sibling') + if (isInternal(node)) { + defineGroup('internal') + } + + if (isCoreModule(node.source.value)) { + defineGroup('builtin') } defineGroup('external') diff --git a/test/sort-imports.test.ts b/test/sort-imports.test.ts index 8b3994c1..96983576 100644 --- a/test/sort-imports.test.ts +++ b/test/sort-imports.test.ts @@ -3608,5 +3608,53 @@ describe(RULE_NAME, () => { ], }, ) + + ruleTester.run( + `${RULE_NAME}: define side-effect import with internal pattern as side-effect import`, + rule, + { + valid: [ + { + code: dedent` + import { useClient } from '~/hooks/useClient' + + import '~/css/globals.css' + `, + options: [ + { + groups: ['internal', 'side-effect'], + }, + ], + }, + ], + invalid: [ + { + code: dedent` + import { useClient } from '~/hooks/useClient' + import '~/css/globals.css' + `, + output: dedent` + import { useClient } from '~/hooks/useClient' + + import '~/css/globals.css' + `, + options: [ + { + groups: ['internal', 'side-effect'], + }, + ], + errors: [ + { + messageId: 'missedSpacingBetweenImports', + data: { + left: '~/hooks/useClient', + right: '~/css/globals.css', + }, + }, + ], + }, + ], + }, + ) }) })