eslint-plugin-perfectionist 4.2.0 → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/alphabet.d.ts +176 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +8 -0
- package/dist/rules/sort-array-includes-utils.js +30 -0
- package/dist/rules/sort-array-includes.js +150 -54
- package/dist/rules/sort-array-includes.types.js +10 -0
- package/dist/rules/sort-classes.js +19 -16
- package/dist/rules/sort-decorators.js +11 -6
- package/dist/rules/sort-enums.js +39 -31
- package/dist/rules/sort-exports.js +11 -5
- package/dist/rules/sort-heritage-clauses.js +12 -6
- package/dist/rules/sort-imports.js +34 -21
- package/dist/rules/sort-interfaces.js +3 -1
- package/dist/rules/sort-intersection-types.js +24 -19
- package/dist/rules/sort-jsx-props.js +12 -7
- package/dist/rules/sort-maps.js +18 -13
- package/dist/rules/sort-modules.js +23 -18
- package/dist/rules/sort-named-exports.js +18 -13
- package/dist/rules/sort-named-imports.js +18 -13
- package/dist/rules/sort-object-types.js +149 -103
- package/dist/rules/sort-objects.js +142 -58
- package/dist/rules/sort-sets.js +14 -5
- package/dist/rules/sort-switch-case.js +16 -14
- package/dist/rules/sort-union-types.js +24 -19
- package/dist/rules/sort-variable-declarations.js +18 -13
- package/dist/utils/alphabet.js +385 -0
- package/dist/utils/common-json-schemas.js +27 -5
- package/dist/utils/compare.js +88 -38
- package/dist/utils/convert-boolean-to-sign.js +4 -0
- package/dist/utils/create-node-index-map.js +10 -0
- package/dist/{rules → utils}/get-custom-groups-compare-options.js +1 -0
- package/dist/utils/{get-node-parent.js → get-first-node-parent-with-type.js} +3 -4
- package/dist/utils/get-matching-context-options.js +18 -0
- package/dist/utils/get-settings.js +1 -0
- package/dist/utils/is-node-function-type.js +15 -0
- package/dist/utils/is-partition-comment.js +4 -1
- package/dist/utils/pairwise.js +1 -3
- package/dist/utils/validate-custom-sort-configuration.js +11 -0
- package/dist/{rules → utils}/validate-generated-groups-configuration.js +1 -1
- package/dist/utils/validate-groups-configuration.js +11 -5
- package/package.json +7 -3
- package/readme.md +4 -3
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
const commonJsonSchemas = require('../utils/common-json-schemas.js')
|
|
3
|
+
const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js')
|
|
3
4
|
const validateGroupsConfiguration = require('../utils/validate-groups-configuration.js')
|
|
4
5
|
const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js')
|
|
5
6
|
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js')
|
|
6
7
|
const isPartitionComment = require('../utils/is-partition-comment.js')
|
|
8
|
+
const createNodeIndexMap = require('../utils/create-node-index-map.js')
|
|
7
9
|
const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js')
|
|
8
10
|
const getCommentsBefore = require('../utils/get-comments-before.js')
|
|
9
11
|
const getNodeDecorators = require('../utils/get-node-decorators.js')
|
|
@@ -31,6 +33,7 @@ let defaultOptions = {
|
|
|
31
33
|
ignoreCase: true,
|
|
32
34
|
customGroups: {},
|
|
33
35
|
locales: 'en-US',
|
|
36
|
+
alphabet: '',
|
|
34
37
|
order: 'asc',
|
|
35
38
|
groups: [],
|
|
36
39
|
}
|
|
@@ -72,10 +75,11 @@ const sortDecorators = createEslintRule.createEslintRule({
|
|
|
72
75
|
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema,
|
|
73
76
|
customGroups: commonJsonSchemas.customGroupsJsonSchema,
|
|
74
77
|
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema,
|
|
78
|
+
alphabet: commonJsonSchemas.alphabetJsonSchema,
|
|
79
|
+
type: commonJsonSchemas.buildTypeJsonSchema(),
|
|
75
80
|
locales: commonJsonSchemas.localesJsonSchema,
|
|
76
81
|
groups: commonJsonSchemas.groupsJsonSchema,
|
|
77
82
|
order: commonJsonSchemas.orderJsonSchema,
|
|
78
|
-
type: commonJsonSchemas.typeJsonSchema,
|
|
79
83
|
},
|
|
80
84
|
additionalProperties: false,
|
|
81
85
|
type: 'object',
|
|
@@ -102,6 +106,7 @@ const sortDecorators = createEslintRule.createEslintRule({
|
|
|
102
106
|
settings,
|
|
103
107
|
defaultOptions,
|
|
104
108
|
)
|
|
109
|
+
validateCustomSortConfiguration.validateCustomSortConfiguration(options)
|
|
105
110
|
validateGroupsConfiguration.validateGroupsConfiguration(
|
|
106
111
|
options.groups,
|
|
107
112
|
['unknown'],
|
|
@@ -173,7 +178,6 @@ let sortDecorators$1 = (context, options, decorators) => {
|
|
|
173
178
|
let formattedMembers = decorators.reduce(
|
|
174
179
|
(accumulator, decorator) => {
|
|
175
180
|
if (
|
|
176
|
-
options.partitionByComment &&
|
|
177
181
|
isPartitionComment.hasPartitionComment(
|
|
178
182
|
options.partitionByComment,
|
|
179
183
|
getCommentsBefore.getCommentsBefore({
|
|
@@ -212,14 +216,15 @@ let sortDecorators$1 = (context, options, decorators) => {
|
|
|
212
216
|
let sortedNodesExcludingEslintDisabled =
|
|
213
217
|
sortNodesExcludingEslintDisabled(true)
|
|
214
218
|
let nodes = formattedMembers.flat()
|
|
219
|
+
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes)
|
|
215
220
|
pairwise.pairwise(nodes, (left, right) => {
|
|
216
|
-
let
|
|
217
|
-
let
|
|
221
|
+
let leftIndex = nodeIndexMap.get(left)
|
|
222
|
+
let rightIndex = nodeIndexMap.get(right)
|
|
218
223
|
let indexOfRightExcludingEslintDisabled =
|
|
219
224
|
sortedNodesExcludingEslintDisabled.indexOf(right)
|
|
220
225
|
if (
|
|
221
|
-
|
|
222
|
-
|
|
226
|
+
leftIndex < rightIndex &&
|
|
227
|
+
leftIndex < indexOfRightExcludingEslintDisabled
|
|
223
228
|
) {
|
|
224
229
|
return
|
|
225
230
|
}
|
package/dist/rules/sort-enums.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
const commonJsonSchemas = require('../utils/common-json-schemas.js')
|
|
3
3
|
const sortNodesByDependencies = require('../utils/sort-nodes-by-dependencies.js')
|
|
4
|
+
const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js')
|
|
4
5
|
const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js')
|
|
5
6
|
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js')
|
|
6
7
|
const isPartitionComment = require('../utils/is-partition-comment.js')
|
|
8
|
+
const createNodeIndexMap = require('../utils/create-node-index-map.js')
|
|
7
9
|
const getCommentsBefore = require('../utils/get-comments-before.js')
|
|
8
10
|
const createEslintRule = require('../utils/create-eslint-rule.js')
|
|
9
11
|
const getLinesBetween = require('../utils/get-lines-between.js')
|
|
@@ -26,6 +28,7 @@ let defaultOptions = {
|
|
|
26
28
|
sortByValue: false,
|
|
27
29
|
ignoreCase: true,
|
|
28
30
|
locales: 'en-US',
|
|
31
|
+
alphabet: '',
|
|
29
32
|
order: 'asc',
|
|
30
33
|
}
|
|
31
34
|
const sortEnums = createEslintRule.createEslintRule({
|
|
@@ -44,6 +47,7 @@ const sortEnums = createEslintRule.createEslintRule({
|
|
|
44
47
|
settings,
|
|
45
48
|
defaultOptions,
|
|
46
49
|
)
|
|
50
|
+
validateCustomSortConfiguration.validateCustomSortConfiguration(options)
|
|
47
51
|
let sourceCode = getSourceCode.getSourceCode(context)
|
|
48
52
|
let eslintDisabledLines = getEslintDisabledLines.getEslintDisabledLines({
|
|
49
53
|
ruleName: context.id,
|
|
@@ -51,30 +55,29 @@ const sortEnums = createEslintRule.createEslintRule({
|
|
|
51
55
|
})
|
|
52
56
|
let extractDependencies = (expression, enumName) => {
|
|
53
57
|
let dependencies = []
|
|
54
|
-
let
|
|
58
|
+
let stack = [expression]
|
|
59
|
+
while (stack.length) {
|
|
60
|
+
let node = stack.pop()
|
|
55
61
|
if (
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
62
|
+
node.type === 'MemberExpression' &&
|
|
63
|
+
node.object.type === 'Identifier' &&
|
|
64
|
+
node.object.name === enumName &&
|
|
65
|
+
node.property.type === 'Identifier'
|
|
60
66
|
) {
|
|
61
|
-
dependencies.push(
|
|
62
|
-
} else if (
|
|
63
|
-
dependencies.push(
|
|
67
|
+
dependencies.push(node.property.name)
|
|
68
|
+
} else if (node.type === 'Identifier') {
|
|
69
|
+
dependencies.push(node.name)
|
|
64
70
|
}
|
|
65
|
-
if ('left' in
|
|
66
|
-
|
|
71
|
+
if ('left' in node) {
|
|
72
|
+
stack.push(node.left)
|
|
67
73
|
}
|
|
68
|
-
if ('right' in
|
|
69
|
-
|
|
74
|
+
if ('right' in node) {
|
|
75
|
+
stack.push(node.right)
|
|
70
76
|
}
|
|
71
|
-
if ('expressions' in
|
|
72
|
-
|
|
73
|
-
checkNode(currentExpression)
|
|
74
|
-
}
|
|
77
|
+
if ('expressions' in node) {
|
|
78
|
+
stack.push(...node.expressions)
|
|
75
79
|
}
|
|
76
80
|
}
|
|
77
|
-
checkNode(expression)
|
|
78
81
|
return dependencies
|
|
79
82
|
}
|
|
80
83
|
let formattedMembers = members.reduce(
|
|
@@ -106,14 +109,13 @@ const sortEnums = createEslintRule.createEslintRule({
|
|
|
106
109
|
dependencies,
|
|
107
110
|
}
|
|
108
111
|
if (
|
|
109
|
-
(
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
)) ||
|
|
112
|
+
isPartitionComment.hasPartitionComment(
|
|
113
|
+
options.partitionByComment,
|
|
114
|
+
getCommentsBefore.getCommentsBefore({
|
|
115
|
+
node: member,
|
|
116
|
+
sourceCode,
|
|
117
|
+
}),
|
|
118
|
+
) ||
|
|
117
119
|
(options.partitionByNewLine &&
|
|
118
120
|
lastSortingNode &&
|
|
119
121
|
getLinesBetween.getLinesBetween(
|
|
@@ -158,13 +160,17 @@ const sortEnums = createEslintRule.createEslintRule({
|
|
|
158
160
|
return ''
|
|
159
161
|
}
|
|
160
162
|
: null,
|
|
161
|
-
|
|
163
|
+
/**
|
|
164
|
+
* If the enum is numeric, and we sort by value, always use the
|
|
165
|
+
* `natural` sort type, which will correctly sort them.
|
|
166
|
+
*/
|
|
162
167
|
type:
|
|
163
168
|
isNumericEnum && (options.forceNumericSort || options.sortByValue)
|
|
164
169
|
? 'natural'
|
|
165
170
|
: options.type,
|
|
166
171
|
specialCharacters: options.specialCharacters,
|
|
167
172
|
ignoreCase: options.ignoreCase,
|
|
173
|
+
alphabet: options.alphabet,
|
|
168
174
|
locales: options.locales,
|
|
169
175
|
order: options.order,
|
|
170
176
|
}
|
|
@@ -182,14 +188,15 @@ const sortEnums = createEslintRule.createEslintRule({
|
|
|
182
188
|
let sortedNodes = sortNodesIgnoringEslintDisabledNodes(false)
|
|
183
189
|
let sortedNodesExcludingEslintDisabled =
|
|
184
190
|
sortNodesIgnoringEslintDisabledNodes(true)
|
|
191
|
+
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes)
|
|
185
192
|
pairwise.pairwise(sortingNodes, (left, right) => {
|
|
186
|
-
let
|
|
187
|
-
let
|
|
193
|
+
let leftIndex = nodeIndexMap.get(left)
|
|
194
|
+
let rightIndex = nodeIndexMap.get(right)
|
|
188
195
|
let indexOfRightExcludingEslintDisabled =
|
|
189
196
|
sortedNodesExcludingEslintDisabled.indexOf(right)
|
|
190
197
|
if (
|
|
191
|
-
|
|
192
|
-
|
|
198
|
+
leftIndex < rightIndex &&
|
|
199
|
+
leftIndex < indexOfRightExcludingEslintDisabled
|
|
193
200
|
) {
|
|
194
201
|
return
|
|
195
202
|
}
|
|
@@ -244,9 +251,10 @@ const sortEnums = createEslintRule.createEslintRule({
|
|
|
244
251
|
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema,
|
|
245
252
|
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema,
|
|
246
253
|
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema,
|
|
254
|
+
alphabet: commonJsonSchemas.alphabetJsonSchema,
|
|
255
|
+
type: commonJsonSchemas.buildTypeJsonSchema(),
|
|
247
256
|
locales: commonJsonSchemas.localesJsonSchema,
|
|
248
257
|
order: commonJsonSchemas.orderJsonSchema,
|
|
249
|
-
type: commonJsonSchemas.typeJsonSchema,
|
|
250
258
|
},
|
|
251
259
|
additionalProperties: false,
|
|
252
260
|
type: 'object',
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
const commonJsonSchemas = require('../utils/common-json-schemas.js')
|
|
3
|
+
const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js')
|
|
3
4
|
const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js')
|
|
4
5
|
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js')
|
|
6
|
+
const createNodeIndexMap = require('../utils/create-node-index-map.js')
|
|
5
7
|
const isPartitionComment = require('../utils/is-partition-comment.js')
|
|
6
8
|
const getCommentsBefore = require('../utils/get-comments-before.js')
|
|
7
9
|
const createEslintRule = require('../utils/create-eslint-rule.js')
|
|
@@ -21,6 +23,7 @@ let defaultOptions = {
|
|
|
21
23
|
groupKind: 'mixed',
|
|
22
24
|
ignoreCase: true,
|
|
23
25
|
locales: 'en-US',
|
|
26
|
+
alphabet: '',
|
|
24
27
|
order: 'asc',
|
|
25
28
|
}
|
|
26
29
|
const sortExports = createEslintRule.createEslintRule({
|
|
@@ -31,6 +34,7 @@ const sortExports = createEslintRule.createEslintRule({
|
|
|
31
34
|
settings,
|
|
32
35
|
defaultOptions,
|
|
33
36
|
)
|
|
37
|
+
validateCustomSortConfiguration.validateCustomSortConfiguration(options)
|
|
34
38
|
let sourceCode = getSourceCode.getSourceCode(context)
|
|
35
39
|
let partitionComment = options.partitionByComment
|
|
36
40
|
let eslintDisabledLines = getEslintDisabledLines.getEslintDisabledLines({
|
|
@@ -95,14 +99,15 @@ const sortExports = createEslintRule.createEslintRule({
|
|
|
95
99
|
let sortedNodes = sortNodesExcludingEslintDisabled(false)
|
|
96
100
|
let sortedNodesExcludingEslintDisabled =
|
|
97
101
|
sortNodesExcludingEslintDisabled(true)
|
|
102
|
+
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes)
|
|
98
103
|
pairwise.pairwise(nodes, (left, right) => {
|
|
99
|
-
let
|
|
100
|
-
let
|
|
104
|
+
let leftIndex = nodeIndexMap.get(left)
|
|
105
|
+
let rightIndex = nodeIndexMap.get(right)
|
|
101
106
|
let indexOfRightExcludingEslintDisabled =
|
|
102
107
|
sortedNodesExcludingEslintDisabled.indexOf(right)
|
|
103
108
|
if (
|
|
104
|
-
|
|
105
|
-
|
|
109
|
+
leftIndex < rightIndex &&
|
|
110
|
+
leftIndex < indexOfRightExcludingEslintDisabled
|
|
106
111
|
) {
|
|
107
112
|
return
|
|
108
113
|
}
|
|
@@ -150,9 +155,10 @@ const sortExports = createEslintRule.createEslintRule({
|
|
|
150
155
|
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema,
|
|
151
156
|
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema,
|
|
152
157
|
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema,
|
|
158
|
+
alphabet: commonJsonSchemas.alphabetJsonSchema,
|
|
159
|
+
type: commonJsonSchemas.buildTypeJsonSchema(),
|
|
153
160
|
locales: commonJsonSchemas.localesJsonSchema,
|
|
154
161
|
order: commonJsonSchemas.orderJsonSchema,
|
|
155
|
-
type: commonJsonSchemas.typeJsonSchema,
|
|
156
162
|
},
|
|
157
163
|
additionalProperties: false,
|
|
158
164
|
type: 'object',
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
const commonJsonSchemas = require('../utils/common-json-schemas.js')
|
|
3
|
+
const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js')
|
|
3
4
|
const validateGroupsConfiguration = require('../utils/validate-groups-configuration.js')
|
|
4
5
|
const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js')
|
|
5
6
|
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js')
|
|
7
|
+
const createNodeIndexMap = require('../utils/create-node-index-map.js')
|
|
6
8
|
const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js')
|
|
7
9
|
const createEslintRule = require('../utils/create-eslint-rule.js')
|
|
8
10
|
const getGroupNumber = require('../utils/get-group-number.js')
|
|
@@ -21,6 +23,7 @@ let defaultOptions = {
|
|
|
21
23
|
ignoreCase: true,
|
|
22
24
|
customGroups: {},
|
|
23
25
|
locales: 'en-US',
|
|
26
|
+
alphabet: '',
|
|
24
27
|
order: 'asc',
|
|
25
28
|
groups: [],
|
|
26
29
|
}
|
|
@@ -32,10 +35,11 @@ const sortHeritageClauses = createEslintRule.createEslintRule({
|
|
|
32
35
|
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema,
|
|
33
36
|
customGroups: commonJsonSchemas.customGroupsJsonSchema,
|
|
34
37
|
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema,
|
|
38
|
+
alphabet: commonJsonSchemas.alphabetJsonSchema,
|
|
39
|
+
type: commonJsonSchemas.buildTypeJsonSchema(),
|
|
35
40
|
locales: commonJsonSchemas.localesJsonSchema,
|
|
36
41
|
groups: commonJsonSchemas.groupsJsonSchema,
|
|
37
42
|
order: commonJsonSchemas.orderJsonSchema,
|
|
38
|
-
type: commonJsonSchemas.typeJsonSchema,
|
|
39
43
|
},
|
|
40
44
|
additionalProperties: false,
|
|
41
45
|
type: 'object',
|
|
@@ -62,6 +66,7 @@ const sortHeritageClauses = createEslintRule.createEslintRule({
|
|
|
62
66
|
settings,
|
|
63
67
|
defaultOptions,
|
|
64
68
|
)
|
|
69
|
+
validateCustomSortConfiguration.validateCustomSortConfiguration(options)
|
|
65
70
|
validateGroupsConfiguration.validateGroupsConfiguration(
|
|
66
71
|
options.groups,
|
|
67
72
|
['unknown'],
|
|
@@ -78,7 +83,7 @@ const sortHeritageClauses = createEslintRule.createEslintRule({
|
|
|
78
83
|
name: 'sort-heritage-clauses',
|
|
79
84
|
})
|
|
80
85
|
let sortHeritageClauses$1 = (context, options, heritageClauses) => {
|
|
81
|
-
if (!
|
|
86
|
+
if (!isSortable.isSortable(heritageClauses)) {
|
|
82
87
|
return
|
|
83
88
|
}
|
|
84
89
|
let sourceCode = getSourceCode.getSourceCode(context)
|
|
@@ -108,14 +113,15 @@ let sortHeritageClauses$1 = (context, options, heritageClauses) => {
|
|
|
108
113
|
let sortedNodes = sortNodesExcludingEslintDisabled(false)
|
|
109
114
|
let sortedNodesExcludingEslintDisabled =
|
|
110
115
|
sortNodesExcludingEslintDisabled(true)
|
|
116
|
+
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes)
|
|
111
117
|
pairwise.pairwise(nodes, (left, right) => {
|
|
112
|
-
let
|
|
113
|
-
let
|
|
118
|
+
let leftIndex = nodeIndexMap.get(left)
|
|
119
|
+
let rightIndex = nodeIndexMap.get(right)
|
|
114
120
|
let indexOfRightExcludingEslintDisabled =
|
|
115
121
|
sortedNodesExcludingEslintDisabled.indexOf(right)
|
|
116
122
|
if (
|
|
117
|
-
|
|
118
|
-
|
|
123
|
+
leftIndex < rightIndex &&
|
|
124
|
+
leftIndex < indexOfRightExcludingEslintDisabled
|
|
119
125
|
) {
|
|
120
126
|
return
|
|
121
127
|
}
|
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
const node_module = require('node:module')
|
|
3
3
|
const commonJsonSchemas = require('../utils/common-json-schemas.js')
|
|
4
4
|
const validateNewlinesAndPartitionConfiguration = require('../utils/validate-newlines-and-partition-configuration.js')
|
|
5
|
+
const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js')
|
|
5
6
|
const validateGroupsConfiguration = require('../utils/validate-groups-configuration.js')
|
|
6
7
|
const readClosestTsConfigByPath = require('../utils/read-closest-ts-config-by-path.js')
|
|
7
8
|
const getOptionsWithCleanGroups = require('../utils/get-options-with-clean-groups.js')
|
|
8
9
|
const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js')
|
|
9
10
|
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js')
|
|
10
11
|
const getTypescriptImport = require('../utils/get-typescript-import.js')
|
|
12
|
+
const createNodeIndexMap = require('../utils/create-node-index-map.js')
|
|
11
13
|
const isPartitionComment = require('../utils/is-partition-comment.js')
|
|
12
14
|
const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js')
|
|
13
15
|
const getCommentsBefore = require('../utils/get-comments-before.js')
|
|
@@ -52,6 +54,7 @@ const sortImports = createEslintRule.createEslintRule({
|
|
|
52
54
|
environment: 'node',
|
|
53
55
|
ignoreCase: true,
|
|
54
56
|
locales: 'en-US',
|
|
57
|
+
alphabet: '',
|
|
55
58
|
order: 'asc',
|
|
56
59
|
}),
|
|
57
60
|
)
|
|
@@ -82,6 +85,7 @@ const sortImports = createEslintRule.createEslintRule({
|
|
|
82
85
|
...Object.keys(options.customGroups.value ?? {}),
|
|
83
86
|
],
|
|
84
87
|
)
|
|
88
|
+
validateCustomSortConfiguration.validateCustomSortConfiguration(options)
|
|
85
89
|
validateNewlinesAndPartitionConfiguration.validateNewlinesAndPartitionConfiguration(
|
|
86
90
|
options,
|
|
87
91
|
)
|
|
@@ -129,11 +133,18 @@ const sortImports = createEslintRule.createEslintRule({
|
|
|
129
133
|
node.specifiers.length ===
|
|
130
134
|
0 /* Avoid matching on named imports without specifiers */ &&
|
|
131
135
|
!/\}\s*from\s+/u.test(sourceCode.getText(node))
|
|
136
|
+
let styleExtensions = [
|
|
137
|
+
'.less',
|
|
138
|
+
'.scss',
|
|
139
|
+
'.sass',
|
|
140
|
+
'.styl',
|
|
141
|
+
'.pcss',
|
|
142
|
+
'.css',
|
|
143
|
+
'.sss',
|
|
144
|
+
]
|
|
132
145
|
let isStyle = value => {
|
|
133
146
|
let [cleanedValue] = value.split('?')
|
|
134
|
-
return
|
|
135
|
-
extension => cleanedValue.endsWith(extension),
|
|
136
|
-
)
|
|
147
|
+
return styleExtensions.some(extension => cleanedValue.endsWith(extension))
|
|
137
148
|
}
|
|
138
149
|
let flatGroups = new Set(options.groups.flat())
|
|
139
150
|
let shouldRegroupSideEffectNodes = flatGroups.has('side-effect')
|
|
@@ -327,24 +338,22 @@ const sortImports = createEslintRule.createEslintRule({
|
|
|
327
338
|
}
|
|
328
339
|
return {
|
|
329
340
|
'Program:exit': () => {
|
|
330
|
-
var _a
|
|
331
341
|
let hasContentBetweenNodes = (left, right) =>
|
|
332
342
|
!!sourceCode.getTokensBetween(left.node, right.node, {
|
|
333
343
|
includeComments: false,
|
|
334
344
|
}).length
|
|
335
345
|
let formattedMembers = [[]]
|
|
336
346
|
for (let sortingNode of nodes) {
|
|
337
|
-
let
|
|
338
|
-
|
|
347
|
+
let lastGroup = formattedMembers.at(-1)
|
|
348
|
+
let lastSortingNode = lastGroup == null ? void 0 : lastGroup.at(-1)
|
|
339
349
|
if (
|
|
340
|
-
(
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
)) ||
|
|
350
|
+
isPartitionComment.hasPartitionComment(
|
|
351
|
+
options.partitionByComment,
|
|
352
|
+
getCommentsBefore.getCommentsBefore({
|
|
353
|
+
node: sortingNode.node,
|
|
354
|
+
sourceCode,
|
|
355
|
+
}),
|
|
356
|
+
) ||
|
|
348
357
|
(options.partitionByNewLine &&
|
|
349
358
|
lastSortingNode &&
|
|
350
359
|
getLinesBetween.getLinesBetween(
|
|
@@ -355,9 +364,10 @@ const sortImports = createEslintRule.createEslintRule({
|
|
|
355
364
|
(lastSortingNode &&
|
|
356
365
|
hasContentBetweenNodes(lastSortingNode, sortingNode))
|
|
357
366
|
) {
|
|
358
|
-
|
|
367
|
+
lastGroup = []
|
|
368
|
+
formattedMembers.push(lastGroup)
|
|
359
369
|
}
|
|
360
|
-
|
|
370
|
+
lastGroup.push(sortingNode)
|
|
361
371
|
}
|
|
362
372
|
for (let nodeList of formattedMembers) {
|
|
363
373
|
let sortNodesExcludingEslintDisabled = ignoreEslintDisabledNodes =>
|
|
@@ -375,20 +385,21 @@ const sortImports = createEslintRule.createEslintRule({
|
|
|
375
385
|
let sortedNodes = sortNodesExcludingEslintDisabled(false)
|
|
376
386
|
let sortedNodesExcludingEslintDisabled =
|
|
377
387
|
sortNodesExcludingEslintDisabled(true)
|
|
388
|
+
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes)
|
|
378
389
|
pairwise.pairwise(nodeList, (left, right) => {
|
|
379
390
|
let leftNumber = getGroupNumber.getGroupNumber(options.groups, left)
|
|
380
391
|
let rightNumber = getGroupNumber.getGroupNumber(
|
|
381
392
|
options.groups,
|
|
382
393
|
right,
|
|
383
394
|
)
|
|
384
|
-
let
|
|
385
|
-
let
|
|
395
|
+
let leftIndex = nodeIndexMap.get(left)
|
|
396
|
+
let rightIndex = nodeIndexMap.get(right)
|
|
386
397
|
let indexOfRightExcludingEslintDisabled =
|
|
387
398
|
sortedNodesExcludingEslintDisabled.indexOf(right)
|
|
388
399
|
let messageIds = []
|
|
389
400
|
if (
|
|
390
|
-
|
|
391
|
-
|
|
401
|
+
leftIndex > rightIndex ||
|
|
402
|
+
leftIndex >= indexOfRightExcludingEslintDisabled
|
|
392
403
|
) {
|
|
393
404
|
messageIds.push(
|
|
394
405
|
leftNumber === rightNumber
|
|
@@ -513,10 +524,11 @@ const sortImports = createEslintRule.createEslintRule({
|
|
|
513
524
|
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema,
|
|
514
525
|
newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema,
|
|
515
526
|
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema,
|
|
527
|
+
alphabet: commonJsonSchemas.alphabetJsonSchema,
|
|
528
|
+
type: commonJsonSchemas.buildTypeJsonSchema(),
|
|
516
529
|
locales: commonJsonSchemas.localesJsonSchema,
|
|
517
530
|
groups: commonJsonSchemas.groupsJsonSchema,
|
|
518
531
|
order: commonJsonSchemas.orderJsonSchema,
|
|
519
|
-
type: commonJsonSchemas.typeJsonSchema,
|
|
520
532
|
},
|
|
521
533
|
definitions: {
|
|
522
534
|
'max-line-length-requires-line-length-type': {
|
|
@@ -594,6 +606,7 @@ const sortImports = createEslintRule.createEslintRule({
|
|
|
594
606
|
environment: 'node',
|
|
595
607
|
ignoreCase: true,
|
|
596
608
|
locales: 'en-US',
|
|
609
|
+
alphabet: '',
|
|
597
610
|
order: 'asc',
|
|
598
611
|
},
|
|
599
612
|
],
|
|
@@ -6,12 +6,14 @@ let defaultOptions = {
|
|
|
6
6
|
partitionByNewLine: false,
|
|
7
7
|
newlinesBetween: 'ignore',
|
|
8
8
|
specialCharacters: 'keep',
|
|
9
|
+
useConfigurationIf: {},
|
|
9
10
|
type: 'alphabetical',
|
|
10
11
|
groupKind: 'mixed',
|
|
11
12
|
ignorePattern: [],
|
|
12
13
|
ignoreCase: true,
|
|
13
14
|
customGroups: {},
|
|
14
15
|
locales: 'en-US',
|
|
16
|
+
alphabet: '',
|
|
15
17
|
order: 'asc',
|
|
16
18
|
groups: [],
|
|
17
19
|
}
|
|
@@ -32,7 +34,7 @@ const sortInterfaces = createEslintRule.createEslintRule({
|
|
|
32
34
|
description: 'Enforce sorted interface properties.',
|
|
33
35
|
recommended: true,
|
|
34
36
|
},
|
|
35
|
-
schema:
|
|
37
|
+
schema: sortObjectTypes.jsonSchema,
|
|
36
38
|
type: 'suggestion',
|
|
37
39
|
fixable: 'code',
|
|
38
40
|
},
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
const commonJsonSchemas = require('../utils/common-json-schemas.js')
|
|
3
3
|
const validateNewlinesAndPartitionConfiguration = require('../utils/validate-newlines-and-partition-configuration.js')
|
|
4
|
+
const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js')
|
|
4
5
|
const validateGroupsConfiguration = require('../utils/validate-groups-configuration.js')
|
|
5
6
|
const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js')
|
|
6
7
|
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js')
|
|
8
|
+
const createNodeIndexMap = require('../utils/create-node-index-map.js')
|
|
7
9
|
const isPartitionComment = require('../utils/is-partition-comment.js')
|
|
8
10
|
const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js')
|
|
9
11
|
const getCommentsBefore = require('../utils/get-comments-before.js')
|
|
@@ -28,6 +30,7 @@ let defaultOptions = {
|
|
|
28
30
|
type: 'alphabetical',
|
|
29
31
|
ignoreCase: true,
|
|
30
32
|
locales: 'en-US',
|
|
33
|
+
alphabet: '',
|
|
31
34
|
order: 'asc',
|
|
32
35
|
groups: [],
|
|
33
36
|
}
|
|
@@ -40,6 +43,7 @@ const sortIntersectionTypes = createEslintRule.createEslintRule({
|
|
|
40
43
|
settings,
|
|
41
44
|
defaultOptions,
|
|
42
45
|
)
|
|
46
|
+
validateCustomSortConfiguration.validateCustomSortConfiguration(options)
|
|
43
47
|
validateGroupsConfiguration.validateGroupsConfiguration(
|
|
44
48
|
options.groups,
|
|
45
49
|
[
|
|
@@ -69,7 +73,6 @@ const sortIntersectionTypes = createEslintRule.createEslintRule({
|
|
|
69
73
|
})
|
|
70
74
|
let formattedMembers = node.types.reduce(
|
|
71
75
|
(accumulator, type) => {
|
|
72
|
-
var _a, _b
|
|
73
76
|
let { defineGroup, getGroup } = useGroups.useGroups(options)
|
|
74
77
|
switch (type.type) {
|
|
75
78
|
case 'TSTemplateLiteralType':
|
|
@@ -128,8 +131,8 @@ const sortIntersectionTypes = createEslintRule.createEslintRule({
|
|
|
128
131
|
defineGroup('union')
|
|
129
132
|
break
|
|
130
133
|
}
|
|
131
|
-
let
|
|
132
|
-
|
|
134
|
+
let lastGroup = accumulator.at(-1)
|
|
135
|
+
let lastSortingNode = lastGroup == null ? void 0 : lastGroup.at(-1)
|
|
133
136
|
let sortingNode = {
|
|
134
137
|
isEslintDisabled: isNodeEslintDisabled.isNodeEslintDisabled(
|
|
135
138
|
type,
|
|
@@ -141,15 +144,14 @@ const sortIntersectionTypes = createEslintRule.createEslintRule({
|
|
|
141
144
|
node: type,
|
|
142
145
|
}
|
|
143
146
|
if (
|
|
144
|
-
(
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
)) ||
|
|
147
|
+
isPartitionComment.hasPartitionComment(
|
|
148
|
+
options.partitionByComment,
|
|
149
|
+
getCommentsBefore.getCommentsBefore({
|
|
150
|
+
tokenValueToIgnoreBefore: '&',
|
|
151
|
+
node: type,
|
|
152
|
+
sourceCode,
|
|
153
|
+
}),
|
|
154
|
+
) ||
|
|
153
155
|
(options.partitionByNewLine &&
|
|
154
156
|
lastSortingNode &&
|
|
155
157
|
getLinesBetween.getLinesBetween(
|
|
@@ -158,9 +160,10 @@ const sortIntersectionTypes = createEslintRule.createEslintRule({
|
|
|
158
160
|
sortingNode,
|
|
159
161
|
))
|
|
160
162
|
) {
|
|
161
|
-
|
|
163
|
+
lastGroup = []
|
|
164
|
+
accumulator.push(lastGroup)
|
|
162
165
|
}
|
|
163
|
-
|
|
166
|
+
lastGroup == null ? void 0 : lastGroup.push(sortingNode)
|
|
164
167
|
return accumulator
|
|
165
168
|
},
|
|
166
169
|
[[]],
|
|
@@ -173,17 +176,18 @@ const sortIntersectionTypes = createEslintRule.createEslintRule({
|
|
|
173
176
|
let sortedNodes = sortNodesExcludingEslintDisabled(false)
|
|
174
177
|
let sortedNodesExcludingEslintDisabled =
|
|
175
178
|
sortNodesExcludingEslintDisabled(true)
|
|
179
|
+
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes)
|
|
176
180
|
pairwise.pairwise(nodes, (left, right) => {
|
|
177
181
|
let leftNumber = getGroupNumber.getGroupNumber(options.groups, left)
|
|
178
182
|
let rightNumber = getGroupNumber.getGroupNumber(options.groups, right)
|
|
179
|
-
let
|
|
180
|
-
let
|
|
183
|
+
let leftIndex = nodeIndexMap.get(left)
|
|
184
|
+
let rightIndex = nodeIndexMap.get(right)
|
|
181
185
|
let indexOfRightExcludingEslintDisabled =
|
|
182
186
|
sortedNodesExcludingEslintDisabled.indexOf(right)
|
|
183
187
|
let messageIds = []
|
|
184
188
|
if (
|
|
185
|
-
|
|
186
|
-
|
|
189
|
+
leftIndex > rightIndex ||
|
|
190
|
+
leftIndex >= indexOfRightExcludingEslintDisabled
|
|
187
191
|
) {
|
|
188
192
|
messageIds.push(
|
|
189
193
|
leftNumber === rightNumber
|
|
@@ -249,10 +253,11 @@ const sortIntersectionTypes = createEslintRule.createEslintRule({
|
|
|
249
253
|
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema,
|
|
250
254
|
newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema,
|
|
251
255
|
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema,
|
|
256
|
+
alphabet: commonJsonSchemas.alphabetJsonSchema,
|
|
257
|
+
type: commonJsonSchemas.buildTypeJsonSchema(),
|
|
252
258
|
locales: commonJsonSchemas.localesJsonSchema,
|
|
253
259
|
groups: commonJsonSchemas.groupsJsonSchema,
|
|
254
260
|
order: commonJsonSchemas.orderJsonSchema,
|
|
255
|
-
type: commonJsonSchemas.typeJsonSchema,
|
|
256
261
|
},
|
|
257
262
|
additionalProperties: false,
|
|
258
263
|
type: 'object',
|