eslint-plugin-perfectionist 3.5.0 → 3.7.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 +41 -12
- package/dist/rules/sort-array-includes.js +118 -60
- package/dist/rules/sort-array-includes.mjs +106 -58
- package/dist/rules/sort-astro-attributes.js +26 -45
- package/dist/rules/sort-astro-attributes.mjs +17 -32
- package/dist/rules/sort-classes.js +63 -40
- package/dist/rules/sort-classes.mjs +60 -40
- package/dist/rules/sort-enums.js +73 -41
- package/dist/rules/sort-enums.mjs +71 -40
- package/dist/rules/sort-exports.js +88 -12
- package/dist/rules/sort-exports.mjs +85 -12
- package/dist/rules/sort-imports.js +14 -10
- package/dist/rules/sort-imports.mjs +13 -9
- package/dist/rules/sort-interfaces.js +98 -107
- package/dist/rules/sort-interfaces.mjs +76 -90
- package/dist/rules/sort-intersection-types.js +153 -114
- package/dist/rules/sort-intersection-types.mjs +149 -108
- package/dist/rules/sort-jsx-props.js +26 -45
- package/dist/rules/sort-jsx-props.mjs +17 -32
- package/dist/rules/sort-maps.js +78 -22
- package/dist/rules/sort-maps.mjs +74 -22
- package/dist/rules/sort-named-exports.js +87 -31
- package/dist/rules/sort-named-exports.mjs +81 -28
- package/dist/rules/sort-named-imports.js +87 -31
- package/dist/rules/sort-named-imports.mjs +81 -28
- package/dist/rules/sort-object-types.js +88 -120
- package/dist/rules/sort-object-types.mjs +80 -105
- package/dist/rules/sort-objects.js +121 -89
- package/dist/rules/sort-objects.mjs +118 -85
- package/dist/rules/sort-svelte-attributes.js +26 -45
- package/dist/rules/sort-svelte-attributes.mjs +17 -32
- package/dist/rules/sort-union-types.js +153 -114
- package/dist/rules/sort-union-types.mjs +149 -108
- package/dist/rules/sort-variable-declarations.js +97 -24
- package/dist/rules/sort-variable-declarations.mjs +95 -25
- package/dist/rules/sort-vue-attributes.js +26 -46
- package/dist/rules/sort-vue-attributes.mjs +17 -33
- package/dist/utils/get-comments-before.js +12 -0
- package/dist/utils/get-comments-before.mjs +10 -0
- package/dist/utils/get-node-range.js +24 -12
- package/dist/utils/get-node-range.mjs +22 -12
- package/dist/utils/is-partition-comment.js +3 -0
- package/dist/utils/is-partition-comment.mjs +3 -1
- package/dist/utils/sort-nodes-by-dependencies.js +16 -0
- package/dist/utils/sort-nodes-by-dependencies.mjs +16 -1
- package/dist/utils/sort-nodes-by-groups.js +22 -0
- package/dist/utils/sort-nodes-by-groups.mjs +20 -0
- package/package.json +4 -4
- package/dist/utils/get-comment-before.js +0 -23
- package/dist/utils/get-comment-before.mjs +0 -21
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
const node_module = require('node:module')
|
|
3
3
|
const minimatch = require('minimatch')
|
|
4
4
|
const validateGroupsConfiguration = require('../utils/validate-groups-configuration.js')
|
|
5
|
-
const
|
|
5
|
+
const getCommentsBefore = require('../utils/get-comments-before.js')
|
|
6
6
|
const createEslintRule = require('../utils/create-eslint-rule.js')
|
|
7
7
|
const getLinesBetween = require('../utils/get-lines-between.js')
|
|
8
8
|
const getGroupNumber = require('../utils/get-group-number.js')
|
|
@@ -140,6 +140,8 @@ const sortImports = createEslintRule.createEslintRule({
|
|
|
140
140
|
},
|
|
141
141
|
],
|
|
142
142
|
messages: {
|
|
143
|
+
unexpectedImportsGroupOrder:
|
|
144
|
+
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
|
|
143
145
|
unexpectedImportsOrder: 'Expected "{{right}}" to come before "{{left}}".',
|
|
144
146
|
missedSpacingBetweenImports:
|
|
145
147
|
'Missed spacing between "{{left}}" and "{{right}}" imports.',
|
|
@@ -409,14 +411,11 @@ const sortImports = createEslintRule.createEslintRule({
|
|
|
409
411
|
'Program:exit': () => {
|
|
410
412
|
var _a
|
|
411
413
|
let hasContentBetweenNodes = (left, right) =>
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
includeComments: true,
|
|
418
|
-
},
|
|
419
|
-
).length
|
|
414
|
+
getCommentsBefore.getCommentsBefore(right.node, sourceCode).length >
|
|
415
|
+
0 ||
|
|
416
|
+
!!sourceCode.getTokensBetween(left.node, right.node, {
|
|
417
|
+
includeComments: false,
|
|
418
|
+
}).length
|
|
420
419
|
let fix = (fixer, nodesToFix) => {
|
|
421
420
|
let fixes = []
|
|
422
421
|
let grouped = {}
|
|
@@ -550,10 +549,15 @@ const sortImports = createEslintRule.createEslintRule({
|
|
|
550
549
|
isPositive.isPositive(compare.compare(left, right, options))))
|
|
551
550
|
) {
|
|
552
551
|
context.report({
|
|
553
|
-
messageId:
|
|
552
|
+
messageId:
|
|
553
|
+
leftNum !== rightNum
|
|
554
|
+
? 'unexpectedImportsGroupOrder'
|
|
555
|
+
: 'unexpectedImportsOrder',
|
|
554
556
|
data: {
|
|
555
557
|
left: left.name,
|
|
558
|
+
leftGroup: left.group,
|
|
556
559
|
right: right.name,
|
|
560
|
+
rightGroup: right.group,
|
|
557
561
|
},
|
|
558
562
|
node: right.node,
|
|
559
563
|
fix: fixer => fix(fixer, nodeList),
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { builtinModules } from 'node:module'
|
|
2
2
|
import { minimatch } from 'minimatch'
|
|
3
3
|
import { validateGroupsConfiguration } from '../utils/validate-groups-configuration.mjs'
|
|
4
|
-
import {
|
|
4
|
+
import { getCommentsBefore } from '../utils/get-comments-before.mjs'
|
|
5
5
|
import { createEslintRule } from '../utils/create-eslint-rule.mjs'
|
|
6
6
|
import { getLinesBetween } from '../utils/get-lines-between.mjs'
|
|
7
7
|
import { getGroupNumber } from '../utils/get-group-number.mjs'
|
|
@@ -139,6 +139,8 @@ const sortImports = createEslintRule({
|
|
|
139
139
|
},
|
|
140
140
|
],
|
|
141
141
|
messages: {
|
|
142
|
+
unexpectedImportsGroupOrder:
|
|
143
|
+
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
|
|
142
144
|
unexpectedImportsOrder: 'Expected "{{right}}" to come before "{{left}}".',
|
|
143
145
|
missedSpacingBetweenImports:
|
|
144
146
|
'Missed spacing between "{{left}}" and "{{right}}" imports.',
|
|
@@ -404,13 +406,10 @@ const sortImports = createEslintRule({
|
|
|
404
406
|
'Program:exit': () => {
|
|
405
407
|
var _a
|
|
406
408
|
let hasContentBetweenNodes = (left, right) =>
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
includeComments: true,
|
|
412
|
-
},
|
|
413
|
-
).length
|
|
409
|
+
getCommentsBefore(right.node, sourceCode).length > 0 ||
|
|
410
|
+
!!sourceCode.getTokensBetween(left.node, right.node, {
|
|
411
|
+
includeComments: false,
|
|
412
|
+
}).length
|
|
414
413
|
let fix = (fixer, nodesToFix) => {
|
|
415
414
|
let fixes = []
|
|
416
415
|
let grouped = {}
|
|
@@ -535,10 +534,15 @@ const sortImports = createEslintRule({
|
|
|
535
534
|
isPositive(compare(left, right, options))))
|
|
536
535
|
) {
|
|
537
536
|
context.report({
|
|
538
|
-
messageId:
|
|
537
|
+
messageId:
|
|
538
|
+
leftNum !== rightNum
|
|
539
|
+
? 'unexpectedImportsGroupOrder'
|
|
540
|
+
: 'unexpectedImportsOrder',
|
|
539
541
|
data: {
|
|
540
542
|
left: left.name,
|
|
543
|
+
leftGroup: left.group,
|
|
541
544
|
right: right.name,
|
|
545
|
+
rightGroup: right.group,
|
|
542
546
|
},
|
|
543
547
|
node: right.node,
|
|
544
548
|
fix: fixer => fix(fixer, nodeList),
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
const minimatch = require('minimatch')
|
|
3
3
|
const validateGroupsConfiguration = require('../utils/validate-groups-configuration.js')
|
|
4
|
+
const isPartitionComment = require('../utils/is-partition-comment.js')
|
|
5
|
+
const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js')
|
|
6
|
+
const getCommentsBefore = require('../utils/get-comments-before.js')
|
|
4
7
|
const createEslintRule = require('../utils/create-eslint-rule.js')
|
|
5
8
|
const isMemberOptional = require('../utils/is-member-optional.js')
|
|
6
9
|
const getLinesBetween = require('../utils/get-lines-between.js')
|
|
@@ -9,13 +12,10 @@ const getSourceCode = require('../utils/get-source-code.js')
|
|
|
9
12
|
const toSingleLine = require('../utils/to-single-line.js')
|
|
10
13
|
const rangeToDiff = require('../utils/range-to-diff.js')
|
|
11
14
|
const getSettings = require('../utils/get-settings.js')
|
|
12
|
-
const isPositive = require('../utils/is-positive.js')
|
|
13
15
|
const useGroups = require('../utils/use-groups.js')
|
|
14
|
-
const sortNodes = require('../utils/sort-nodes.js')
|
|
15
16
|
const makeFixes = require('../utils/make-fixes.js')
|
|
16
17
|
const complete = require('../utils/complete.js')
|
|
17
18
|
const pairwise = require('../utils/pairwise.js')
|
|
18
|
-
const compare = require('../utils/compare.js')
|
|
19
19
|
const sortInterfaces = createEslintRule.createEslintRule({
|
|
20
20
|
name: 'sort-interfaces',
|
|
21
21
|
meta: {
|
|
@@ -52,6 +52,24 @@ const sortInterfaces = createEslintRule.createEslintRule({
|
|
|
52
52
|
},
|
|
53
53
|
type: 'array',
|
|
54
54
|
},
|
|
55
|
+
partitionByComment: {
|
|
56
|
+
description:
|
|
57
|
+
'Allows you to use comments to separate the interface properties into logical groups.',
|
|
58
|
+
anyOf: [
|
|
59
|
+
{
|
|
60
|
+
type: 'boolean',
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
type: 'string',
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
type: 'array',
|
|
67
|
+
items: {
|
|
68
|
+
type: 'string',
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
},
|
|
55
73
|
partitionByNewLine: {
|
|
56
74
|
description:
|
|
57
75
|
'Allows to use spaces to separate the nodes into logical groups.',
|
|
@@ -101,6 +119,8 @@ const sortInterfaces = createEslintRule.createEslintRule({
|
|
|
101
119
|
},
|
|
102
120
|
],
|
|
103
121
|
messages: {
|
|
122
|
+
unexpectedInterfacePropertiesGroupOrder:
|
|
123
|
+
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
|
|
104
124
|
unexpectedInterfacePropertiesOrder:
|
|
105
125
|
'Expected "{{right}}" to come before "{{left}}".',
|
|
106
126
|
},
|
|
@@ -111,6 +131,7 @@ const sortInterfaces = createEslintRule.createEslintRule({
|
|
|
111
131
|
order: 'asc',
|
|
112
132
|
ignoreCase: true,
|
|
113
133
|
ignorePattern: [],
|
|
134
|
+
partitionByComment: false,
|
|
114
135
|
partitionByNewLine: false,
|
|
115
136
|
groupKind: 'mixed',
|
|
116
137
|
groups: [],
|
|
@@ -122,6 +143,7 @@ const sortInterfaces = createEslintRule.createEslintRule({
|
|
|
122
143
|
if (node.body.body.length > 1) {
|
|
123
144
|
let settings = getSettings.getSettings(context.settings)
|
|
124
145
|
let options = complete.complete(context.options.at(0), settings, {
|
|
146
|
+
partitionByComment: false,
|
|
125
147
|
partitionByNewLine: false,
|
|
126
148
|
type: 'alphabetical',
|
|
127
149
|
groupKind: 'mixed',
|
|
@@ -137,6 +159,7 @@ const sortInterfaces = createEslintRule.createEslintRule({
|
|
|
137
159
|
Object.keys(options.customGroups),
|
|
138
160
|
)
|
|
139
161
|
let sourceCode = getSourceCode.getSourceCode(context)
|
|
162
|
+
let partitionComment = options.partitionByComment
|
|
140
163
|
if (
|
|
141
164
|
!options.ignorePattern.some(pattern =>
|
|
142
165
|
minimatch.minimatch(node.id.name, pattern, {
|
|
@@ -182,132 +205,100 @@ const sortInterfaces = createEslintRule.createEslintRule({
|
|
|
182
205
|
: _d.range.at(0)) ?? element.range.at(1)
|
|
183
206
|
name = sourceCode.text.slice(element.range.at(0), endIndex)
|
|
184
207
|
}
|
|
208
|
+
setCustomGroups(options.customGroups, name)
|
|
209
|
+
if (element.loc.start.line !== element.loc.end.line) {
|
|
210
|
+
defineGroup('multiline')
|
|
211
|
+
}
|
|
185
212
|
let elementSortingNode = {
|
|
186
213
|
size: rangeToDiff.rangeToDiff(element.range),
|
|
187
214
|
node: element,
|
|
215
|
+
group: getGroup(),
|
|
188
216
|
name,
|
|
189
217
|
}
|
|
190
218
|
if (
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
219
|
+
(partitionComment &&
|
|
220
|
+
isPartitionComment.hasPartitionComment(
|
|
221
|
+
partitionComment,
|
|
222
|
+
getCommentsBefore.getCommentsBefore(element, sourceCode),
|
|
223
|
+
)) ||
|
|
224
|
+
(options.partitionByNewLine &&
|
|
225
|
+
lastElement &&
|
|
226
|
+
getLinesBetween.getLinesBetween(
|
|
227
|
+
sourceCode,
|
|
228
|
+
lastElement,
|
|
229
|
+
elementSortingNode,
|
|
230
|
+
))
|
|
198
231
|
) {
|
|
199
232
|
accumulator.push([])
|
|
200
233
|
}
|
|
201
|
-
|
|
202
|
-
if (element.loc.start.line !== element.loc.end.line) {
|
|
203
|
-
defineGroup('multiline')
|
|
204
|
-
}
|
|
205
|
-
accumulator.at(-1).push({
|
|
206
|
-
...elementSortingNode,
|
|
207
|
-
group: getGroup(),
|
|
208
|
-
})
|
|
234
|
+
accumulator.at(-1).push(elementSortingNode)
|
|
209
235
|
return accumulator
|
|
210
236
|
},
|
|
211
237
|
[[]],
|
|
212
238
|
)
|
|
213
|
-
let toSorted = nodes => {
|
|
214
|
-
let grouped = {}
|
|
215
|
-
for (let currentNode of nodes) {
|
|
216
|
-
let groupNum = getGroupNumber.getGroupNumber(
|
|
217
|
-
options.groups,
|
|
218
|
-
currentNode,
|
|
219
|
-
)
|
|
220
|
-
if (!(groupNum in grouped)) {
|
|
221
|
-
grouped[groupNum] = [currentNode]
|
|
222
|
-
} else {
|
|
223
|
-
grouped[groupNum] = sortNodes.sortNodes(
|
|
224
|
-
[...grouped[groupNum], currentNode],
|
|
225
|
-
options,
|
|
226
|
-
)
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
let sortedNodes = []
|
|
230
|
-
for (let group of Object.keys(grouped).sort(
|
|
231
|
-
(a, b) => Number(a) - Number(b),
|
|
232
|
-
)) {
|
|
233
|
-
sortedNodes.push(...sortNodes.sortNodes(grouped[group], options))
|
|
234
|
-
}
|
|
235
|
-
return sortedNodes
|
|
236
|
-
}
|
|
237
|
-
let checkGroupSort = (left, right) => {
|
|
238
|
-
let leftNum = getGroupNumber.getGroupNumber(options.groups, left)
|
|
239
|
-
let rightNum = getGroupNumber.getGroupNumber(options.groups, right)
|
|
240
|
-
return (
|
|
241
|
-
leftNum > rightNum ||
|
|
242
|
-
(leftNum === rightNum &&
|
|
243
|
-
isPositive.isPositive(compare.compare(left, right, options)))
|
|
244
|
-
)
|
|
245
|
-
}
|
|
246
239
|
let { groupKind } = options
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
(_, i) =>
|
|
253
|
-
i &&
|
|
254
|
-
isMemberOptional.isMemberOptional(members[i - 1].node) !==
|
|
255
|
-
isMemberOptional.isMemberOptional(members[i].node),
|
|
256
|
-
)
|
|
257
|
-
if (iteration < switchIndex && iteration + 1 !== switchIndex) {
|
|
258
|
-
return checkGroupSort(left, right)
|
|
259
|
-
}
|
|
260
|
-
if (
|
|
261
|
-
isMemberOptional.isMemberOptional(left.node) !==
|
|
262
|
-
isMemberOptional.isMemberOptional(right.node)
|
|
263
|
-
) {
|
|
264
|
-
return (
|
|
265
|
-
isMemberOptional.isMemberOptional(left.node) !==
|
|
266
|
-
(groupKind === 'optional-first')
|
|
240
|
+
for (let nodes of formattedMembers) {
|
|
241
|
+
let sortedNodes
|
|
242
|
+
if (groupKind !== 'mixed') {
|
|
243
|
+
let optionalNodes = nodes.filter(member =>
|
|
244
|
+
isMemberOptional.isMemberOptional(member.node),
|
|
267
245
|
)
|
|
246
|
+
let requiredNodes = nodes.filter(
|
|
247
|
+
member => !isMemberOptional.isMemberOptional(member.node),
|
|
248
|
+
)
|
|
249
|
+
sortedNodes =
|
|
250
|
+
groupKind === 'optional-first'
|
|
251
|
+
? [
|
|
252
|
+
...sortNodesByGroups.sortNodesByGroups(
|
|
253
|
+
optionalNodes,
|
|
254
|
+
options,
|
|
255
|
+
),
|
|
256
|
+
...sortNodesByGroups.sortNodesByGroups(
|
|
257
|
+
requiredNodes,
|
|
258
|
+
options,
|
|
259
|
+
),
|
|
260
|
+
]
|
|
261
|
+
: [
|
|
262
|
+
...sortNodesByGroups.sortNodesByGroups(
|
|
263
|
+
requiredNodes,
|
|
264
|
+
options,
|
|
265
|
+
),
|
|
266
|
+
...sortNodesByGroups.sortNodesByGroups(
|
|
267
|
+
optionalNodes,
|
|
268
|
+
options,
|
|
269
|
+
),
|
|
270
|
+
]
|
|
271
|
+
} else {
|
|
272
|
+
sortedNodes = sortNodesByGroups.sortNodesByGroups(nodes, options)
|
|
268
273
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
+
pairwise.pairwise(nodes, (left, right) => {
|
|
275
|
+
let indexOfLeft = sortedNodes.indexOf(left)
|
|
276
|
+
let indexOfRight = sortedNodes.indexOf(right)
|
|
277
|
+
if (indexOfLeft > indexOfRight) {
|
|
278
|
+
let leftNum = getGroupNumber.getGroupNumber(
|
|
279
|
+
options.groups,
|
|
280
|
+
left,
|
|
281
|
+
)
|
|
282
|
+
let rightNum = getGroupNumber.getGroupNumber(
|
|
283
|
+
options.groups,
|
|
284
|
+
right,
|
|
285
|
+
)
|
|
274
286
|
context.report({
|
|
275
|
-
messageId:
|
|
287
|
+
messageId:
|
|
288
|
+
leftNum !== rightNum
|
|
289
|
+
? 'unexpectedInterfacePropertiesGroupOrder'
|
|
290
|
+
: 'unexpectedInterfacePropertiesOrder',
|
|
276
291
|
data: {
|
|
277
292
|
left: toSingleLine.toSingleLine(left.name),
|
|
293
|
+
leftGroup: left.group,
|
|
278
294
|
right: toSingleLine.toSingleLine(right.name),
|
|
295
|
+
rightGroup: right.group,
|
|
279
296
|
},
|
|
280
297
|
node: right.node,
|
|
281
|
-
fix: fixer =>
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
isMemberOptional.isMemberOptional(member.node),
|
|
286
|
-
)
|
|
287
|
-
let requiredNodes = nodes.filter(
|
|
288
|
-
member =>
|
|
289
|
-
!isMemberOptional.isMemberOptional(member.node),
|
|
290
|
-
)
|
|
291
|
-
sortedNodes =
|
|
292
|
-
groupKind === 'optional-first'
|
|
293
|
-
? [
|
|
294
|
-
...toSorted(optionalNodes),
|
|
295
|
-
...toSorted(requiredNodes),
|
|
296
|
-
]
|
|
297
|
-
: [
|
|
298
|
-
...toSorted(requiredNodes),
|
|
299
|
-
...toSorted(optionalNodes),
|
|
300
|
-
]
|
|
301
|
-
} else {
|
|
302
|
-
sortedNodes = toSorted(nodes)
|
|
303
|
-
}
|
|
304
|
-
return makeFixes.makeFixes(
|
|
305
|
-
fixer,
|
|
306
|
-
nodes,
|
|
307
|
-
sortedNodes,
|
|
308
|
-
sourceCode,
|
|
309
|
-
)
|
|
310
|
-
},
|
|
298
|
+
fix: fixer =>
|
|
299
|
+
makeFixes.makeFixes(fixer, nodes, sortedNodes, sourceCode, {
|
|
300
|
+
partitionComment,
|
|
301
|
+
}),
|
|
311
302
|
})
|
|
312
303
|
}
|
|
313
304
|
})
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { minimatch } from 'minimatch'
|
|
2
2
|
import { validateGroupsConfiguration } from '../utils/validate-groups-configuration.mjs'
|
|
3
|
+
import { hasPartitionComment } from '../utils/is-partition-comment.mjs'
|
|
4
|
+
import { sortNodesByGroups } from '../utils/sort-nodes-by-groups.mjs'
|
|
5
|
+
import { getCommentsBefore } from '../utils/get-comments-before.mjs'
|
|
3
6
|
import { createEslintRule } from '../utils/create-eslint-rule.mjs'
|
|
4
7
|
import { isMemberOptional } from '../utils/is-member-optional.mjs'
|
|
5
8
|
import { getLinesBetween } from '../utils/get-lines-between.mjs'
|
|
@@ -8,13 +11,10 @@ import { getSourceCode } from '../utils/get-source-code.mjs'
|
|
|
8
11
|
import { toSingleLine } from '../utils/to-single-line.mjs'
|
|
9
12
|
import { rangeToDiff } from '../utils/range-to-diff.mjs'
|
|
10
13
|
import { getSettings } from '../utils/get-settings.mjs'
|
|
11
|
-
import { isPositive } from '../utils/is-positive.mjs'
|
|
12
14
|
import { useGroups } from '../utils/use-groups.mjs'
|
|
13
|
-
import { sortNodes } from '../utils/sort-nodes.mjs'
|
|
14
15
|
import { makeFixes } from '../utils/make-fixes.mjs'
|
|
15
16
|
import { complete } from '../utils/complete.mjs'
|
|
16
17
|
import { pairwise } from '../utils/pairwise.mjs'
|
|
17
|
-
import { compare } from '../utils/compare.mjs'
|
|
18
18
|
const sortInterfaces = createEslintRule({
|
|
19
19
|
name: 'sort-interfaces',
|
|
20
20
|
meta: {
|
|
@@ -51,6 +51,24 @@ const sortInterfaces = createEslintRule({
|
|
|
51
51
|
},
|
|
52
52
|
type: 'array',
|
|
53
53
|
},
|
|
54
|
+
partitionByComment: {
|
|
55
|
+
description:
|
|
56
|
+
'Allows you to use comments to separate the interface properties into logical groups.',
|
|
57
|
+
anyOf: [
|
|
58
|
+
{
|
|
59
|
+
type: 'boolean',
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
type: 'string',
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
type: 'array',
|
|
66
|
+
items: {
|
|
67
|
+
type: 'string',
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
},
|
|
54
72
|
partitionByNewLine: {
|
|
55
73
|
description:
|
|
56
74
|
'Allows to use spaces to separate the nodes into logical groups.',
|
|
@@ -100,6 +118,8 @@ const sortInterfaces = createEslintRule({
|
|
|
100
118
|
},
|
|
101
119
|
],
|
|
102
120
|
messages: {
|
|
121
|
+
unexpectedInterfacePropertiesGroupOrder:
|
|
122
|
+
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
|
|
103
123
|
unexpectedInterfacePropertiesOrder:
|
|
104
124
|
'Expected "{{right}}" to come before "{{left}}".',
|
|
105
125
|
},
|
|
@@ -110,6 +130,7 @@ const sortInterfaces = createEslintRule({
|
|
|
110
130
|
order: 'asc',
|
|
111
131
|
ignoreCase: true,
|
|
112
132
|
ignorePattern: [],
|
|
133
|
+
partitionByComment: false,
|
|
113
134
|
partitionByNewLine: false,
|
|
114
135
|
groupKind: 'mixed',
|
|
115
136
|
groups: [],
|
|
@@ -121,6 +142,7 @@ const sortInterfaces = createEslintRule({
|
|
|
121
142
|
if (node.body.body.length > 1) {
|
|
122
143
|
let settings = getSettings(context.settings)
|
|
123
144
|
let options = complete(context.options.at(0), settings, {
|
|
145
|
+
partitionByComment: false,
|
|
124
146
|
partitionByNewLine: false,
|
|
125
147
|
type: 'alphabetical',
|
|
126
148
|
groupKind: 'mixed',
|
|
@@ -136,6 +158,7 @@ const sortInterfaces = createEslintRule({
|
|
|
136
158
|
Object.keys(options.customGroups),
|
|
137
159
|
)
|
|
138
160
|
let sourceCode = getSourceCode(context)
|
|
161
|
+
let partitionComment = options.partitionByComment
|
|
139
162
|
if (
|
|
140
163
|
!options.ignorePattern.some(pattern =>
|
|
141
164
|
minimatch(node.id.name, pattern, {
|
|
@@ -182,115 +205,78 @@ const sortInterfaces = createEslintRule({
|
|
|
182
205
|
: _d.range.at(0)) ?? element.range.at(1)
|
|
183
206
|
name = sourceCode.text.slice(element.range.at(0), endIndex)
|
|
184
207
|
}
|
|
208
|
+
setCustomGroups(options.customGroups, name)
|
|
209
|
+
if (element.loc.start.line !== element.loc.end.line) {
|
|
210
|
+
defineGroup('multiline')
|
|
211
|
+
}
|
|
185
212
|
let elementSortingNode = {
|
|
186
213
|
size: rangeToDiff(element.range),
|
|
187
214
|
node: element,
|
|
215
|
+
group: getGroup(),
|
|
188
216
|
name,
|
|
189
217
|
}
|
|
190
218
|
if (
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
219
|
+
(partitionComment &&
|
|
220
|
+
hasPartitionComment(
|
|
221
|
+
partitionComment,
|
|
222
|
+
getCommentsBefore(element, sourceCode),
|
|
223
|
+
)) ||
|
|
224
|
+
(options.partitionByNewLine &&
|
|
225
|
+
lastElement &&
|
|
226
|
+
getLinesBetween(sourceCode, lastElement, elementSortingNode))
|
|
194
227
|
) {
|
|
195
228
|
accumulator.push([])
|
|
196
229
|
}
|
|
197
|
-
|
|
198
|
-
if (element.loc.start.line !== element.loc.end.line) {
|
|
199
|
-
defineGroup('multiline')
|
|
200
|
-
}
|
|
201
|
-
accumulator.at(-1).push({
|
|
202
|
-
...elementSortingNode,
|
|
203
|
-
group: getGroup(),
|
|
204
|
-
})
|
|
230
|
+
accumulator.at(-1).push(elementSortingNode)
|
|
205
231
|
return accumulator
|
|
206
232
|
},
|
|
207
233
|
[[]],
|
|
208
234
|
)
|
|
209
|
-
let toSorted = nodes => {
|
|
210
|
-
let grouped = {}
|
|
211
|
-
for (let currentNode of nodes) {
|
|
212
|
-
let groupNum = getGroupNumber(options.groups, currentNode)
|
|
213
|
-
if (!(groupNum in grouped)) {
|
|
214
|
-
grouped[groupNum] = [currentNode]
|
|
215
|
-
} else {
|
|
216
|
-
grouped[groupNum] = sortNodes(
|
|
217
|
-
[...grouped[groupNum], currentNode],
|
|
218
|
-
options,
|
|
219
|
-
)
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
let sortedNodes = []
|
|
223
|
-
for (let group of Object.keys(grouped).sort(
|
|
224
|
-
(a, b) => Number(a) - Number(b),
|
|
225
|
-
)) {
|
|
226
|
-
sortedNodes.push(...sortNodes(grouped[group], options))
|
|
227
|
-
}
|
|
228
|
-
return sortedNodes
|
|
229
|
-
}
|
|
230
|
-
let checkGroupSort = (left, right) => {
|
|
231
|
-
let leftNum = getGroupNumber(options.groups, left)
|
|
232
|
-
let rightNum = getGroupNumber(options.groups, right)
|
|
233
|
-
return (
|
|
234
|
-
leftNum > rightNum ||
|
|
235
|
-
(leftNum === rightNum &&
|
|
236
|
-
isPositive(compare(left, right, options)))
|
|
237
|
-
)
|
|
238
|
-
}
|
|
239
235
|
let { groupKind } = options
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
isMemberOptional(
|
|
248
|
-
isMemberOptional(members[i].node),
|
|
249
|
-
)
|
|
250
|
-
if (iteration < switchIndex && iteration + 1 !== switchIndex) {
|
|
251
|
-
return checkGroupSort(left, right)
|
|
252
|
-
}
|
|
253
|
-
if (isMemberOptional(left.node) !== isMemberOptional(right.node)) {
|
|
254
|
-
return (
|
|
255
|
-
isMemberOptional(left.node) !== (groupKind === 'optional-first')
|
|
236
|
+
for (let nodes of formattedMembers) {
|
|
237
|
+
let sortedNodes
|
|
238
|
+
if (groupKind !== 'mixed') {
|
|
239
|
+
let optionalNodes = nodes.filter(member =>
|
|
240
|
+
isMemberOptional(member.node),
|
|
241
|
+
)
|
|
242
|
+
let requiredNodes = nodes.filter(
|
|
243
|
+
member => !isMemberOptional(member.node),
|
|
256
244
|
)
|
|
245
|
+
sortedNodes =
|
|
246
|
+
groupKind === 'optional-first'
|
|
247
|
+
? [
|
|
248
|
+
...sortNodesByGroups(optionalNodes, options),
|
|
249
|
+
...sortNodesByGroups(requiredNodes, options),
|
|
250
|
+
]
|
|
251
|
+
: [
|
|
252
|
+
...sortNodesByGroups(requiredNodes, options),
|
|
253
|
+
...sortNodesByGroups(optionalNodes, options),
|
|
254
|
+
]
|
|
255
|
+
} else {
|
|
256
|
+
sortedNodes = sortNodesByGroups(nodes, options)
|
|
257
257
|
}
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
258
|
+
pairwise(nodes, (left, right) => {
|
|
259
|
+
let indexOfLeft = sortedNodes.indexOf(left)
|
|
260
|
+
let indexOfRight = sortedNodes.indexOf(right)
|
|
261
|
+
if (indexOfLeft > indexOfRight) {
|
|
262
|
+
let leftNum = getGroupNumber(options.groups, left)
|
|
263
|
+
let rightNum = getGroupNumber(options.groups, right)
|
|
263
264
|
context.report({
|
|
264
|
-
messageId:
|
|
265
|
+
messageId:
|
|
266
|
+
leftNum !== rightNum
|
|
267
|
+
? 'unexpectedInterfacePropertiesGroupOrder'
|
|
268
|
+
: 'unexpectedInterfacePropertiesOrder',
|
|
265
269
|
data: {
|
|
266
270
|
left: toSingleLine(left.name),
|
|
271
|
+
leftGroup: left.group,
|
|
267
272
|
right: toSingleLine(right.name),
|
|
273
|
+
rightGroup: right.group,
|
|
268
274
|
},
|
|
269
275
|
node: right.node,
|
|
270
|
-
fix: fixer =>
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
isMemberOptional(member.node),
|
|
275
|
-
)
|
|
276
|
-
let requiredNodes = nodes.filter(
|
|
277
|
-
member => !isMemberOptional(member.node),
|
|
278
|
-
)
|
|
279
|
-
sortedNodes =
|
|
280
|
-
groupKind === 'optional-first'
|
|
281
|
-
? [
|
|
282
|
-
...toSorted(optionalNodes),
|
|
283
|
-
...toSorted(requiredNodes),
|
|
284
|
-
]
|
|
285
|
-
: [
|
|
286
|
-
...toSorted(requiredNodes),
|
|
287
|
-
...toSorted(optionalNodes),
|
|
288
|
-
]
|
|
289
|
-
} else {
|
|
290
|
-
sortedNodes = toSorted(nodes)
|
|
291
|
-
}
|
|
292
|
-
return makeFixes(fixer, nodes, sortedNodes, sourceCode)
|
|
293
|
-
},
|
|
276
|
+
fix: fixer =>
|
|
277
|
+
makeFixes(fixer, nodes, sortedNodes, sourceCode, {
|
|
278
|
+
partitionComment,
|
|
279
|
+
}),
|
|
294
280
|
})
|
|
295
281
|
}
|
|
296
282
|
})
|