1
- import type { TSESTree } from '@typescript-eslint/types'
1
+ import type { TSESLint } from '@typescript-eslint/utils'
2
+
3
+ import { TSESTree } from '@typescript-eslint/types'
2
4
3
5
import type { SortingNode } from '../types/sorting-node'
4
6
import type { Options } from './sort-maps/types'
5
7
6
8
import {
9
+ buildUseConfigurationIfJsonSchema ,
7
10
buildCustomGroupsArrayJsonSchema ,
8
11
partitionByCommentJsonSchema ,
9
12
partitionByNewLineJsonSchema ,
@@ -19,6 +22,7 @@ import {
19
22
import { validateGeneratedGroupsConfiguration } from '../utils/validate-generated-groups-configuration'
20
23
import { validateCustomSortConfiguration } from '../utils/validate-custom-sort-configuration'
21
24
import { getCustomGroupsCompareOptions } from '../utils/get-custom-groups-compare-options'
25
+ import { getMatchingContextOptions } from '../utils/get-matching-context-options'
22
26
import { getEslintDisabledLines } from '../utils/get-eslint-disabled-lines'
23
27
import { doesCustomGroupMatch } from './sort-maps/does-custom-group-match'
24
28
import { isNodeEslintDisabled } from '../utils/is-node-eslint-disabled'
@@ -52,6 +56,7 @@ let defaultOptions: Required<Options[0]> = {
52
56
partitionByComment : false ,
53
57
partitionByNewLine : false ,
54
58
newlinesBetween : 'ignore' ,
59
+ useConfigurationIf : { } ,
55
60
type : 'alphabetical' ,
56
61
ignoreCase : true ,
57
62
customGroups : [ ] ,
@@ -77,8 +82,21 @@ export default createEslintRule<Options, MESSAGE_ID>({
77
82
return
78
83
}
79
84
85
+ let sourceCode = getSourceCode ( context )
80
86
let settings = getSettings ( context . settings )
81
- let options = complete ( context . options . at ( 0 ) , settings , defaultOptions )
87
+
88
+ let matchedContextOptions = getMatchingContextOptions ( {
89
+ nodeNames : elements
90
+ . filter (
91
+ element =>
92
+ element !== null &&
93
+ element . type !== TSESTree . AST_NODE_TYPES . SpreadElement ,
94
+ )
95
+ . map ( element => getNodeName ( { sourceCode, element } ) ) ,
96
+ contextOptions : context . options ,
97
+ } )
98
+
99
+ let options = complete ( matchedContextOptions [ 0 ] , settings , defaultOptions )
82
100
validateCustomSortConfiguration ( options )
83
101
validateGeneratedGroupsConfiguration ( {
84
102
customGroups : options . customGroups ,
@@ -87,7 +105,6 @@ export default createEslintRule<Options, MESSAGE_ID>({
87
105
modifiers : [ ] ,
88
106
} )
89
107
90
- let sourceCode = getSourceCode ( context )
91
108
let eslintDisabledLines = getEslintDisabledLines ( {
92
109
ruleName : context . id ,
93
110
sourceCode,
@@ -110,21 +127,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
110
127
for ( let part of parts ) {
111
128
let formattedMembers : SortingNode [ ] [ ] = [ [ ] ]
112
129
for ( let element of part ) {
113
- let name : string
114
-
115
- if ( element . type === 'ArrayExpression' ) {
116
- let [ left ] = element . elements
117
-
118
- if ( ! left ) {
119
- name = `${ left } `
120
- } else if ( left . type === 'Literal' ) {
121
- name = left . raw
122
- } else {
123
- name = sourceCode . getText ( left )
124
- }
125
- } else {
126
- name = sourceCode . getText ( element )
127
- }
130
+ let name : string = getNodeName ( {
131
+ sourceCode,
132
+ element,
133
+ } )
128
134
129
135
let lastSortingNode = formattedMembers . at ( - 1 ) ?. at ( - 1 )
130
136
@@ -265,6 +271,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
265
271
customGroups : buildCustomGroupsArrayJsonSchema ( {
266
272
singleCustomGroupJsonSchema,
267
273
} ) ,
274
+ useConfigurationIf : buildUseConfigurationIfJsonSchema ( ) ,
268
275
partitionByNewLine : partitionByNewLineJsonSchema ,
269
276
specialCharacters : specialCharactersJsonSchema ,
270
277
newlinesBetween : newlinesBetweenJsonSchema ,
@@ -300,3 +307,23 @@ export default createEslintRule<Options, MESSAGE_ID>({
300
307
defaultOptions : [ defaultOptions ] ,
301
308
name : 'sort-maps' ,
302
309
} )
310
+
311
+ let getNodeName = ( {
312
+ sourceCode,
313
+ element,
314
+ } : {
315
+ sourceCode : TSESLint . SourceCode
316
+ element : TSESTree . Expression
317
+ } ) : string => {
318
+ if ( element . type === 'ArrayExpression' ) {
319
+ let [ left ] = element . elements
320
+
321
+ if ( ! left ) {
322
+ return `${ left } `
323
+ } else if ( left . type === 'Literal' ) {
324
+ return left . raw
325
+ }
326
+ return sourceCode . getText ( left )
327
+ }
328
+ return sourceCode . getText ( element )
329
+ }
0 commit comments