11import type { TSESTree } from '@typescript-eslint/types'
22import type { TSESLint } from '@typescript-eslint/utils'
33
4- import type { SortImportsSortingNode , Options } from './sort-imports/types'
4+ import type {
5+ SortImportsSortingNode ,
6+ Options ,
7+ Group ,
8+ } from './sort-imports/types'
9+ import type { DeprecatedCustomGroupsOption } from '../types/common-options'
510
611import {
712 partitionByCommentJsonSchema ,
@@ -31,10 +36,10 @@ import { sortNodesByGroups } from '../utils/sort-nodes-by-groups'
3136import { createEslintRule } from '../utils/create-eslint-rule'
3237import { reportAllErrors } from '../utils/report-all-errors'
3338import { shouldPartition } from '../utils/should-partition'
39+ import { computeGroup } from '../utils/compute-group'
3440import { rangeToDiff } from '../utils/range-to-diff'
3541import { getSettings } from '../utils/get-settings'
3642import { isSortable } from '../utils/is-sortable'
37- import { useGroups } from '../utils/use-groups'
3843import { complete } from '../utils/complete'
3944
4045export type MESSAGE_ID =
@@ -133,8 +138,6 @@ export default createEslintRule<Options, MESSAGE_ID>({
133138 | TSESTree . VariableDeclaration
134139 | TSESTree . ImportDeclaration ,
135140 ) : void => {
136- let { setCustomGroups, defineGroup, getGroup } = useGroups ( options )
137-
138141 let name = getNodeName ( {
139142 sourceCode,
140143 node,
@@ -147,16 +150,30 @@ export default createEslintRule<Options, MESSAGE_ID>({
147150 name,
148151 } )
149152
153+ let predefinedGroups : Group [ ] = [ ]
154+ let group : Group | null = null
155+
150156 if ( node . type !== 'VariableDeclaration' && node . importKind === 'type' ) {
151157 if ( node . type === 'ImportDeclaration' ) {
152- setCustomGroups ( options . customGroups . type , name )
158+ group = computeGroupExceptUnknown ( {
159+ customGroups : options . customGroups . type ,
160+ predefinedGroups : [ ] ,
161+ options,
162+ name,
163+ } )
153164
154- for ( let group of commonPredefinedGroups ) {
155- defineGroup ( `${ group } -type` )
165+ for ( let predefinedGroup of commonPredefinedGroups ) {
166+ predefinedGroups . push ( `${ predefinedGroup } -type` )
156167 }
157168 }
158169
159- defineGroup ( 'type' )
170+ predefinedGroups . push ( 'type' )
171+
172+ group ??= computeGroupExceptUnknown ( {
173+ predefinedGroups,
174+ options,
175+ name,
176+ } )
160177 }
161178
162179 let isSideEffect = isSideEffectImport ( { sourceCode, node } )
@@ -168,25 +185,37 @@ export default createEslintRule<Options, MESSAGE_ID>({
168185 let isStyleValue = isStyle ( name )
169186 isStyleSideEffect = isSideEffect && isStyleValue
170187
171- setCustomGroups ( options . customGroups . value , name )
188+ group ??= computeGroupExceptUnknown ( {
189+ customGroups : options . customGroups . value ,
190+ predefinedGroups : [ ] ,
191+ options,
192+ name,
193+ } )
172194
173195 if ( isStyleSideEffect ) {
174- defineGroup ( 'side-effect-style' )
196+ predefinedGroups . push ( 'side-effect-style' )
175197 }
176198
177199 if ( isSideEffect ) {
178- defineGroup ( 'side-effect' )
200+ predefinedGroups . push ( 'side-effect' )
179201 }
180202
181203 if ( isStyleValue ) {
182- defineGroup ( 'style' )
204+ predefinedGroups . push ( 'style' )
183205 }
184206
185- for ( let group of commonPredefinedGroups ) {
186- defineGroup ( group )
207+ for ( let predefinedGroup of commonPredefinedGroups ) {
208+ predefinedGroups . push ( predefinedGroup )
187209 }
188210 }
189211
212+ group ??=
213+ computeGroupExceptUnknown ( {
214+ predefinedGroups,
215+ options,
216+ name,
217+ } ) ?? 'unknown'
218+
190219 sortingNodes . push ( {
191220 isIgnored :
192221 ! options . sortSideEffects &&
@@ -196,7 +225,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
196225 isEslintDisabled : isNodeEslintDisabled ( node , eslintDisabledLines ) ,
197226 size : rangeToDiff ( node , sourceCode ) ,
198227 addSafetySemicolonWhenInline : true ,
199- group : getGroup ( ) ,
228+ group,
200229 name,
201230 node,
202231 ...( options . type === 'line-length' &&
@@ -480,3 +509,31 @@ let getNodeName = ({
480509 let { value } = callExpression . arguments [ 0 ] as TSESTree . Literal
481510 return value ! . toString ( )
482511}
512+
513+ let computeGroupExceptUnknown = ( {
514+ predefinedGroups,
515+ customGroups,
516+ options,
517+ name,
518+ } : {
519+ options : Omit <
520+ Required < Options [ 0 ] > ,
521+ 'tsconfigRootDir' | 'maxLineLength' | 'customGroups'
522+ >
523+ customGroups ?: DeprecatedCustomGroupsOption | undefined
524+ predefinedGroups : Group [ ]
525+ name : string
526+ } ) : Group | null => {
527+ let computedCustomGroup = computeGroup ( {
528+ options : {
529+ ...options ,
530+ customGroups,
531+ } ,
532+ predefinedGroups,
533+ name,
534+ } )
535+ if ( computedCustomGroup === 'unknown' ) {
536+ return null
537+ }
538+ return computedCustomGroup
539+ }
0 commit comments