eslint-plugin-perfectionist 3.2.0 → 3.3.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/rules/sort-classes-utils.js +31 -0
- package/dist/rules/sort-classes-utils.mjs +31 -1
- package/dist/rules/sort-classes.js +56 -28
- package/dist/rules/sort-classes.mjs +60 -28
- package/dist/rules/sort-enums.js +31 -0
- package/dist/rules/sort-enums.mjs +31 -0
- package/dist/rules/sort-switch-case.js +6 -3
- package/dist/rules/sort-switch-case.mjs +6 -3
- package/package.json +3 -3
|
@@ -65,4 +65,35 @@ const getPermutations = elements => {
|
|
|
65
65
|
backtrack(0)
|
|
66
66
|
return result
|
|
67
67
|
}
|
|
68
|
+
const getOverloadSignatureGroups = members => {
|
|
69
|
+
let methods = members
|
|
70
|
+
.filter(
|
|
71
|
+
member =>
|
|
72
|
+
member.type === 'MethodDefinition' ||
|
|
73
|
+
member.type === 'TSAbstractMethodDefinition',
|
|
74
|
+
)
|
|
75
|
+
.filter(member => member.kind === 'method')
|
|
76
|
+
let staticOverloadSignaturesByName = /* @__PURE__ */ new Map()
|
|
77
|
+
let overloadSignaturesByName = /* @__PURE__ */ new Map()
|
|
78
|
+
for (let method of methods) {
|
|
79
|
+
if (method.key.type !== 'Identifier') {
|
|
80
|
+
continue
|
|
81
|
+
}
|
|
82
|
+
let { name } = method.key
|
|
83
|
+
let mapToUse = method.static
|
|
84
|
+
? staticOverloadSignaturesByName
|
|
85
|
+
: overloadSignaturesByName
|
|
86
|
+
let signatureOverloadsGroup = mapToUse.get(name)
|
|
87
|
+
if (!signatureOverloadsGroup) {
|
|
88
|
+
signatureOverloadsGroup = []
|
|
89
|
+
mapToUse.set(name, signatureOverloadsGroup)
|
|
90
|
+
}
|
|
91
|
+
signatureOverloadsGroup.push(method)
|
|
92
|
+
}
|
|
93
|
+
return [
|
|
94
|
+
...overloadSignaturesByName.values(),
|
|
95
|
+
...staticOverloadSignaturesByName.values(),
|
|
96
|
+
].filter(group => group.length > 1)
|
|
97
|
+
}
|
|
68
98
|
exports.generateOfficialGroups = generateOfficialGroups
|
|
99
|
+
exports.getOverloadSignatureGroups = getOverloadSignatureGroups
|
|
@@ -63,4 +63,34 @@ const getPermutations = elements => {
|
|
|
63
63
|
backtrack(0)
|
|
64
64
|
return result
|
|
65
65
|
}
|
|
66
|
-
|
|
66
|
+
const getOverloadSignatureGroups = members => {
|
|
67
|
+
let methods = members
|
|
68
|
+
.filter(
|
|
69
|
+
member =>
|
|
70
|
+
member.type === 'MethodDefinition' ||
|
|
71
|
+
member.type === 'TSAbstractMethodDefinition',
|
|
72
|
+
)
|
|
73
|
+
.filter(member => member.kind === 'method')
|
|
74
|
+
let staticOverloadSignaturesByName = /* @__PURE__ */ new Map()
|
|
75
|
+
let overloadSignaturesByName = /* @__PURE__ */ new Map()
|
|
76
|
+
for (let method of methods) {
|
|
77
|
+
if (method.key.type !== 'Identifier') {
|
|
78
|
+
continue
|
|
79
|
+
}
|
|
80
|
+
let { name } = method.key
|
|
81
|
+
let mapToUse = method.static
|
|
82
|
+
? staticOverloadSignaturesByName
|
|
83
|
+
: overloadSignaturesByName
|
|
84
|
+
let signatureOverloadsGroup = mapToUse.get(name)
|
|
85
|
+
if (!signatureOverloadsGroup) {
|
|
86
|
+
signatureOverloadsGroup = []
|
|
87
|
+
mapToUse.set(name, signatureOverloadsGroup)
|
|
88
|
+
}
|
|
89
|
+
signatureOverloadsGroup.push(method)
|
|
90
|
+
}
|
|
91
|
+
return [
|
|
92
|
+
...overloadSignaturesByName.values(),
|
|
93
|
+
...staticOverloadSignaturesByName.values(),
|
|
94
|
+
].filter(group => group.length > 1)
|
|
95
|
+
}
|
|
96
|
+
export { generateOfficialGroups, getOverloadSignatureGroups }
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict'
|
|
2
|
+
const sortClassesUtils = require('./sort-classes-utils.js')
|
|
2
3
|
const isPartitionComment = require('../utils/is-partition-comment.js')
|
|
3
4
|
const getCommentBefore = require('../utils/get-comment-before.js')
|
|
4
5
|
const createEslintRule = require('../utils/create-eslint-rule.js')
|
|
5
|
-
const sortClassesUtils = require('./sort-classes-utils.js')
|
|
6
6
|
const getGroupNumber = require('../utils/get-group-number.js')
|
|
7
7
|
const getSourceCode = require('../utils/get-source-code.js')
|
|
8
8
|
const toSingleLine = require('../utils/to-single-line.js')
|
|
@@ -112,11 +112,12 @@ const sortClasses = createEslintRule.createEslintRule({
|
|
|
112
112
|
ignoreCase: true,
|
|
113
113
|
partitionByComment: false,
|
|
114
114
|
groups: [
|
|
115
|
+
'static-block',
|
|
115
116
|
'index-signature',
|
|
116
117
|
'static-property',
|
|
117
|
-
'protected-property',
|
|
118
|
-
'private-property',
|
|
119
|
-
'property',
|
|
118
|
+
['protected-property', 'protected-accessor-property'],
|
|
119
|
+
['private-property', 'private-accessor-property'],
|
|
120
|
+
['property', 'accessor-property'],
|
|
120
121
|
'constructor',
|
|
121
122
|
'static-method',
|
|
122
123
|
'protected-method',
|
|
@@ -134,11 +135,12 @@ const sortClasses = createEslintRule.createEslintRule({
|
|
|
134
135
|
let settings = getSettings.getSettings(context.settings)
|
|
135
136
|
let options = complete.complete(context.options.at(0), settings, {
|
|
136
137
|
groups: [
|
|
138
|
+
'static-block',
|
|
137
139
|
'index-signature',
|
|
138
140
|
'static-property',
|
|
139
|
-
'protected-property',
|
|
140
|
-
'private-property',
|
|
141
|
-
'property',
|
|
141
|
+
['protected-property', 'protected-accessor-property'],
|
|
142
|
+
['private-property', 'private-accessor-property'],
|
|
143
|
+
['property', 'accessor-property'],
|
|
142
144
|
'constructor',
|
|
143
145
|
'static-method',
|
|
144
146
|
'protected-method',
|
|
@@ -192,9 +194,11 @@ const sortClasses = createEslintRule.createEslintRule({
|
|
|
192
194
|
traverseNode(expression)
|
|
193
195
|
return dependencies
|
|
194
196
|
}
|
|
197
|
+
let overloadSignatureGroups =
|
|
198
|
+
sortClassesUtils.getOverloadSignatureGroups(node.body)
|
|
195
199
|
let formattedNodes = node.body.reduce(
|
|
196
200
|
(accumulator, member) => {
|
|
197
|
-
var _a, _b, _c
|
|
201
|
+
var _a, _b, _c, _d
|
|
198
202
|
let comment = getCommentBefore.getCommentBefore(member, sourceCode)
|
|
199
203
|
if (
|
|
200
204
|
options.partitionByComment &&
|
|
@@ -226,7 +230,8 @@ const sortClasses = createEslintRule.createEslintRule({
|
|
|
226
230
|
name = sourceCode.text.slice(...member.key.range)
|
|
227
231
|
}
|
|
228
232
|
}
|
|
229
|
-
let
|
|
233
|
+
let isPrivateHash =
|
|
234
|
+
'key' in member && member.key.type === 'PrivateIdentifier'
|
|
230
235
|
let decorated =
|
|
231
236
|
'decorators' in member && member.decorators.length > 0
|
|
232
237
|
let modifiers = []
|
|
@@ -249,11 +254,14 @@ const sortClasses = createEslintRule.createEslintRule({
|
|
|
249
254
|
}
|
|
250
255
|
if (member.accessibility === 'protected') {
|
|
251
256
|
modifiers.push('protected')
|
|
252
|
-
} else if (member.accessibility === 'private' ||
|
|
257
|
+
} else if (member.accessibility === 'private' || isPrivateHash) {
|
|
253
258
|
modifiers.push('private')
|
|
254
259
|
} else {
|
|
255
260
|
modifiers.push('public')
|
|
256
261
|
}
|
|
262
|
+
if (member.optional) {
|
|
263
|
+
modifiers.push('optional')
|
|
264
|
+
}
|
|
257
265
|
if (member.kind === 'constructor') {
|
|
258
266
|
selectors.push('constructor')
|
|
259
267
|
}
|
|
@@ -292,7 +300,7 @@ const sortClasses = createEslintRule.createEslintRule({
|
|
|
292
300
|
}
|
|
293
301
|
if (member.accessibility === 'protected') {
|
|
294
302
|
modifiers.push('protected')
|
|
295
|
-
} else if (member.accessibility === 'private' ||
|
|
303
|
+
} else if (member.accessibility === 'private' || isPrivateHash) {
|
|
296
304
|
modifiers.push('private')
|
|
297
305
|
} else {
|
|
298
306
|
modifiers.push('public')
|
|
@@ -319,11 +327,14 @@ const sortClasses = createEslintRule.createEslintRule({
|
|
|
319
327
|
}
|
|
320
328
|
if (member.accessibility === 'protected') {
|
|
321
329
|
modifiers.push('protected')
|
|
322
|
-
} else if (member.accessibility === 'private' ||
|
|
330
|
+
} else if (member.accessibility === 'private' || isPrivateHash) {
|
|
323
331
|
modifiers.push('private')
|
|
324
332
|
} else {
|
|
325
333
|
modifiers.push('public')
|
|
326
334
|
}
|
|
335
|
+
if (member.optional) {
|
|
336
|
+
modifiers.push('optional')
|
|
337
|
+
}
|
|
327
338
|
if (
|
|
328
339
|
((_b = member.value) == null ? void 0 : _b.type) ===
|
|
329
340
|
'ArrowFunctionExpression' ||
|
|
@@ -346,8 +357,16 @@ const sortClasses = createEslintRule.createEslintRule({
|
|
|
346
357
|
if (member.type === 'PropertyDefinition' && member.value) {
|
|
347
358
|
dependencies = extractDependencies(member.value)
|
|
348
359
|
}
|
|
360
|
+
let overloadSignatureGroupMember =
|
|
361
|
+
(_d = overloadSignatureGroups.find(overloadSignatures =>
|
|
362
|
+
overloadSignatures.includes(member),
|
|
363
|
+
)) == null
|
|
364
|
+
? void 0
|
|
365
|
+
: _d.at(-1)
|
|
349
366
|
let value = {
|
|
350
|
-
size:
|
|
367
|
+
size: overloadSignatureGroupMember
|
|
368
|
+
? rangeToDiff.rangeToDiff(overloadSignatureGroupMember.range)
|
|
369
|
+
: rangeToDiff.rangeToDiff(member.range),
|
|
351
370
|
group: getGroup(),
|
|
352
371
|
node: member,
|
|
353
372
|
dependencies,
|
|
@@ -362,8 +381,11 @@ const sortClasses = createEslintRule.createEslintRule({
|
|
|
362
381
|
pairwise.pairwise(nodes, (left, right) => {
|
|
363
382
|
let leftNum = getGroupNumber.getGroupNumber(options.groups, left)
|
|
364
383
|
let rightNum = getGroupNumber.getGroupNumber(options.groups, right)
|
|
384
|
+
let isLeftOrRightIgnored =
|
|
385
|
+
leftNum === options.groups.length ||
|
|
386
|
+
rightNum === options.groups.length
|
|
365
387
|
if (
|
|
366
|
-
|
|
388
|
+
!isLeftOrRightIgnored &&
|
|
367
389
|
(leftNum > rightNum ||
|
|
368
390
|
(leftNum === rightNum &&
|
|
369
391
|
isPositive.isPositive(compare.compare(left, right, options))))
|
|
@@ -381,29 +403,35 @@ const sortClasses = createEslintRule.createEslintRule({
|
|
|
381
403
|
},
|
|
382
404
|
node: right.node,
|
|
383
405
|
fix: fixer => {
|
|
384
|
-
let
|
|
406
|
+
let nodesByNonIgnoredGroupNumber = {}
|
|
407
|
+
let ignoredNodeIndices = []
|
|
408
|
+
for (let [index, sortingNode] of nodes.entries()) {
|
|
385
409
|
let groupNum = getGroupNumber.getGroupNumber(
|
|
386
410
|
options.groups,
|
|
387
411
|
sortingNode,
|
|
388
412
|
)
|
|
389
|
-
if (
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
accumulator[groupNum] = sortNodes.sortNodes(
|
|
393
|
-
[...accumulator[groupNum], sortingNode],
|
|
394
|
-
options,
|
|
395
|
-
)
|
|
413
|
+
if (groupNum === options.groups.length) {
|
|
414
|
+
ignoredNodeIndices.push(index)
|
|
415
|
+
continue
|
|
396
416
|
}
|
|
397
|
-
|
|
398
|
-
|
|
417
|
+
nodesByNonIgnoredGroupNumber[groupNum] =
|
|
418
|
+
nodesByNonIgnoredGroupNumber[groupNum] ?? []
|
|
419
|
+
nodesByNonIgnoredGroupNumber[groupNum].push(sortingNode)
|
|
420
|
+
}
|
|
399
421
|
let sortedNodes = []
|
|
400
|
-
for (let
|
|
401
|
-
|
|
402
|
-
)) {
|
|
422
|
+
for (let groupNumber of Object.keys(
|
|
423
|
+
nodesByNonIgnoredGroupNumber,
|
|
424
|
+
).sort((a, b) => Number(a) - Number(b))) {
|
|
403
425
|
sortedNodes.push(
|
|
404
|
-
...sortNodes.sortNodes(
|
|
426
|
+
...sortNodes.sortNodes(
|
|
427
|
+
nodesByNonIgnoredGroupNumber[Number(groupNumber)],
|
|
428
|
+
options,
|
|
429
|
+
),
|
|
405
430
|
)
|
|
406
431
|
}
|
|
432
|
+
for (let ignoredIndex of ignoredNodeIndices) {
|
|
433
|
+
sortedNodes.splice(ignoredIndex, 0, nodes[ignoredIndex])
|
|
434
|
+
}
|
|
407
435
|
return makeFixes.makeFixes(
|
|
408
436
|
fixer,
|
|
409
437
|
nodes,
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getOverloadSignatureGroups,
|
|
3
|
+
generateOfficialGroups,
|
|
4
|
+
} from './sort-classes-utils.mjs'
|
|
1
5
|
import { isPartitionComment } from '../utils/is-partition-comment.mjs'
|
|
2
6
|
import { getCommentBefore } from '../utils/get-comment-before.mjs'
|
|
3
7
|
import { createEslintRule } from '../utils/create-eslint-rule.mjs'
|
|
4
|
-
import { generateOfficialGroups } from './sort-classes-utils.mjs'
|
|
5
8
|
import { getGroupNumber } from '../utils/get-group-number.mjs'
|
|
6
9
|
import { getSourceCode } from '../utils/get-source-code.mjs'
|
|
7
10
|
import { toSingleLine } from '../utils/to-single-line.mjs'
|
|
@@ -111,11 +114,12 @@ const sortClasses = createEslintRule({
|
|
|
111
114
|
ignoreCase: true,
|
|
112
115
|
partitionByComment: false,
|
|
113
116
|
groups: [
|
|
117
|
+
'static-block',
|
|
114
118
|
'index-signature',
|
|
115
119
|
'static-property',
|
|
116
|
-
'protected-property',
|
|
117
|
-
'private-property',
|
|
118
|
-
'property',
|
|
120
|
+
['protected-property', 'protected-accessor-property'],
|
|
121
|
+
['private-property', 'private-accessor-property'],
|
|
122
|
+
['property', 'accessor-property'],
|
|
119
123
|
'constructor',
|
|
120
124
|
'static-method',
|
|
121
125
|
'protected-method',
|
|
@@ -133,11 +137,12 @@ const sortClasses = createEslintRule({
|
|
|
133
137
|
let settings = getSettings(context.settings)
|
|
134
138
|
let options = complete(context.options.at(0), settings, {
|
|
135
139
|
groups: [
|
|
140
|
+
'static-block',
|
|
136
141
|
'index-signature',
|
|
137
142
|
'static-property',
|
|
138
|
-
'protected-property',
|
|
139
|
-
'private-property',
|
|
140
|
-
'property',
|
|
143
|
+
['protected-property', 'protected-accessor-property'],
|
|
144
|
+
['private-property', 'private-accessor-property'],
|
|
145
|
+
['property', 'accessor-property'],
|
|
141
146
|
'constructor',
|
|
142
147
|
'static-method',
|
|
143
148
|
'protected-method',
|
|
@@ -191,9 +196,10 @@ const sortClasses = createEslintRule({
|
|
|
191
196
|
traverseNode(expression)
|
|
192
197
|
return dependencies
|
|
193
198
|
}
|
|
199
|
+
let overloadSignatureGroups = getOverloadSignatureGroups(node.body)
|
|
194
200
|
let formattedNodes = node.body.reduce(
|
|
195
201
|
(accumulator, member) => {
|
|
196
|
-
var _a, _b, _c
|
|
202
|
+
var _a, _b, _c, _d
|
|
197
203
|
let comment = getCommentBefore(member, sourceCode)
|
|
198
204
|
if (
|
|
199
205
|
options.partitionByComment &&
|
|
@@ -223,7 +229,8 @@ const sortClasses = createEslintRule({
|
|
|
223
229
|
name = sourceCode.text.slice(...member.key.range)
|
|
224
230
|
}
|
|
225
231
|
}
|
|
226
|
-
let
|
|
232
|
+
let isPrivateHash =
|
|
233
|
+
'key' in member && member.key.type === 'PrivateIdentifier'
|
|
227
234
|
let decorated =
|
|
228
235
|
'decorators' in member && member.decorators.length > 0
|
|
229
236
|
let modifiers = []
|
|
@@ -246,11 +253,14 @@ const sortClasses = createEslintRule({
|
|
|
246
253
|
}
|
|
247
254
|
if (member.accessibility === 'protected') {
|
|
248
255
|
modifiers.push('protected')
|
|
249
|
-
} else if (member.accessibility === 'private' ||
|
|
256
|
+
} else if (member.accessibility === 'private' || isPrivateHash) {
|
|
250
257
|
modifiers.push('private')
|
|
251
258
|
} else {
|
|
252
259
|
modifiers.push('public')
|
|
253
260
|
}
|
|
261
|
+
if (member.optional) {
|
|
262
|
+
modifiers.push('optional')
|
|
263
|
+
}
|
|
254
264
|
if (member.kind === 'constructor') {
|
|
255
265
|
selectors.push('constructor')
|
|
256
266
|
}
|
|
@@ -289,7 +299,7 @@ const sortClasses = createEslintRule({
|
|
|
289
299
|
}
|
|
290
300
|
if (member.accessibility === 'protected') {
|
|
291
301
|
modifiers.push('protected')
|
|
292
|
-
} else if (member.accessibility === 'private' ||
|
|
302
|
+
} else if (member.accessibility === 'private' || isPrivateHash) {
|
|
293
303
|
modifiers.push('private')
|
|
294
304
|
} else {
|
|
295
305
|
modifiers.push('public')
|
|
@@ -316,11 +326,14 @@ const sortClasses = createEslintRule({
|
|
|
316
326
|
}
|
|
317
327
|
if (member.accessibility === 'protected') {
|
|
318
328
|
modifiers.push('protected')
|
|
319
|
-
} else if (member.accessibility === 'private' ||
|
|
329
|
+
} else if (member.accessibility === 'private' || isPrivateHash) {
|
|
320
330
|
modifiers.push('private')
|
|
321
331
|
} else {
|
|
322
332
|
modifiers.push('public')
|
|
323
333
|
}
|
|
334
|
+
if (member.optional) {
|
|
335
|
+
modifiers.push('optional')
|
|
336
|
+
}
|
|
324
337
|
if (
|
|
325
338
|
((_b = member.value) == null ? void 0 : _b.type) ===
|
|
326
339
|
'ArrowFunctionExpression' ||
|
|
@@ -343,8 +356,16 @@ const sortClasses = createEslintRule({
|
|
|
343
356
|
if (member.type === 'PropertyDefinition' && member.value) {
|
|
344
357
|
dependencies = extractDependencies(member.value)
|
|
345
358
|
}
|
|
359
|
+
let overloadSignatureGroupMember =
|
|
360
|
+
(_d = overloadSignatureGroups.find(overloadSignatures =>
|
|
361
|
+
overloadSignatures.includes(member),
|
|
362
|
+
)) == null
|
|
363
|
+
? void 0
|
|
364
|
+
: _d.at(-1)
|
|
346
365
|
let value = {
|
|
347
|
-
size:
|
|
366
|
+
size: overloadSignatureGroupMember
|
|
367
|
+
? rangeToDiff(overloadSignatureGroupMember.range)
|
|
368
|
+
: rangeToDiff(member.range),
|
|
348
369
|
group: getGroup(),
|
|
349
370
|
node: member,
|
|
350
371
|
dependencies,
|
|
@@ -359,8 +380,11 @@ const sortClasses = createEslintRule({
|
|
|
359
380
|
pairwise(nodes, (left, right) => {
|
|
360
381
|
let leftNum = getGroupNumber(options.groups, left)
|
|
361
382
|
let rightNum = getGroupNumber(options.groups, right)
|
|
383
|
+
let isLeftOrRightIgnored =
|
|
384
|
+
leftNum === options.groups.length ||
|
|
385
|
+
rightNum === options.groups.length
|
|
362
386
|
if (
|
|
363
|
-
|
|
387
|
+
!isLeftOrRightIgnored &&
|
|
364
388
|
(leftNum > rightNum ||
|
|
365
389
|
(leftNum === rightNum &&
|
|
366
390
|
isPositive(compare(left, right, options))))
|
|
@@ -378,23 +402,31 @@ const sortClasses = createEslintRule({
|
|
|
378
402
|
},
|
|
379
403
|
node: right.node,
|
|
380
404
|
fix: fixer => {
|
|
381
|
-
let
|
|
405
|
+
let nodesByNonIgnoredGroupNumber = {}
|
|
406
|
+
let ignoredNodeIndices = []
|
|
407
|
+
for (let [index, sortingNode] of nodes.entries()) {
|
|
382
408
|
let groupNum = getGroupNumber(options.groups, sortingNode)
|
|
383
|
-
if (
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
accumulator[groupNum] = sortNodes(
|
|
387
|
-
[...accumulator[groupNum], sortingNode],
|
|
388
|
-
options,
|
|
389
|
-
)
|
|
409
|
+
if (groupNum === options.groups.length) {
|
|
410
|
+
ignoredNodeIndices.push(index)
|
|
411
|
+
continue
|
|
390
412
|
}
|
|
391
|
-
|
|
392
|
-
|
|
413
|
+
nodesByNonIgnoredGroupNumber[groupNum] =
|
|
414
|
+
nodesByNonIgnoredGroupNumber[groupNum] ?? []
|
|
415
|
+
nodesByNonIgnoredGroupNumber[groupNum].push(sortingNode)
|
|
416
|
+
}
|
|
393
417
|
let sortedNodes = []
|
|
394
|
-
for (let
|
|
395
|
-
|
|
396
|
-
)) {
|
|
397
|
-
sortedNodes.push(
|
|
418
|
+
for (let groupNumber of Object.keys(
|
|
419
|
+
nodesByNonIgnoredGroupNumber,
|
|
420
|
+
).sort((a, b) => Number(a) - Number(b))) {
|
|
421
|
+
sortedNodes.push(
|
|
422
|
+
...sortNodes(
|
|
423
|
+
nodesByNonIgnoredGroupNumber[Number(groupNumber)],
|
|
424
|
+
options,
|
|
425
|
+
),
|
|
426
|
+
)
|
|
427
|
+
}
|
|
428
|
+
for (let ignoredIndex of ignoredNodeIndices) {
|
|
429
|
+
sortedNodes.splice(ignoredIndex, 0, nodes[ignoredIndex])
|
|
398
430
|
}
|
|
399
431
|
return makeFixes(fixer, nodes, sortedNodes, sourceCode, {
|
|
400
432
|
partitionComment: options.partitionByComment,
|
package/dist/rules/sort-enums.js
CHANGED
|
@@ -113,6 +113,29 @@ const sortEnums = createEslintRule.createEslintRule({
|
|
|
113
113
|
})
|
|
114
114
|
let sourceCode = getSourceCode.getSourceCode(context)
|
|
115
115
|
let partitionComment = options.partitionByComment
|
|
116
|
+
let extractDependencies = (expression, enumName) => {
|
|
117
|
+
let dependencies = []
|
|
118
|
+
let checkNode = nodeValue => {
|
|
119
|
+
if (
|
|
120
|
+
nodeValue.type === 'MemberExpression' &&
|
|
121
|
+
nodeValue.object.type === 'Identifier' &&
|
|
122
|
+
nodeValue.object.name === enumName &&
|
|
123
|
+
nodeValue.property.type === 'Identifier'
|
|
124
|
+
) {
|
|
125
|
+
dependencies.push(nodeValue.property.name)
|
|
126
|
+
} else if (nodeValue.type === 'Identifier') {
|
|
127
|
+
dependencies.push(nodeValue.name)
|
|
128
|
+
}
|
|
129
|
+
if ('left' in nodeValue) {
|
|
130
|
+
checkNode(nodeValue.left)
|
|
131
|
+
}
|
|
132
|
+
if ('right' in nodeValue) {
|
|
133
|
+
checkNode(nodeValue.right)
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
checkNode(expression)
|
|
137
|
+
return dependencies
|
|
138
|
+
}
|
|
116
139
|
let formattedMembers = members.reduce(
|
|
117
140
|
(accumulator, member) => {
|
|
118
141
|
let comment = getCommentBefore.getCommentBefore(member, sourceCode)
|
|
@@ -130,9 +153,17 @@ const sortEnums = createEslintRule.createEslintRule({
|
|
|
130
153
|
member.id.type === 'Literal'
|
|
131
154
|
? `${member.id.value}`
|
|
132
155
|
: `${sourceCode.text.slice(...member.id.range)}`
|
|
156
|
+
let dependencies
|
|
157
|
+
if (member.initializer) {
|
|
158
|
+
dependencies = extractDependencies(
|
|
159
|
+
member.initializer,
|
|
160
|
+
node.id.name,
|
|
161
|
+
)
|
|
162
|
+
}
|
|
133
163
|
let sortingNode = {
|
|
134
164
|
size: rangeToDiff.rangeToDiff(member.range),
|
|
135
165
|
node: member,
|
|
166
|
+
dependencies,
|
|
136
167
|
name,
|
|
137
168
|
}
|
|
138
169
|
accumulator.at(-1).push(sortingNode)
|
|
@@ -112,6 +112,29 @@ const sortEnums = createEslintRule({
|
|
|
112
112
|
})
|
|
113
113
|
let sourceCode = getSourceCode(context)
|
|
114
114
|
let partitionComment = options.partitionByComment
|
|
115
|
+
let extractDependencies = (expression, enumName) => {
|
|
116
|
+
let dependencies = []
|
|
117
|
+
let checkNode = nodeValue => {
|
|
118
|
+
if (
|
|
119
|
+
nodeValue.type === 'MemberExpression' &&
|
|
120
|
+
nodeValue.object.type === 'Identifier' &&
|
|
121
|
+
nodeValue.object.name === enumName &&
|
|
122
|
+
nodeValue.property.type === 'Identifier'
|
|
123
|
+
) {
|
|
124
|
+
dependencies.push(nodeValue.property.name)
|
|
125
|
+
} else if (nodeValue.type === 'Identifier') {
|
|
126
|
+
dependencies.push(nodeValue.name)
|
|
127
|
+
}
|
|
128
|
+
if ('left' in nodeValue) {
|
|
129
|
+
checkNode(nodeValue.left)
|
|
130
|
+
}
|
|
131
|
+
if ('right' in nodeValue) {
|
|
132
|
+
checkNode(nodeValue.right)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
checkNode(expression)
|
|
136
|
+
return dependencies
|
|
137
|
+
}
|
|
115
138
|
let formattedMembers = members.reduce(
|
|
116
139
|
(accumulator, member) => {
|
|
117
140
|
let comment = getCommentBefore(member, sourceCode)
|
|
@@ -126,9 +149,17 @@ const sortEnums = createEslintRule({
|
|
|
126
149
|
member.id.type === 'Literal'
|
|
127
150
|
? `${member.id.value}`
|
|
128
151
|
: `${sourceCode.text.slice(...member.id.range)}`
|
|
152
|
+
let dependencies
|
|
153
|
+
if (member.initializer) {
|
|
154
|
+
dependencies = extractDependencies(
|
|
155
|
+
member.initializer,
|
|
156
|
+
node.id.name,
|
|
157
|
+
)
|
|
158
|
+
}
|
|
129
159
|
let sortingNode = {
|
|
130
160
|
size: rangeToDiff(member.range),
|
|
131
161
|
node: member,
|
|
162
|
+
dependencies,
|
|
132
163
|
name,
|
|
133
164
|
}
|
|
134
165
|
accumulator.at(-1).push(sortingNode)
|
|
@@ -78,10 +78,12 @@ const sortSwitchCase = createEslintRule.createEslintRule({
|
|
|
78
78
|
let nodes = node.cases.map(caseNode => {
|
|
79
79
|
var _a, _b
|
|
80
80
|
let name
|
|
81
|
+
let isDefaultClause = false
|
|
81
82
|
if (((_a = caseNode.test) == null ? void 0 : _a.type) === 'Literal') {
|
|
82
83
|
name = `${caseNode.test.value}`
|
|
83
84
|
} else if (caseNode.test === null) {
|
|
84
85
|
name = 'default'
|
|
86
|
+
isDefaultClause = true
|
|
85
87
|
} else {
|
|
86
88
|
name = sourceCode.text.slice(...caseNode.test.range)
|
|
87
89
|
}
|
|
@@ -91,6 +93,7 @@ const sortSwitchCase = createEslintRule.createEslintRule({
|
|
|
91
93
|
caseNode.range,
|
|
92
94
|
),
|
|
93
95
|
node: caseNode,
|
|
96
|
+
isDefaultClause,
|
|
94
97
|
name,
|
|
95
98
|
}
|
|
96
99
|
})
|
|
@@ -101,7 +104,7 @@ const sortSwitchCase = createEslintRule.createEslintRule({
|
|
|
101
104
|
(lefter == null ? void 0 : lefter.node.consequent.length) === 0 &&
|
|
102
105
|
left.node.consequent.length !== 0
|
|
103
106
|
let isGroupContainsDefault = group =>
|
|
104
|
-
group.some(currentNode => currentNode.
|
|
107
|
+
group.some(currentNode => currentNode.isDefaultClause)
|
|
105
108
|
let leftCaseGroup = [left]
|
|
106
109
|
let rightCaseGroup = [right]
|
|
107
110
|
for (let i = iteration - 1; i >= 0; i--) {
|
|
@@ -168,7 +171,7 @@ const sortSwitchCase = createEslintRule.createEslintRule({
|
|
|
168
171
|
let sortedGroup = sortNodes
|
|
169
172
|
.sortNodes(group, options)
|
|
170
173
|
.sort((a, b) => {
|
|
171
|
-
if (b.
|
|
174
|
+
if (b.isDefaultClause) {
|
|
172
175
|
return -1
|
|
173
176
|
}
|
|
174
177
|
return 1
|
|
@@ -226,7 +229,7 @@ const sortSwitchCase = createEslintRule.createEslintRule({
|
|
|
226
229
|
})
|
|
227
230
|
let sortedNodes = sortedNodeGroups.flat()
|
|
228
231
|
for (let max = sortedNodes.length, i = 0; i < max; i++) {
|
|
229
|
-
if (sortedNodes.at(i).
|
|
232
|
+
if (sortedNodes.at(i).isDefaultClause) {
|
|
230
233
|
sortedNodes.push(sortedNodes.splice(i, 1).at(0))
|
|
231
234
|
}
|
|
232
235
|
}
|
|
@@ -77,10 +77,12 @@ const sortSwitchCase = createEslintRule({
|
|
|
77
77
|
let nodes = node.cases.map(caseNode => {
|
|
78
78
|
var _a, _b
|
|
79
79
|
let name
|
|
80
|
+
let isDefaultClause = false
|
|
80
81
|
if (((_a = caseNode.test) == null ? void 0 : _a.type) === 'Literal') {
|
|
81
82
|
name = `${caseNode.test.value}`
|
|
82
83
|
} else if (caseNode.test === null) {
|
|
83
84
|
name = 'default'
|
|
85
|
+
isDefaultClause = true
|
|
84
86
|
} else {
|
|
85
87
|
name = sourceCode.text.slice(...caseNode.test.range)
|
|
86
88
|
}
|
|
@@ -90,6 +92,7 @@ const sortSwitchCase = createEslintRule({
|
|
|
90
92
|
caseNode.range,
|
|
91
93
|
),
|
|
92
94
|
node: caseNode,
|
|
95
|
+
isDefaultClause,
|
|
93
96
|
name,
|
|
94
97
|
}
|
|
95
98
|
})
|
|
@@ -100,7 +103,7 @@ const sortSwitchCase = createEslintRule({
|
|
|
100
103
|
(lefter == null ? void 0 : lefter.node.consequent.length) === 0 &&
|
|
101
104
|
left.node.consequent.length !== 0
|
|
102
105
|
let isGroupContainsDefault = group =>
|
|
103
|
-
group.some(currentNode => currentNode.
|
|
106
|
+
group.some(currentNode => currentNode.isDefaultClause)
|
|
104
107
|
let leftCaseGroup = [left]
|
|
105
108
|
let rightCaseGroup = [right]
|
|
106
109
|
for (let i = iteration - 1; i >= 0; i--) {
|
|
@@ -161,7 +164,7 @@ const sortSwitchCase = createEslintRule({
|
|
|
161
164
|
.map(group => {
|
|
162
165
|
var _a, _b
|
|
163
166
|
let sortedGroup = sortNodes(group, options).sort((a, b) => {
|
|
164
|
-
if (b.
|
|
167
|
+
if (b.isDefaultClause) {
|
|
165
168
|
return -1
|
|
166
169
|
}
|
|
167
170
|
return 1
|
|
@@ -214,7 +217,7 @@ const sortSwitchCase = createEslintRule({
|
|
|
214
217
|
})
|
|
215
218
|
let sortedNodes = sortedNodeGroups.flat()
|
|
216
219
|
for (let max = sortedNodes.length, i = 0; i < max; i++) {
|
|
217
|
-
if (sortedNodes.at(i).
|
|
220
|
+
if (sortedNodes.at(i).isDefaultClause) {
|
|
218
221
|
sortedNodes.push(sortedNodes.splice(i, 1).at(0))
|
|
219
222
|
}
|
|
220
223
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-perfectionist",
|
|
3
3
|
"description": "ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.3.0",
|
|
5
5
|
"homepage": "https://perfectionist.dev",
|
|
6
6
|
"repository": "azat-io/eslint-plugin-perfectionist",
|
|
7
7
|
"author": "Azat S. <to@azat.io>",
|
|
@@ -53,8 +53,8 @@
|
|
|
53
53
|
"vue-eslint-parser": ">=9.0.0"
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@typescript-eslint/types": "^8.
|
|
57
|
-
"@typescript-eslint/utils": "^8.
|
|
56
|
+
"@typescript-eslint/types": "^8.3.0",
|
|
57
|
+
"@typescript-eslint/utils": "^8.3.0",
|
|
58
58
|
"minimatch": "^10.0.1",
|
|
59
59
|
"natural-compare-lite": "^1.4.0"
|
|
60
60
|
}
|