diff --git a/rules/sort-imports.ts b/rules/sort-imports.ts index 4faa56bf..d906a4b5 100644 --- a/rules/sort-imports.ts +++ b/rules/sort-imports.ts @@ -217,7 +217,10 @@ export default createEslintRule, MESSAGE_ID>({ let nodes: SortingNode[] = [] let isSideEffectImport = (node: TSESTree.Node) => - node.type === 'ImportDeclaration' && node.specifiers.length === 0 + node.type === 'ImportDeclaration' && + node.specifiers.length === 0 && + /* Avoid matching on named imports without specifiers */ + !/}\s*from\s+/.test(context.sourceCode.getText(node)) let computeGroup = (node: ModuleDeclaration): Group => { let isStyle = (value: string) => diff --git a/test/sort-imports.test.ts b/test/sort-imports.test.ts index 69581fdf..6b7e924d 100644 --- a/test/sort-imports.test.ts +++ b/test/sort-imports.test.ts @@ -3984,5 +3984,28 @@ describe(RULE_NAME, () => { ], }, ) + + ruleTester.run( + `${RULE_NAME}: does not consider empty named imports to be side-effects`, + rule, + { + valid: [ + { + code: dedent` + import {} from 'node:os' + import { hinaAmano } from 'weathering-with-you' + import 'node:os' + `, + options: [ + { + 'newlines-between': NewlinesBetweenValue.never, + groups: ['builtin', 'external', 'side-effect'], + }, + ], + }, + ], + invalid: [], + }, + ) }) })