Skip to content

Commit b07949b

Browse files
authored
refactor(sort-imports): use compute group instead of use groups
1 parent ae7a4a1 commit b07949b

9 files changed

Lines changed: 254 additions & 1247 deletions

rules/sort-imports.ts

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import type { TSESTree } from '@typescript-eslint/types'
22
import 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

611
import {
712
partitionByCommentJsonSchema,
@@ -31,10 +36,10 @@ import { sortNodesByGroups } from '../utils/sort-nodes-by-groups'
3136
import { createEslintRule } from '../utils/create-eslint-rule'
3237
import { reportAllErrors } from '../utils/report-all-errors'
3338
import { shouldPartition } from '../utils/should-partition'
39+
import { computeGroup } from '../utils/compute-group'
3440
import { rangeToDiff } from '../utils/range-to-diff'
3541
import { getSettings } from '../utils/get-settings'
3642
import { isSortable } from '../utils/is-sortable'
37-
import { useGroups } from '../utils/use-groups'
3843
import { complete } from '../utils/complete'
3944

4045
export 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+
}

test/rules/sort-classes.test.ts

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2791,11 +2791,7 @@ describe(ruleName, () => {
27912791
b = () => null
27922792
}
27932793
`,
2794-
options: [
2795-
{
2796-
...options,
2797-
},
2798-
],
2794+
options: [options],
27992795
},
28002796
],
28012797
invalid: [],
@@ -3117,11 +3113,7 @@ describe(ruleName, () => {
31173113
c = 10
31183114
}
31193115
`,
3120-
options: [
3121-
{
3122-
...options,
3123-
},
3124-
],
3116+
options: [options],
31253117
},
31263118
],
31273119
valid: [],
@@ -3295,11 +3287,7 @@ describe(ruleName, () => {
32953287
static a = Class.b.bMethod().anotherNestedMethod(this.c).finalMethod()
32963288
}
32973289
`,
3298-
options: [
3299-
{
3300-
...options,
3301-
},
3302-
],
3290+
options: [options],
33033291
},
33043292
],
33053293
invalid: [],
@@ -3745,11 +3733,7 @@ describe(ruleName, () => {
37453733
static #a = this.#b
37463734
}
37473735
`,
3748-
options: [
3749-
{
3750-
...options,
3751-
},
3752-
],
3736+
options: [options],
37533737
},
37543738
{
37553739
code: dedent`
@@ -3758,11 +3742,7 @@ describe(ruleName, () => {
37583742
static #a = this.#b()
37593743
}
37603744
`,
3761-
options: [
3762-
{
3763-
...options,
3764-
},
3765-
],
3745+
options: [options],
37663746
},
37673747
{
37683748
code: dedent`
@@ -7010,11 +6990,7 @@ describe(ruleName, () => {
70106990
messageId: 'unexpectedClassesGroupOrder',
70116991
},
70126992
],
7013-
options: [
7014-
{
7015-
...options,
7016-
},
7017-
],
6993+
options: [options],
70186994
},
70196995
],
70206996
valid: [
@@ -8716,11 +8692,7 @@ describe(ruleName, () => {
87168692
b
87178693
}
87188694
`,
8719-
options: [
8720-
{
8721-
...options,
8722-
},
8723-
],
8695+
options: [options],
87248696
},
87258697
],
87268698
valid: [],

test/rules/sort-enums.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3005,11 +3005,7 @@ describe(ruleName, () => {
30053005
b = 1,
30063006
}
30073007
`,
3008-
options: [
3009-
{
3010-
...options,
3011-
},
3012-
],
3008+
options: [options],
30133009
},
30143010
],
30153011
valid: [],

0 commit comments

Comments
 (0)