eslint-plugin-perfectionist 3.3.0 → 3.5.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/index.d.ts +243 -21
- package/dist/index.js +3 -1
- package/dist/index.mjs +2 -0
- package/dist/rules/sort-array-includes.js +108 -107
- package/dist/rules/sort-array-includes.mjs +100 -100
- package/dist/rules/sort-astro-attributes.js +6 -0
- package/dist/rules/sort-astro-attributes.mjs +6 -0
- package/dist/rules/sort-classes-utils.js +71 -0
- package/dist/rules/sort-classes-utils.mjs +75 -1
- package/dist/rules/sort-classes.js +234 -79
- package/dist/rules/sort-classes.mjs +232 -69
- package/dist/rules/sort-classes.types.js +71 -0
- package/dist/rules/sort-classes.types.mjs +71 -0
- package/dist/rules/sort-enums.js +11 -15
- package/dist/rules/sort-enums.mjs +11 -11
- package/dist/rules/sort-imports.js +46 -5
- package/dist/rules/sort-imports.mjs +46 -5
- package/dist/rules/sort-interfaces.js +6 -0
- package/dist/rules/sort-interfaces.mjs +6 -0
- package/dist/rules/sort-intersection-types.js +20 -0
- package/dist/rules/sort-intersection-types.mjs +20 -0
- package/dist/rules/sort-jsx-props.js +6 -0
- package/dist/rules/sort-jsx-props.mjs +6 -0
- package/dist/rules/sort-object-types.js +6 -0
- package/dist/rules/sort-object-types.mjs +6 -0
- package/dist/rules/sort-objects.js +34 -43
- package/dist/rules/sort-objects.mjs +28 -30
- package/dist/rules/sort-sets.js +48 -0
- package/dist/rules/sort-sets.mjs +47 -0
- package/dist/rules/sort-svelte-attributes.js +6 -0
- package/dist/rules/sort-svelte-attributes.mjs +6 -0
- package/dist/rules/sort-union-types.js +20 -0
- package/dist/rules/sort-union-types.mjs +20 -0
- package/dist/rules/sort-variable-declarations.js +43 -14
- package/dist/rules/sort-variable-declarations.mjs +41 -10
- package/dist/rules/sort-vue-attributes.js +6 -0
- package/dist/rules/sort-vue-attributes.mjs +6 -0
- package/dist/utils/compare.js +0 -6
- package/dist/utils/compare.mjs +0 -6
- package/dist/utils/sort-nodes-by-dependencies.js +30 -0
- package/dist/utils/sort-nodes-by-dependencies.mjs +28 -0
- package/dist/utils/validate-groups-configuration.js +19 -0
- package/dist/utils/validate-groups-configuration.mjs +17 -0
- package/package.json +4 -4
- package/readme.md +1 -0
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { sortNodesByDependencies } from '../utils/sort-nodes-by-dependencies.mjs'
|
|
1
2
|
import { isPartitionComment } from '../utils/is-partition-comment.mjs'
|
|
2
3
|
import { createEslintRule } from '../utils/create-eslint-rule.mjs'
|
|
3
4
|
import { getCommentBefore } from '../utils/get-comment-before.mjs'
|
|
@@ -5,12 +6,10 @@ import { getSourceCode } from '../utils/get-source-code.mjs'
|
|
|
5
6
|
import { toSingleLine } from '../utils/to-single-line.mjs'
|
|
6
7
|
import { rangeToDiff } from '../utils/range-to-diff.mjs'
|
|
7
8
|
import { getSettings } from '../utils/get-settings.mjs'
|
|
8
|
-
import { isPositive } from '../utils/is-positive.mjs'
|
|
9
9
|
import { sortNodes } from '../utils/sort-nodes.mjs'
|
|
10
10
|
import { makeFixes } from '../utils/make-fixes.mjs'
|
|
11
11
|
import { complete } from '../utils/complete.mjs'
|
|
12
12
|
import { pairwise } from '../utils/pairwise.mjs'
|
|
13
|
-
import { compare } from '../utils/compare.mjs'
|
|
14
13
|
const sortEnums = createEslintRule({
|
|
15
14
|
name: 'sort-enums',
|
|
16
15
|
meta: {
|
|
@@ -149,7 +148,7 @@ const sortEnums = createEslintRule({
|
|
|
149
148
|
member.id.type === 'Literal'
|
|
150
149
|
? `${member.id.value}`
|
|
151
150
|
: `${sourceCode.text.slice(...member.id.range)}`
|
|
152
|
-
let dependencies
|
|
151
|
+
let dependencies = []
|
|
153
152
|
if (member.initializer) {
|
|
154
153
|
dependencies = extractDependencies(
|
|
155
154
|
member.initializer,
|
|
@@ -204,8 +203,13 @@ const sortEnums = createEslintRule({
|
|
|
204
203
|
: void 0,
|
|
205
204
|
}
|
|
206
205
|
for (let nodes of formattedMembers) {
|
|
206
|
+
let sortedNodes = sortNodesByDependencies(
|
|
207
|
+
sortNodes(nodes, compareOptions),
|
|
208
|
+
)
|
|
207
209
|
pairwise(nodes, (left, right) => {
|
|
208
|
-
|
|
210
|
+
let indexOfLeft = sortedNodes.indexOf(left)
|
|
211
|
+
let indexOfRight = sortedNodes.indexOf(right)
|
|
212
|
+
if (indexOfLeft > indexOfRight) {
|
|
209
213
|
context.report({
|
|
210
214
|
messageId: 'unexpectedEnumsOrder',
|
|
211
215
|
data: {
|
|
@@ -214,13 +218,9 @@ const sortEnums = createEslintRule({
|
|
|
214
218
|
},
|
|
215
219
|
node: right.node,
|
|
216
220
|
fix: fixer =>
|
|
217
|
-
makeFixes(
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
sortNodes(nodes, compareOptions),
|
|
221
|
-
sourceCode,
|
|
222
|
-
{ partitionComment },
|
|
223
|
-
),
|
|
221
|
+
makeFixes(fixer, nodes, sortedNodes, sourceCode, {
|
|
222
|
+
partitionComment,
|
|
223
|
+
}),
|
|
224
224
|
})
|
|
225
225
|
}
|
|
226
226
|
})
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
const node_module = require('node:module')
|
|
3
3
|
const minimatch = require('minimatch')
|
|
4
|
+
const validateGroupsConfiguration = require('../utils/validate-groups-configuration.js')
|
|
4
5
|
const getCommentBefore = require('../utils/get-comment-before.js')
|
|
5
6
|
const createEslintRule = require('../utils/create-eslint-rule.js')
|
|
6
7
|
const getLinesBetween = require('../utils/get-lines-between.js')
|
|
@@ -51,6 +52,11 @@ const sortImports = createEslintRule.createEslintRule({
|
|
|
51
52
|
},
|
|
52
53
|
type: 'array',
|
|
53
54
|
},
|
|
55
|
+
sortSideEffects: {
|
|
56
|
+
description:
|
|
57
|
+
'Controls whether side-effect imports should be sorted.',
|
|
58
|
+
type: 'boolean',
|
|
59
|
+
},
|
|
54
60
|
newlinesBetween: {
|
|
55
61
|
description:
|
|
56
62
|
'Specifies how new lines should be handled between import groups.',
|
|
@@ -147,6 +153,7 @@ const sortImports = createEslintRule.createEslintRule({
|
|
|
147
153
|
order: 'asc',
|
|
148
154
|
ignoreCase: true,
|
|
149
155
|
internalPattern: ['~/**'],
|
|
156
|
+
sortSideEffects: false,
|
|
150
157
|
newlinesBetween: 'always',
|
|
151
158
|
maxLineLength: void 0,
|
|
152
159
|
groups: [
|
|
@@ -179,6 +186,7 @@ const sortImports = createEslintRule.createEslintRule({
|
|
|
179
186
|
customGroups: { type: {}, value: {} },
|
|
180
187
|
internalPattern: ['~/**'],
|
|
181
188
|
newlinesBetween: 'always',
|
|
189
|
+
sortSideEffects: false,
|
|
182
190
|
type: 'alphabetical',
|
|
183
191
|
environment: 'node',
|
|
184
192
|
ignoreCase: true,
|
|
@@ -202,6 +210,33 @@ const sortImports = createEslintRule.createEslintRule({
|
|
|
202
210
|
if (!hasUnknownGroup) {
|
|
203
211
|
options.groups = [...options.groups, 'unknown']
|
|
204
212
|
}
|
|
213
|
+
validateGroupsConfiguration.validateGroupsConfiguration(
|
|
214
|
+
options.groups,
|
|
215
|
+
[
|
|
216
|
+
'side-effect-style',
|
|
217
|
+
'external-type',
|
|
218
|
+
'internal-type',
|
|
219
|
+
'builtin-type',
|
|
220
|
+
'sibling-type',
|
|
221
|
+
'parent-type',
|
|
222
|
+
'side-effect',
|
|
223
|
+
'index-type',
|
|
224
|
+
'internal',
|
|
225
|
+
'external',
|
|
226
|
+
'sibling',
|
|
227
|
+
'unknown',
|
|
228
|
+
'builtin',
|
|
229
|
+
'parent',
|
|
230
|
+
'object',
|
|
231
|
+
'index',
|
|
232
|
+
'style',
|
|
233
|
+
'type',
|
|
234
|
+
],
|
|
235
|
+
[
|
|
236
|
+
...Object.keys(options.customGroups.type ?? {}),
|
|
237
|
+
...Object.keys(options.customGroups.value ?? {}),
|
|
238
|
+
],
|
|
239
|
+
)
|
|
205
240
|
let nodes = []
|
|
206
241
|
let isSideEffectImport = node =>
|
|
207
242
|
node.type === 'ImportDeclaration' &&
|
|
@@ -390,10 +425,14 @@ const sortImports = createEslintRule.createEslintRule({
|
|
|
390
425
|
if (!(groupNum in grouped)) {
|
|
391
426
|
grouped[groupNum] = [node]
|
|
392
427
|
} else {
|
|
393
|
-
|
|
394
|
-
[...grouped[groupNum], node]
|
|
395
|
-
|
|
396
|
-
|
|
428
|
+
if (!options.sortSideEffects && isSideEffectImport(node.node)) {
|
|
429
|
+
grouped[groupNum] = [...grouped[groupNum], node]
|
|
430
|
+
} else {
|
|
431
|
+
grouped[groupNum] = sortNodes.sortNodes(
|
|
432
|
+
[...grouped[groupNum], node],
|
|
433
|
+
options,
|
|
434
|
+
)
|
|
435
|
+
}
|
|
397
436
|
}
|
|
398
437
|
}
|
|
399
438
|
let formatted = Object.keys(grouped)
|
|
@@ -501,7 +540,9 @@ const sortImports = createEslintRule.createEslintRule({
|
|
|
501
540
|
)
|
|
502
541
|
if (
|
|
503
542
|
!(
|
|
504
|
-
|
|
543
|
+
!options.sortSideEffects &&
|
|
544
|
+
isSideEffectImport(left.node) &&
|
|
545
|
+
isSideEffectImport(right.node)
|
|
505
546
|
) &&
|
|
506
547
|
!hasContentBetweenNodes(left, right) &&
|
|
507
548
|
(leftNum > rightNum ||
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { builtinModules } from 'node:module'
|
|
2
2
|
import { minimatch } from 'minimatch'
|
|
3
|
+
import { validateGroupsConfiguration } from '../utils/validate-groups-configuration.mjs'
|
|
3
4
|
import { getCommentBefore } from '../utils/get-comment-before.mjs'
|
|
4
5
|
import { createEslintRule } from '../utils/create-eslint-rule.mjs'
|
|
5
6
|
import { getLinesBetween } from '../utils/get-lines-between.mjs'
|
|
@@ -50,6 +51,11 @@ const sortImports = createEslintRule({
|
|
|
50
51
|
},
|
|
51
52
|
type: 'array',
|
|
52
53
|
},
|
|
54
|
+
sortSideEffects: {
|
|
55
|
+
description:
|
|
56
|
+
'Controls whether side-effect imports should be sorted.',
|
|
57
|
+
type: 'boolean',
|
|
58
|
+
},
|
|
53
59
|
newlinesBetween: {
|
|
54
60
|
description:
|
|
55
61
|
'Specifies how new lines should be handled between import groups.',
|
|
@@ -146,6 +152,7 @@ const sortImports = createEslintRule({
|
|
|
146
152
|
order: 'asc',
|
|
147
153
|
ignoreCase: true,
|
|
148
154
|
internalPattern: ['~/**'],
|
|
155
|
+
sortSideEffects: false,
|
|
149
156
|
newlinesBetween: 'always',
|
|
150
157
|
maxLineLength: void 0,
|
|
151
158
|
groups: [
|
|
@@ -178,6 +185,7 @@ const sortImports = createEslintRule({
|
|
|
178
185
|
customGroups: { type: {}, value: {} },
|
|
179
186
|
internalPattern: ['~/**'],
|
|
180
187
|
newlinesBetween: 'always',
|
|
188
|
+
sortSideEffects: false,
|
|
181
189
|
type: 'alphabetical',
|
|
182
190
|
environment: 'node',
|
|
183
191
|
ignoreCase: true,
|
|
@@ -201,6 +209,33 @@ const sortImports = createEslintRule({
|
|
|
201
209
|
if (!hasUnknownGroup) {
|
|
202
210
|
options.groups = [...options.groups, 'unknown']
|
|
203
211
|
}
|
|
212
|
+
validateGroupsConfiguration(
|
|
213
|
+
options.groups,
|
|
214
|
+
[
|
|
215
|
+
'side-effect-style',
|
|
216
|
+
'external-type',
|
|
217
|
+
'internal-type',
|
|
218
|
+
'builtin-type',
|
|
219
|
+
'sibling-type',
|
|
220
|
+
'parent-type',
|
|
221
|
+
'side-effect',
|
|
222
|
+
'index-type',
|
|
223
|
+
'internal',
|
|
224
|
+
'external',
|
|
225
|
+
'sibling',
|
|
226
|
+
'unknown',
|
|
227
|
+
'builtin',
|
|
228
|
+
'parent',
|
|
229
|
+
'object',
|
|
230
|
+
'index',
|
|
231
|
+
'style',
|
|
232
|
+
'type',
|
|
233
|
+
],
|
|
234
|
+
[
|
|
235
|
+
...Object.keys(options.customGroups.type ?? {}),
|
|
236
|
+
...Object.keys(options.customGroups.value ?? {}),
|
|
237
|
+
],
|
|
238
|
+
)
|
|
204
239
|
let nodes = []
|
|
205
240
|
let isSideEffectImport = node =>
|
|
206
241
|
node.type === 'ImportDeclaration' &&
|
|
@@ -384,10 +419,14 @@ const sortImports = createEslintRule({
|
|
|
384
419
|
if (!(groupNum in grouped)) {
|
|
385
420
|
grouped[groupNum] = [node]
|
|
386
421
|
} else {
|
|
387
|
-
|
|
388
|
-
[...grouped[groupNum], node]
|
|
389
|
-
|
|
390
|
-
|
|
422
|
+
if (!options.sortSideEffects && isSideEffectImport(node.node)) {
|
|
423
|
+
grouped[groupNum] = [...grouped[groupNum], node]
|
|
424
|
+
} else {
|
|
425
|
+
grouped[groupNum] = sortNodes(
|
|
426
|
+
[...grouped[groupNum], node],
|
|
427
|
+
options,
|
|
428
|
+
)
|
|
429
|
+
}
|
|
391
430
|
}
|
|
392
431
|
}
|
|
393
432
|
let formatted = Object.keys(grouped)
|
|
@@ -486,7 +525,9 @@ const sortImports = createEslintRule({
|
|
|
486
525
|
)
|
|
487
526
|
if (
|
|
488
527
|
!(
|
|
489
|
-
|
|
528
|
+
!options.sortSideEffects &&
|
|
529
|
+
isSideEffectImport(left.node) &&
|
|
530
|
+
isSideEffectImport(right.node)
|
|
490
531
|
) &&
|
|
491
532
|
!hasContentBetweenNodes(left, right) &&
|
|
492
533
|
(leftNum > rightNum ||
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
const minimatch = require('minimatch')
|
|
3
|
+
const validateGroupsConfiguration = require('../utils/validate-groups-configuration.js')
|
|
3
4
|
const createEslintRule = require('../utils/create-eslint-rule.js')
|
|
4
5
|
const isMemberOptional = require('../utils/is-member-optional.js')
|
|
5
6
|
const getLinesBetween = require('../utils/get-lines-between.js')
|
|
@@ -130,6 +131,11 @@ const sortInterfaces = createEslintRule.createEslintRule({
|
|
|
130
131
|
order: 'asc',
|
|
131
132
|
groups: [],
|
|
132
133
|
})
|
|
134
|
+
validateGroupsConfiguration.validateGroupsConfiguration(
|
|
135
|
+
options.groups,
|
|
136
|
+
['multiline', 'unknown'],
|
|
137
|
+
Object.keys(options.customGroups),
|
|
138
|
+
)
|
|
133
139
|
let sourceCode = getSourceCode.getSourceCode(context)
|
|
134
140
|
if (
|
|
135
141
|
!options.ignorePattern.some(pattern =>
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { minimatch } from 'minimatch'
|
|
2
|
+
import { validateGroupsConfiguration } from '../utils/validate-groups-configuration.mjs'
|
|
2
3
|
import { createEslintRule } from '../utils/create-eslint-rule.mjs'
|
|
3
4
|
import { isMemberOptional } from '../utils/is-member-optional.mjs'
|
|
4
5
|
import { getLinesBetween } from '../utils/get-lines-between.mjs'
|
|
@@ -129,6 +130,11 @@ const sortInterfaces = createEslintRule({
|
|
|
129
130
|
order: 'asc',
|
|
130
131
|
groups: [],
|
|
131
132
|
})
|
|
133
|
+
validateGroupsConfiguration(
|
|
134
|
+
options.groups,
|
|
135
|
+
['multiline', 'unknown'],
|
|
136
|
+
Object.keys(options.customGroups),
|
|
137
|
+
)
|
|
132
138
|
let sourceCode = getSourceCode(context)
|
|
133
139
|
if (
|
|
134
140
|
!options.ignorePattern.some(pattern =>
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
'use strict'
|
|
2
|
+
const validateGroupsConfiguration = require('../utils/validate-groups-configuration.js')
|
|
2
3
|
const createEslintRule = require('../utils/create-eslint-rule.js')
|
|
3
4
|
const getGroupNumber = require('../utils/get-group-number.js')
|
|
4
5
|
const getSourceCode = require('../utils/get-source-code.js')
|
|
@@ -83,6 +84,25 @@ const sortIntersectionTypes = createEslintRule.createEslintRule({
|
|
|
83
84
|
order: 'asc',
|
|
84
85
|
groups: [],
|
|
85
86
|
})
|
|
87
|
+
validateGroupsConfiguration.validateGroupsConfiguration(
|
|
88
|
+
options.groups,
|
|
89
|
+
[
|
|
90
|
+
'intersection',
|
|
91
|
+
'conditional',
|
|
92
|
+
'function',
|
|
93
|
+
'operator',
|
|
94
|
+
'keyword',
|
|
95
|
+
'literal',
|
|
96
|
+
'nullish',
|
|
97
|
+
'unknown',
|
|
98
|
+
'import',
|
|
99
|
+
'object',
|
|
100
|
+
'named',
|
|
101
|
+
'tuple',
|
|
102
|
+
'union',
|
|
103
|
+
],
|
|
104
|
+
[],
|
|
105
|
+
)
|
|
86
106
|
let sourceCode = getSourceCode.getSourceCode(context)
|
|
87
107
|
let nodes = node.types.map(type => {
|
|
88
108
|
let { getGroup, defineGroup } = useGroups.useGroups(options.groups)
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { validateGroupsConfiguration } from '../utils/validate-groups-configuration.mjs'
|
|
1
2
|
import { createEslintRule } from '../utils/create-eslint-rule.mjs'
|
|
2
3
|
import { getGroupNumber } from '../utils/get-group-number.mjs'
|
|
3
4
|
import { getSourceCode } from '../utils/get-source-code.mjs'
|
|
@@ -82,6 +83,25 @@ const sortIntersectionTypes = createEslintRule({
|
|
|
82
83
|
order: 'asc',
|
|
83
84
|
groups: [],
|
|
84
85
|
})
|
|
86
|
+
validateGroupsConfiguration(
|
|
87
|
+
options.groups,
|
|
88
|
+
[
|
|
89
|
+
'intersection',
|
|
90
|
+
'conditional',
|
|
91
|
+
'function',
|
|
92
|
+
'operator',
|
|
93
|
+
'keyword',
|
|
94
|
+
'literal',
|
|
95
|
+
'nullish',
|
|
96
|
+
'unknown',
|
|
97
|
+
'import',
|
|
98
|
+
'object',
|
|
99
|
+
'named',
|
|
100
|
+
'tuple',
|
|
101
|
+
'union',
|
|
102
|
+
],
|
|
103
|
+
[],
|
|
104
|
+
)
|
|
85
105
|
let sourceCode = getSourceCode(context)
|
|
86
106
|
let nodes = node.types.map(type => {
|
|
87
107
|
let { getGroup, defineGroup } = useGroups(options.groups)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
const minimatch = require('minimatch')
|
|
3
|
+
const validateGroupsConfiguration = require('../utils/validate-groups-configuration.js')
|
|
3
4
|
const createEslintRule = require('../utils/create-eslint-rule.js')
|
|
4
5
|
const getGroupNumber = require('../utils/get-group-number.js')
|
|
5
6
|
const getSourceCode = require('../utils/get-source-code.js')
|
|
@@ -113,6 +114,11 @@ const sortJsxProps = createEslintRule.createEslintRule({
|
|
|
113
114
|
order: 'asc',
|
|
114
115
|
groups: [],
|
|
115
116
|
})
|
|
117
|
+
validateGroupsConfiguration.validateGroupsConfiguration(
|
|
118
|
+
options.groups,
|
|
119
|
+
['multiline', 'shorthand', 'unknown'],
|
|
120
|
+
Object.keys(options.customGroups),
|
|
121
|
+
)
|
|
116
122
|
let sourceCode = getSourceCode.getSourceCode(context)
|
|
117
123
|
let shouldIgnore = false
|
|
118
124
|
if (options.ignorePattern.length) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { minimatch } from 'minimatch'
|
|
2
|
+
import { validateGroupsConfiguration } from '../utils/validate-groups-configuration.mjs'
|
|
2
3
|
import { createEslintRule } from '../utils/create-eslint-rule.mjs'
|
|
3
4
|
import { getGroupNumber } from '../utils/get-group-number.mjs'
|
|
4
5
|
import { getSourceCode } from '../utils/get-source-code.mjs'
|
|
@@ -112,6 +113,11 @@ const sortJsxProps = createEslintRule({
|
|
|
112
113
|
order: 'asc',
|
|
113
114
|
groups: [],
|
|
114
115
|
})
|
|
116
|
+
validateGroupsConfiguration(
|
|
117
|
+
options.groups,
|
|
118
|
+
['multiline', 'shorthand', 'unknown'],
|
|
119
|
+
Object.keys(options.customGroups),
|
|
120
|
+
)
|
|
115
121
|
let sourceCode = getSourceCode(context)
|
|
116
122
|
let shouldIgnore = false
|
|
117
123
|
if (options.ignorePattern.length) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
'use strict'
|
|
2
|
+
const validateGroupsConfiguration = require('../utils/validate-groups-configuration.js')
|
|
2
3
|
const createEslintRule = require('../utils/create-eslint-rule.js')
|
|
3
4
|
const getLinesBetween = require('../utils/get-lines-between.js')
|
|
4
5
|
const getGroupNumber = require('../utils/get-group-number.js')
|
|
@@ -118,6 +119,11 @@ const sortObjectTypes = createEslintRule.createEslintRule({
|
|
|
118
119
|
order: 'asc',
|
|
119
120
|
groups: [],
|
|
120
121
|
})
|
|
122
|
+
validateGroupsConfiguration.validateGroupsConfiguration(
|
|
123
|
+
options.groups,
|
|
124
|
+
['multiline', 'unknown'],
|
|
125
|
+
Object.keys(options.customGroups),
|
|
126
|
+
)
|
|
121
127
|
let sourceCode = getSourceCode.getSourceCode(context)
|
|
122
128
|
let formattedMembers = node.members.reduce(
|
|
123
129
|
(accumulator, member) => {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { validateGroupsConfiguration } from '../utils/validate-groups-configuration.mjs'
|
|
1
2
|
import { createEslintRule } from '../utils/create-eslint-rule.mjs'
|
|
2
3
|
import { getLinesBetween } from '../utils/get-lines-between.mjs'
|
|
3
4
|
import { getGroupNumber } from '../utils/get-group-number.mjs'
|
|
@@ -117,6 +118,11 @@ const sortObjectTypes = createEslintRule({
|
|
|
117
118
|
order: 'asc',
|
|
118
119
|
groups: [],
|
|
119
120
|
})
|
|
121
|
+
validateGroupsConfiguration(
|
|
122
|
+
options.groups,
|
|
123
|
+
['multiline', 'unknown'],
|
|
124
|
+
Object.keys(options.customGroups),
|
|
125
|
+
)
|
|
120
126
|
let sourceCode = getSourceCode(context)
|
|
121
127
|
let formattedMembers = node.members.reduce(
|
|
122
128
|
(accumulator, member) => {
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
const minimatch = require('minimatch')
|
|
3
|
+
const validateGroupsConfiguration = require('../utils/validate-groups-configuration.js')
|
|
4
|
+
const sortNodesByDependencies = require('../utils/sort-nodes-by-dependencies.js')
|
|
3
5
|
const isPartitionComment = require('../utils/is-partition-comment.js')
|
|
4
6
|
const getCommentBefore = require('../utils/get-comment-before.js')
|
|
5
7
|
const createEslintRule = require('../utils/create-eslint-rule.js')
|
|
@@ -10,13 +12,11 @@ const getNodeParent = require('../utils/get-node-parent.js')
|
|
|
10
12
|
const toSingleLine = require('../utils/to-single-line.js')
|
|
11
13
|
const rangeToDiff = require('../utils/range-to-diff.js')
|
|
12
14
|
const getSettings = require('../utils/get-settings.js')
|
|
13
|
-
const isPositive = require('../utils/is-positive.js')
|
|
14
15
|
const useGroups = require('../utils/use-groups.js')
|
|
15
16
|
const makeFixes = require('../utils/make-fixes.js')
|
|
16
17
|
const sortNodes = require('../utils/sort-nodes.js')
|
|
17
18
|
const complete = require('../utils/complete.js')
|
|
18
19
|
const pairwise = require('../utils/pairwise.js')
|
|
19
|
-
const compare = require('../utils/compare.js')
|
|
20
20
|
const sortObjects = createEslintRule.createEslintRule({
|
|
21
21
|
name: 'sort-objects',
|
|
22
22
|
meta: {
|
|
@@ -155,6 +155,11 @@ const sortObjects = createEslintRule.createEslintRule({
|
|
|
155
155
|
order: 'asc',
|
|
156
156
|
groups: [],
|
|
157
157
|
})
|
|
158
|
+
validateGroupsConfiguration.validateGroupsConfiguration(
|
|
159
|
+
options.groups,
|
|
160
|
+
['unknown'],
|
|
161
|
+
Object.keys(options.customGroups),
|
|
162
|
+
)
|
|
158
163
|
let shouldIgnore = false
|
|
159
164
|
if (options.destructureOnly) {
|
|
160
165
|
shouldIgnore = node.type !== 'ObjectPattern'
|
|
@@ -327,48 +332,34 @@ const sortObjects = createEslintRule.createEslintRule({
|
|
|
327
332
|
[[]],
|
|
328
333
|
)
|
|
329
334
|
for (let nodes of formatProperties(node.properties)) {
|
|
335
|
+
let grouped = {}
|
|
336
|
+
for (let currentNode of nodes) {
|
|
337
|
+
let groupNum = getGroupNumber.getGroupNumber(
|
|
338
|
+
options.groups,
|
|
339
|
+
currentNode,
|
|
340
|
+
)
|
|
341
|
+
if (!(groupNum in grouped)) {
|
|
342
|
+
grouped[groupNum] = [currentNode]
|
|
343
|
+
} else {
|
|
344
|
+
grouped[groupNum].push(currentNode)
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
let sortedNodes = []
|
|
348
|
+
for (let group of Object.keys(grouped).sort(
|
|
349
|
+
(a, b) => Number(a) - Number(b),
|
|
350
|
+
)) {
|
|
351
|
+
sortedNodes.push(...sortNodes.sortNodes(grouped[group], options))
|
|
352
|
+
}
|
|
353
|
+
sortedNodes =
|
|
354
|
+
sortNodesByDependencies.sortNodesByDependencies(sortedNodes)
|
|
330
355
|
pairwise.pairwise(nodes, (left, right) => {
|
|
331
|
-
let
|
|
332
|
-
let
|
|
333
|
-
if (
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
let fix = fixer => {
|
|
339
|
-
let grouped = {}
|
|
340
|
-
for (let currentNode of nodes) {
|
|
341
|
-
let groupNum = getGroupNumber.getGroupNumber(
|
|
342
|
-
options.groups,
|
|
343
|
-
currentNode,
|
|
344
|
-
)
|
|
345
|
-
if (!(groupNum in grouped)) {
|
|
346
|
-
grouped[groupNum] = [currentNode]
|
|
347
|
-
} else {
|
|
348
|
-
grouped[groupNum] = sortNodes.sortNodes(
|
|
349
|
-
[...grouped[groupNum], currentNode],
|
|
350
|
-
options,
|
|
351
|
-
)
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
let sortedNodes = []
|
|
355
|
-
for (let group of Object.keys(grouped).sort(
|
|
356
|
-
(a, b) => Number(a) - Number(b),
|
|
357
|
-
)) {
|
|
358
|
-
sortedNodes.push(
|
|
359
|
-
...sortNodes.sortNodes(grouped[group], options),
|
|
360
|
-
)
|
|
361
|
-
}
|
|
362
|
-
return makeFixes.makeFixes(
|
|
363
|
-
fixer,
|
|
364
|
-
nodes,
|
|
365
|
-
sortedNodes,
|
|
366
|
-
sourceCode,
|
|
367
|
-
{
|
|
368
|
-
partitionComment: options.partitionByComment,
|
|
369
|
-
},
|
|
370
|
-
)
|
|
371
|
-
}
|
|
356
|
+
let indexOfLeft = sortedNodes.indexOf(left)
|
|
357
|
+
let indexOfRight = sortedNodes.indexOf(right)
|
|
358
|
+
if (indexOfLeft > indexOfRight) {
|
|
359
|
+
let fix = fixer =>
|
|
360
|
+
makeFixes.makeFixes(fixer, nodes, sortedNodes, sourceCode, {
|
|
361
|
+
partitionComment: options.partitionByComment,
|
|
362
|
+
})
|
|
372
363
|
context.report({
|
|
373
364
|
messageId: 'unexpectedObjectsOrder',
|
|
374
365
|
data: {
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { minimatch } from 'minimatch'
|
|
2
|
+
import { validateGroupsConfiguration } from '../utils/validate-groups-configuration.mjs'
|
|
3
|
+
import { sortNodesByDependencies } from '../utils/sort-nodes-by-dependencies.mjs'
|
|
2
4
|
import { isPartitionComment } from '../utils/is-partition-comment.mjs'
|
|
3
5
|
import { getCommentBefore } from '../utils/get-comment-before.mjs'
|
|
4
6
|
import { createEslintRule } from '../utils/create-eslint-rule.mjs'
|
|
@@ -9,13 +11,11 @@ import { getNodeParent } from '../utils/get-node-parent.mjs'
|
|
|
9
11
|
import { toSingleLine } from '../utils/to-single-line.mjs'
|
|
10
12
|
import { rangeToDiff } from '../utils/range-to-diff.mjs'
|
|
11
13
|
import { getSettings } from '../utils/get-settings.mjs'
|
|
12
|
-
import { isPositive } from '../utils/is-positive.mjs'
|
|
13
14
|
import { useGroups } from '../utils/use-groups.mjs'
|
|
14
15
|
import { makeFixes } from '../utils/make-fixes.mjs'
|
|
15
16
|
import { sortNodes } from '../utils/sort-nodes.mjs'
|
|
16
17
|
import { complete } from '../utils/complete.mjs'
|
|
17
18
|
import { pairwise } from '../utils/pairwise.mjs'
|
|
18
|
-
import { compare } from '../utils/compare.mjs'
|
|
19
19
|
const sortObjects = createEslintRule({
|
|
20
20
|
name: 'sort-objects',
|
|
21
21
|
meta: {
|
|
@@ -154,6 +154,11 @@ const sortObjects = createEslintRule({
|
|
|
154
154
|
order: 'asc',
|
|
155
155
|
groups: [],
|
|
156
156
|
})
|
|
157
|
+
validateGroupsConfiguration(
|
|
158
|
+
options.groups,
|
|
159
|
+
['unknown'],
|
|
160
|
+
Object.keys(options.customGroups),
|
|
161
|
+
)
|
|
157
162
|
let shouldIgnore = false
|
|
158
163
|
if (options.destructureOnly) {
|
|
159
164
|
shouldIgnore = node.type !== 'ObjectPattern'
|
|
@@ -314,37 +319,30 @@ const sortObjects = createEslintRule({
|
|
|
314
319
|
[[]],
|
|
315
320
|
)
|
|
316
321
|
for (let nodes of formatProperties(node.properties)) {
|
|
322
|
+
let grouped = {}
|
|
323
|
+
for (let currentNode of nodes) {
|
|
324
|
+
let groupNum = getGroupNumber(options.groups, currentNode)
|
|
325
|
+
if (!(groupNum in grouped)) {
|
|
326
|
+
grouped[groupNum] = [currentNode]
|
|
327
|
+
} else {
|
|
328
|
+
grouped[groupNum].push(currentNode)
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
let sortedNodes = []
|
|
332
|
+
for (let group of Object.keys(grouped).sort(
|
|
333
|
+
(a, b) => Number(a) - Number(b),
|
|
334
|
+
)) {
|
|
335
|
+
sortedNodes.push(...sortNodes(grouped[group], options))
|
|
336
|
+
}
|
|
337
|
+
sortedNodes = sortNodesByDependencies(sortedNodes)
|
|
317
338
|
pairwise(nodes, (left, right) => {
|
|
318
|
-
let
|
|
319
|
-
let
|
|
320
|
-
if (
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
isPositive(compare(left, right, options)))
|
|
324
|
-
) {
|
|
325
|
-
let fix = fixer => {
|
|
326
|
-
let grouped = {}
|
|
327
|
-
for (let currentNode of nodes) {
|
|
328
|
-
let groupNum = getGroupNumber(options.groups, currentNode)
|
|
329
|
-
if (!(groupNum in grouped)) {
|
|
330
|
-
grouped[groupNum] = [currentNode]
|
|
331
|
-
} else {
|
|
332
|
-
grouped[groupNum] = sortNodes(
|
|
333
|
-
[...grouped[groupNum], currentNode],
|
|
334
|
-
options,
|
|
335
|
-
)
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
let sortedNodes = []
|
|
339
|
-
for (let group of Object.keys(grouped).sort(
|
|
340
|
-
(a, b) => Number(a) - Number(b),
|
|
341
|
-
)) {
|
|
342
|
-
sortedNodes.push(...sortNodes(grouped[group], options))
|
|
343
|
-
}
|
|
344
|
-
return makeFixes(fixer, nodes, sortedNodes, sourceCode, {
|
|
339
|
+
let indexOfLeft = sortedNodes.indexOf(left)
|
|
340
|
+
let indexOfRight = sortedNodes.indexOf(right)
|
|
341
|
+
if (indexOfLeft > indexOfRight) {
|
|
342
|
+
let fix = fixer =>
|
|
343
|
+
makeFixes(fixer, nodes, sortedNodes, sourceCode, {
|
|
345
344
|
partitionComment: options.partitionByComment,
|
|
346
345
|
})
|
|
347
|
-
}
|
|
348
346
|
context.report({
|
|
349
347
|
messageId: 'unexpectedObjectsOrder',
|
|
350
348
|
data: {
|