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
package/dist/rules/sort-enums.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
const sortNodesByDependencies = require('../utils/sort-nodes-by-dependencies.js')
|
|
3
3
|
const isPartitionComment = require('../utils/is-partition-comment.js')
|
|
4
|
+
const getCommentsBefore = require('../utils/get-comments-before.js')
|
|
4
5
|
const createEslintRule = require('../utils/create-eslint-rule.js')
|
|
5
|
-
const
|
|
6
|
+
const getLinesBetween = require('../utils/get-lines-between.js')
|
|
6
7
|
const getSourceCode = require('../utils/get-source-code.js')
|
|
7
8
|
const toSingleLine = require('../utils/to-single-line.js')
|
|
8
9
|
const rangeToDiff = require('../utils/range-to-diff.js')
|
|
@@ -50,7 +51,7 @@ const sortEnums = createEslintRule.createEslintRule({
|
|
|
50
51
|
},
|
|
51
52
|
partitionByComment: {
|
|
52
53
|
description:
|
|
53
|
-
'Allows you to use comments to separate the
|
|
54
|
+
'Allows you to use comments to separate the members of enums into logical groups.',
|
|
54
55
|
anyOf: [
|
|
55
56
|
{
|
|
56
57
|
type: 'array',
|
|
@@ -66,12 +67,19 @@ const sortEnums = createEslintRule.createEslintRule({
|
|
|
66
67
|
},
|
|
67
68
|
],
|
|
68
69
|
},
|
|
70
|
+
partitionByNewLine: {
|
|
71
|
+
description:
|
|
72
|
+
'Allows to use spaces to separate the nodes into logical groups.',
|
|
73
|
+
type: 'boolean',
|
|
74
|
+
},
|
|
69
75
|
},
|
|
70
76
|
additionalProperties: false,
|
|
71
77
|
},
|
|
72
78
|
],
|
|
73
79
|
messages: {
|
|
74
80
|
unexpectedEnumsOrder: 'Expected "{{right}}" to come before "{{left}}".',
|
|
81
|
+
unexpectedEnumsDependencyOrder:
|
|
82
|
+
'Expected dependency "{{right}}" to come before "{{nodeDependentOnRight}}".',
|
|
75
83
|
},
|
|
76
84
|
},
|
|
77
85
|
defaultOptions: [
|
|
@@ -81,6 +89,7 @@ const sortEnums = createEslintRule.createEslintRule({
|
|
|
81
89
|
ignoreCase: true,
|
|
82
90
|
sortByValue: false,
|
|
83
91
|
partitionByComment: false,
|
|
92
|
+
partitionByNewLine: false,
|
|
84
93
|
forceNumericSort: false,
|
|
85
94
|
},
|
|
86
95
|
],
|
|
@@ -104,6 +113,7 @@ const sortEnums = createEslintRule.createEslintRule({
|
|
|
104
113
|
let settings = getSettings.getSettings(context.settings)
|
|
105
114
|
let options = complete.complete(context.options.at(0), settings, {
|
|
106
115
|
partitionByComment: false,
|
|
116
|
+
partitionByNewLine: false,
|
|
107
117
|
type: 'alphabetical',
|
|
108
118
|
ignoreCase: true,
|
|
109
119
|
order: 'asc',
|
|
@@ -131,27 +141,16 @@ const sortEnums = createEslintRule.createEslintRule({
|
|
|
131
141
|
if ('right' in nodeValue) {
|
|
132
142
|
checkNode(nodeValue.right)
|
|
133
143
|
}
|
|
144
|
+
if ('expressions' in nodeValue) {
|
|
145
|
+
nodeValue.expressions.forEach(checkNode)
|
|
146
|
+
}
|
|
134
147
|
}
|
|
135
148
|
checkNode(expression)
|
|
136
149
|
return dependencies
|
|
137
150
|
}
|
|
138
151
|
let formattedMembers = members.reduce(
|
|
139
152
|
(accumulator, member) => {
|
|
140
|
-
|
|
141
|
-
if (
|
|
142
|
-
partitionComment &&
|
|
143
|
-
comment &&
|
|
144
|
-
isPartitionComment.isPartitionComment(
|
|
145
|
-
partitionComment,
|
|
146
|
-
comment.value,
|
|
147
|
-
)
|
|
148
|
-
) {
|
|
149
|
-
accumulator.push([])
|
|
150
|
-
}
|
|
151
|
-
let name =
|
|
152
|
-
member.id.type === 'Literal'
|
|
153
|
-
? `${member.id.value}`
|
|
154
|
-
: `${sourceCode.text.slice(...member.id.range)}`
|
|
153
|
+
var _a
|
|
155
154
|
let dependencies = []
|
|
156
155
|
if (member.initializer) {
|
|
157
156
|
dependencies = extractDependencies(
|
|
@@ -159,11 +158,32 @@ const sortEnums = createEslintRule.createEslintRule({
|
|
|
159
158
|
node.id.name,
|
|
160
159
|
)
|
|
161
160
|
}
|
|
161
|
+
let lastSortingNode =
|
|
162
|
+
(_a = accumulator.at(-1)) == null ? void 0 : _a.at(-1)
|
|
162
163
|
let sortingNode = {
|
|
163
164
|
size: rangeToDiff.rangeToDiff(member.range),
|
|
164
165
|
node: member,
|
|
165
166
|
dependencies,
|
|
166
|
-
name
|
|
167
|
+
name:
|
|
168
|
+
member.id.type === 'Literal'
|
|
169
|
+
? `${member.id.value}`
|
|
170
|
+
: `${sourceCode.text.slice(...member.id.range)}`,
|
|
171
|
+
}
|
|
172
|
+
if (
|
|
173
|
+
(partitionComment &&
|
|
174
|
+
isPartitionComment.hasPartitionComment(
|
|
175
|
+
partitionComment,
|
|
176
|
+
getCommentsBefore.getCommentsBefore(member, sourceCode),
|
|
177
|
+
)) ||
|
|
178
|
+
(options.partitionByNewLine &&
|
|
179
|
+
lastSortingNode &&
|
|
180
|
+
getLinesBetween.getLinesBetween(
|
|
181
|
+
sourceCode,
|
|
182
|
+
lastSortingNode,
|
|
183
|
+
sortingNode,
|
|
184
|
+
))
|
|
185
|
+
) {
|
|
186
|
+
accumulator.push([])
|
|
167
187
|
}
|
|
168
188
|
accumulator.at(-1).push(sortingNode)
|
|
169
189
|
return accumulator
|
|
@@ -206,29 +226,41 @@ const sortEnums = createEslintRule.createEslintRule({
|
|
|
206
226
|
}
|
|
207
227
|
: void 0,
|
|
208
228
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
sortNodes.sortNodes(
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
229
|
+
let sortedNodes = sortNodesByDependencies.sortNodesByDependencies(
|
|
230
|
+
formattedMembers
|
|
231
|
+
.map(nodes2 => sortNodes.sortNodes(nodes2, compareOptions))
|
|
232
|
+
.flat(),
|
|
233
|
+
)
|
|
234
|
+
let nodes = formattedMembers.flat()
|
|
235
|
+
pairwise.pairwise(nodes, (left, right) => {
|
|
236
|
+
let indexOfLeft = sortedNodes.indexOf(left)
|
|
237
|
+
let indexOfRight = sortedNodes.indexOf(right)
|
|
238
|
+
if (indexOfLeft > indexOfRight) {
|
|
239
|
+
let firstUnorderedNodeDependentOnRight =
|
|
240
|
+
sortNodesByDependencies.getFirstUnorderedNodeDependentOn(
|
|
241
|
+
right,
|
|
242
|
+
nodes,
|
|
243
|
+
)
|
|
244
|
+
context.report({
|
|
245
|
+
messageId: firstUnorderedNodeDependentOnRight
|
|
246
|
+
? 'unexpectedEnumsDependencyOrder'
|
|
247
|
+
: 'unexpectedEnumsOrder',
|
|
248
|
+
data: {
|
|
249
|
+
left: toSingleLine.toSingleLine(left.name),
|
|
250
|
+
right: toSingleLine.toSingleLine(right.name),
|
|
251
|
+
nodeDependentOnRight:
|
|
252
|
+
firstUnorderedNodeDependentOnRight == null
|
|
253
|
+
? void 0
|
|
254
|
+
: firstUnorderedNodeDependentOnRight.name,
|
|
255
|
+
},
|
|
256
|
+
node: right.node,
|
|
257
|
+
fix: fixer =>
|
|
258
|
+
makeFixes.makeFixes(fixer, nodes, sortedNodes, sourceCode, {
|
|
259
|
+
partitionComment,
|
|
260
|
+
}),
|
|
261
|
+
})
|
|
262
|
+
}
|
|
263
|
+
})
|
|
232
264
|
}
|
|
233
265
|
},
|
|
234
266
|
}),
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
sortNodesByDependencies,
|
|
3
|
+
getFirstUnorderedNodeDependentOn,
|
|
4
|
+
} from '../utils/sort-nodes-by-dependencies.mjs'
|
|
5
|
+
import { hasPartitionComment } from '../utils/is-partition-comment.mjs'
|
|
6
|
+
import { getCommentsBefore } from '../utils/get-comments-before.mjs'
|
|
3
7
|
import { createEslintRule } from '../utils/create-eslint-rule.mjs'
|
|
4
|
-
import {
|
|
8
|
+
import { getLinesBetween } from '../utils/get-lines-between.mjs'
|
|
5
9
|
import { getSourceCode } from '../utils/get-source-code.mjs'
|
|
6
10
|
import { toSingleLine } from '../utils/to-single-line.mjs'
|
|
7
11
|
import { rangeToDiff } from '../utils/range-to-diff.mjs'
|
|
@@ -49,7 +53,7 @@ const sortEnums = createEslintRule({
|
|
|
49
53
|
},
|
|
50
54
|
partitionByComment: {
|
|
51
55
|
description:
|
|
52
|
-
'Allows you to use comments to separate the
|
|
56
|
+
'Allows you to use comments to separate the members of enums into logical groups.',
|
|
53
57
|
anyOf: [
|
|
54
58
|
{
|
|
55
59
|
type: 'array',
|
|
@@ -65,12 +69,19 @@ const sortEnums = createEslintRule({
|
|
|
65
69
|
},
|
|
66
70
|
],
|
|
67
71
|
},
|
|
72
|
+
partitionByNewLine: {
|
|
73
|
+
description:
|
|
74
|
+
'Allows to use spaces to separate the nodes into logical groups.',
|
|
75
|
+
type: 'boolean',
|
|
76
|
+
},
|
|
68
77
|
},
|
|
69
78
|
additionalProperties: false,
|
|
70
79
|
},
|
|
71
80
|
],
|
|
72
81
|
messages: {
|
|
73
82
|
unexpectedEnumsOrder: 'Expected "{{right}}" to come before "{{left}}".',
|
|
83
|
+
unexpectedEnumsDependencyOrder:
|
|
84
|
+
'Expected dependency "{{right}}" to come before "{{nodeDependentOnRight}}".',
|
|
74
85
|
},
|
|
75
86
|
},
|
|
76
87
|
defaultOptions: [
|
|
@@ -80,6 +91,7 @@ const sortEnums = createEslintRule({
|
|
|
80
91
|
ignoreCase: true,
|
|
81
92
|
sortByValue: false,
|
|
82
93
|
partitionByComment: false,
|
|
94
|
+
partitionByNewLine: false,
|
|
83
95
|
forceNumericSort: false,
|
|
84
96
|
},
|
|
85
97
|
],
|
|
@@ -103,6 +115,7 @@ const sortEnums = createEslintRule({
|
|
|
103
115
|
let settings = getSettings(context.settings)
|
|
104
116
|
let options = complete(context.options.at(0), settings, {
|
|
105
117
|
partitionByComment: false,
|
|
118
|
+
partitionByNewLine: false,
|
|
106
119
|
type: 'alphabetical',
|
|
107
120
|
ignoreCase: true,
|
|
108
121
|
order: 'asc',
|
|
@@ -130,24 +143,16 @@ const sortEnums = createEslintRule({
|
|
|
130
143
|
if ('right' in nodeValue) {
|
|
131
144
|
checkNode(nodeValue.right)
|
|
132
145
|
}
|
|
146
|
+
if ('expressions' in nodeValue) {
|
|
147
|
+
nodeValue.expressions.forEach(checkNode)
|
|
148
|
+
}
|
|
133
149
|
}
|
|
134
150
|
checkNode(expression)
|
|
135
151
|
return dependencies
|
|
136
152
|
}
|
|
137
153
|
let formattedMembers = members.reduce(
|
|
138
154
|
(accumulator, member) => {
|
|
139
|
-
|
|
140
|
-
if (
|
|
141
|
-
partitionComment &&
|
|
142
|
-
comment &&
|
|
143
|
-
isPartitionComment(partitionComment, comment.value)
|
|
144
|
-
) {
|
|
145
|
-
accumulator.push([])
|
|
146
|
-
}
|
|
147
|
-
let name =
|
|
148
|
-
member.id.type === 'Literal'
|
|
149
|
-
? `${member.id.value}`
|
|
150
|
-
: `${sourceCode.text.slice(...member.id.range)}`
|
|
155
|
+
var _a
|
|
151
156
|
let dependencies = []
|
|
152
157
|
if (member.initializer) {
|
|
153
158
|
dependencies = extractDependencies(
|
|
@@ -155,11 +160,28 @@ const sortEnums = createEslintRule({
|
|
|
155
160
|
node.id.name,
|
|
156
161
|
)
|
|
157
162
|
}
|
|
163
|
+
let lastSortingNode =
|
|
164
|
+
(_a = accumulator.at(-1)) == null ? void 0 : _a.at(-1)
|
|
158
165
|
let sortingNode = {
|
|
159
166
|
size: rangeToDiff(member.range),
|
|
160
167
|
node: member,
|
|
161
168
|
dependencies,
|
|
162
|
-
name
|
|
169
|
+
name:
|
|
170
|
+
member.id.type === 'Literal'
|
|
171
|
+
? `${member.id.value}`
|
|
172
|
+
: `${sourceCode.text.slice(...member.id.range)}`,
|
|
173
|
+
}
|
|
174
|
+
if (
|
|
175
|
+
(partitionComment &&
|
|
176
|
+
hasPartitionComment(
|
|
177
|
+
partitionComment,
|
|
178
|
+
getCommentsBefore(member, sourceCode),
|
|
179
|
+
)) ||
|
|
180
|
+
(options.partitionByNewLine &&
|
|
181
|
+
lastSortingNode &&
|
|
182
|
+
getLinesBetween(sourceCode, lastSortingNode, sortingNode))
|
|
183
|
+
) {
|
|
184
|
+
accumulator.push([])
|
|
163
185
|
}
|
|
164
186
|
accumulator.at(-1).push(sortingNode)
|
|
165
187
|
return accumulator
|
|
@@ -202,29 +224,38 @@ const sortEnums = createEslintRule({
|
|
|
202
224
|
}
|
|
203
225
|
: void 0,
|
|
204
226
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
sortNodes(
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
227
|
+
let sortedNodes = sortNodesByDependencies(
|
|
228
|
+
formattedMembers
|
|
229
|
+
.map(nodes2 => sortNodes(nodes2, compareOptions))
|
|
230
|
+
.flat(),
|
|
231
|
+
)
|
|
232
|
+
let nodes = formattedMembers.flat()
|
|
233
|
+
pairwise(nodes, (left, right) => {
|
|
234
|
+
let indexOfLeft = sortedNodes.indexOf(left)
|
|
235
|
+
let indexOfRight = sortedNodes.indexOf(right)
|
|
236
|
+
if (indexOfLeft > indexOfRight) {
|
|
237
|
+
let firstUnorderedNodeDependentOnRight =
|
|
238
|
+
getFirstUnorderedNodeDependentOn(right, nodes)
|
|
239
|
+
context.report({
|
|
240
|
+
messageId: firstUnorderedNodeDependentOnRight
|
|
241
|
+
? 'unexpectedEnumsDependencyOrder'
|
|
242
|
+
: 'unexpectedEnumsOrder',
|
|
243
|
+
data: {
|
|
244
|
+
left: toSingleLine(left.name),
|
|
245
|
+
right: toSingleLine(right.name),
|
|
246
|
+
nodeDependentOnRight:
|
|
247
|
+
firstUnorderedNodeDependentOnRight == null
|
|
248
|
+
? void 0
|
|
249
|
+
: firstUnorderedNodeDependentOnRight.name,
|
|
250
|
+
},
|
|
251
|
+
node: right.node,
|
|
252
|
+
fix: fixer =>
|
|
253
|
+
makeFixes(fixer, nodes, sortedNodes, sourceCode, {
|
|
254
|
+
partitionComment,
|
|
255
|
+
}),
|
|
256
|
+
})
|
|
257
|
+
}
|
|
258
|
+
})
|
|
228
259
|
}
|
|
229
260
|
},
|
|
230
261
|
}),
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
'use strict'
|
|
2
|
+
const isPartitionComment = require('../utils/is-partition-comment.js')
|
|
3
|
+
const getCommentsBefore = require('../utils/get-comments-before.js')
|
|
2
4
|
const createEslintRule = require('../utils/create-eslint-rule.js')
|
|
5
|
+
const getLinesBetween = require('../utils/get-lines-between.js')
|
|
3
6
|
const getSourceCode = require('../utils/get-source-code.js')
|
|
4
7
|
const rangeToDiff = require('../utils/range-to-diff.js')
|
|
5
8
|
const getSettings = require('../utils/get-settings.js')
|
|
6
|
-
const isPositive = require('../utils/is-positive.js')
|
|
7
9
|
const sortNodes = require('../utils/sort-nodes.js')
|
|
8
10
|
const makeFixes = require('../utils/make-fixes.js')
|
|
9
11
|
const complete = require('../utils/complete.js')
|
|
10
12
|
const pairwise = require('../utils/pairwise.js')
|
|
11
|
-
const compare = require('../utils/compare.js')
|
|
12
13
|
const sortExports = createEslintRule.createEslintRule({
|
|
13
14
|
name: 'sort-exports',
|
|
14
15
|
meta: {
|
|
@@ -37,6 +38,34 @@ const sortExports = createEslintRule.createEslintRule({
|
|
|
37
38
|
'Controls whether sorting should be case-sensitive or not.',
|
|
38
39
|
type: 'boolean',
|
|
39
40
|
},
|
|
41
|
+
partitionByComment: {
|
|
42
|
+
description:
|
|
43
|
+
'Allows you to use comments to separate the exports into logical groups.',
|
|
44
|
+
anyOf: [
|
|
45
|
+
{
|
|
46
|
+
type: 'array',
|
|
47
|
+
items: {
|
|
48
|
+
type: 'string',
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
type: 'boolean',
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
type: 'string',
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
},
|
|
59
|
+
partitionByNewLine: {
|
|
60
|
+
description:
|
|
61
|
+
'Allows to use spaces to separate the nodes into logical groups.',
|
|
62
|
+
type: 'boolean',
|
|
63
|
+
},
|
|
64
|
+
groupKind: {
|
|
65
|
+
description: 'Specifies top-level groups.',
|
|
66
|
+
type: 'string',
|
|
67
|
+
enum: ['mixed', 'values-first', 'types-first'],
|
|
68
|
+
},
|
|
40
69
|
},
|
|
41
70
|
additionalProperties: false,
|
|
42
71
|
},
|
|
@@ -50,6 +79,9 @@ const sortExports = createEslintRule.createEslintRule({
|
|
|
50
79
|
type: 'alphabetical',
|
|
51
80
|
order: 'asc',
|
|
52
81
|
ignoreCase: true,
|
|
82
|
+
partitionByComment: false,
|
|
83
|
+
partitionByNewLine: false,
|
|
84
|
+
groupKind: 'mixed',
|
|
53
85
|
},
|
|
54
86
|
],
|
|
55
87
|
create: context => {
|
|
@@ -58,14 +90,34 @@ const sortExports = createEslintRule.createEslintRule({
|
|
|
58
90
|
type: 'alphabetical',
|
|
59
91
|
ignoreCase: true,
|
|
60
92
|
order: 'asc',
|
|
93
|
+
partitionByComment: false,
|
|
94
|
+
partitionByNewLine: false,
|
|
95
|
+
groupKind: 'mixed',
|
|
61
96
|
})
|
|
97
|
+
let sourceCode = getSourceCode.getSourceCode(context)
|
|
98
|
+
let partitionComment = options.partitionByComment
|
|
62
99
|
let parts = [[]]
|
|
63
100
|
let registerNode = node => {
|
|
64
|
-
|
|
101
|
+
var _a
|
|
102
|
+
let sortingNode = {
|
|
65
103
|
size: rangeToDiff.rangeToDiff(node.range),
|
|
66
104
|
name: node.source.value,
|
|
67
105
|
node,
|
|
68
|
-
}
|
|
106
|
+
}
|
|
107
|
+
let lastNode = (_a = parts.at(-1)) == null ? void 0 : _a.at(-1)
|
|
108
|
+
if (
|
|
109
|
+
(partitionComment &&
|
|
110
|
+
isPartitionComment.hasPartitionComment(
|
|
111
|
+
partitionComment,
|
|
112
|
+
getCommentsBefore.getCommentsBefore(node, sourceCode),
|
|
113
|
+
)) ||
|
|
114
|
+
(options.partitionByNewLine &&
|
|
115
|
+
lastNode &&
|
|
116
|
+
getLinesBetween.getLinesBetween(sourceCode, lastNode, sortingNode))
|
|
117
|
+
) {
|
|
118
|
+
parts.push([])
|
|
119
|
+
}
|
|
120
|
+
parts.at(-1).push(sortingNode)
|
|
69
121
|
}
|
|
70
122
|
return {
|
|
71
123
|
ExportAllDeclaration: registerNode,
|
|
@@ -75,10 +127,37 @@ const sortExports = createEslintRule.createEslintRule({
|
|
|
75
127
|
}
|
|
76
128
|
},
|
|
77
129
|
'Program:exit': () => {
|
|
78
|
-
let sourceCode = getSourceCode.getSourceCode(context)
|
|
79
130
|
for (let nodes of parts) {
|
|
131
|
+
let groupedByKind
|
|
132
|
+
if (options.groupKind !== 'mixed') {
|
|
133
|
+
groupedByKind = nodes.reduce(
|
|
134
|
+
(accumulator, currentNode) => {
|
|
135
|
+
let exportTypeIndex =
|
|
136
|
+
options.groupKind === 'types-first' ? 0 : 1
|
|
137
|
+
let exportIndex = options.groupKind === 'types-first' ? 1 : 0
|
|
138
|
+
if (currentNode.node.exportKind === 'value') {
|
|
139
|
+
accumulator[exportIndex].push(currentNode)
|
|
140
|
+
} else {
|
|
141
|
+
accumulator[exportTypeIndex].push(currentNode)
|
|
142
|
+
}
|
|
143
|
+
return accumulator
|
|
144
|
+
},
|
|
145
|
+
[[], []],
|
|
146
|
+
)
|
|
147
|
+
} else {
|
|
148
|
+
groupedByKind = [nodes]
|
|
149
|
+
}
|
|
150
|
+
let sortedNodes = []
|
|
151
|
+
for (let nodesByKind of groupedByKind) {
|
|
152
|
+
sortedNodes = [
|
|
153
|
+
...sortedNodes,
|
|
154
|
+
...sortNodes.sortNodes(nodesByKind, options),
|
|
155
|
+
]
|
|
156
|
+
}
|
|
80
157
|
pairwise.pairwise(nodes, (left, right) => {
|
|
81
|
-
|
|
158
|
+
let indexOfLeft = sortedNodes.indexOf(left)
|
|
159
|
+
let indexOfRight = sortedNodes.indexOf(right)
|
|
160
|
+
if (indexOfLeft > indexOfRight) {
|
|
82
161
|
context.report({
|
|
83
162
|
messageId: 'unexpectedExportsOrder',
|
|
84
163
|
data: {
|
|
@@ -87,12 +166,9 @@ const sortExports = createEslintRule.createEslintRule({
|
|
|
87
166
|
},
|
|
88
167
|
node: right.node,
|
|
89
168
|
fix: fixer =>
|
|
90
|
-
makeFixes.makeFixes(
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
sortNodes.sortNodes(nodes, options),
|
|
94
|
-
sourceCode,
|
|
95
|
-
),
|
|
169
|
+
makeFixes.makeFixes(fixer, nodes, sortedNodes, sourceCode, {
|
|
170
|
+
partitionComment,
|
|
171
|
+
}),
|
|
96
172
|
})
|
|
97
173
|
}
|
|
98
174
|
})
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
+
import { hasPartitionComment } from '../utils/is-partition-comment.mjs'
|
|
2
|
+
import { getCommentsBefore } from '../utils/get-comments-before.mjs'
|
|
1
3
|
import { createEslintRule } from '../utils/create-eslint-rule.mjs'
|
|
4
|
+
import { getLinesBetween } from '../utils/get-lines-between.mjs'
|
|
2
5
|
import { getSourceCode } from '../utils/get-source-code.mjs'
|
|
3
6
|
import { rangeToDiff } from '../utils/range-to-diff.mjs'
|
|
4
7
|
import { getSettings } from '../utils/get-settings.mjs'
|
|
5
|
-
import { isPositive } from '../utils/is-positive.mjs'
|
|
6
8
|
import { sortNodes } from '../utils/sort-nodes.mjs'
|
|
7
9
|
import { makeFixes } from '../utils/make-fixes.mjs'
|
|
8
10
|
import { complete } from '../utils/complete.mjs'
|
|
9
11
|
import { pairwise } from '../utils/pairwise.mjs'
|
|
10
|
-
import { compare } from '../utils/compare.mjs'
|
|
11
12
|
const sortExports = createEslintRule({
|
|
12
13
|
name: 'sort-exports',
|
|
13
14
|
meta: {
|
|
@@ -36,6 +37,34 @@ const sortExports = createEslintRule({
|
|
|
36
37
|
'Controls whether sorting should be case-sensitive or not.',
|
|
37
38
|
type: 'boolean',
|
|
38
39
|
},
|
|
40
|
+
partitionByComment: {
|
|
41
|
+
description:
|
|
42
|
+
'Allows you to use comments to separate the exports into logical groups.',
|
|
43
|
+
anyOf: [
|
|
44
|
+
{
|
|
45
|
+
type: 'array',
|
|
46
|
+
items: {
|
|
47
|
+
type: 'string',
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
type: 'boolean',
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
type: 'string',
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
},
|
|
58
|
+
partitionByNewLine: {
|
|
59
|
+
description:
|
|
60
|
+
'Allows to use spaces to separate the nodes into logical groups.',
|
|
61
|
+
type: 'boolean',
|
|
62
|
+
},
|
|
63
|
+
groupKind: {
|
|
64
|
+
description: 'Specifies top-level groups.',
|
|
65
|
+
type: 'string',
|
|
66
|
+
enum: ['mixed', 'values-first', 'types-first'],
|
|
67
|
+
},
|
|
39
68
|
},
|
|
40
69
|
additionalProperties: false,
|
|
41
70
|
},
|
|
@@ -49,6 +78,9 @@ const sortExports = createEslintRule({
|
|
|
49
78
|
type: 'alphabetical',
|
|
50
79
|
order: 'asc',
|
|
51
80
|
ignoreCase: true,
|
|
81
|
+
partitionByComment: false,
|
|
82
|
+
partitionByNewLine: false,
|
|
83
|
+
groupKind: 'mixed',
|
|
52
84
|
},
|
|
53
85
|
],
|
|
54
86
|
create: context => {
|
|
@@ -57,14 +89,34 @@ const sortExports = createEslintRule({
|
|
|
57
89
|
type: 'alphabetical',
|
|
58
90
|
ignoreCase: true,
|
|
59
91
|
order: 'asc',
|
|
92
|
+
partitionByComment: false,
|
|
93
|
+
partitionByNewLine: false,
|
|
94
|
+
groupKind: 'mixed',
|
|
60
95
|
})
|
|
96
|
+
let sourceCode = getSourceCode(context)
|
|
97
|
+
let partitionComment = options.partitionByComment
|
|
61
98
|
let parts = [[]]
|
|
62
99
|
let registerNode = node => {
|
|
63
|
-
|
|
100
|
+
var _a
|
|
101
|
+
let sortingNode = {
|
|
64
102
|
size: rangeToDiff(node.range),
|
|
65
103
|
name: node.source.value,
|
|
66
104
|
node,
|
|
67
|
-
}
|
|
105
|
+
}
|
|
106
|
+
let lastNode = (_a = parts.at(-1)) == null ? void 0 : _a.at(-1)
|
|
107
|
+
if (
|
|
108
|
+
(partitionComment &&
|
|
109
|
+
hasPartitionComment(
|
|
110
|
+
partitionComment,
|
|
111
|
+
getCommentsBefore(node, sourceCode),
|
|
112
|
+
)) ||
|
|
113
|
+
(options.partitionByNewLine &&
|
|
114
|
+
lastNode &&
|
|
115
|
+
getLinesBetween(sourceCode, lastNode, sortingNode))
|
|
116
|
+
) {
|
|
117
|
+
parts.push([])
|
|
118
|
+
}
|
|
119
|
+
parts.at(-1).push(sortingNode)
|
|
68
120
|
}
|
|
69
121
|
return {
|
|
70
122
|
ExportAllDeclaration: registerNode,
|
|
@@ -74,10 +126,34 @@ const sortExports = createEslintRule({
|
|
|
74
126
|
}
|
|
75
127
|
},
|
|
76
128
|
'Program:exit': () => {
|
|
77
|
-
let sourceCode = getSourceCode(context)
|
|
78
129
|
for (let nodes of parts) {
|
|
130
|
+
let groupedByKind
|
|
131
|
+
if (options.groupKind !== 'mixed') {
|
|
132
|
+
groupedByKind = nodes.reduce(
|
|
133
|
+
(accumulator, currentNode) => {
|
|
134
|
+
let exportTypeIndex =
|
|
135
|
+
options.groupKind === 'types-first' ? 0 : 1
|
|
136
|
+
let exportIndex = options.groupKind === 'types-first' ? 1 : 0
|
|
137
|
+
if (currentNode.node.exportKind === 'value') {
|
|
138
|
+
accumulator[exportIndex].push(currentNode)
|
|
139
|
+
} else {
|
|
140
|
+
accumulator[exportTypeIndex].push(currentNode)
|
|
141
|
+
}
|
|
142
|
+
return accumulator
|
|
143
|
+
},
|
|
144
|
+
[[], []],
|
|
145
|
+
)
|
|
146
|
+
} else {
|
|
147
|
+
groupedByKind = [nodes]
|
|
148
|
+
}
|
|
149
|
+
let sortedNodes = []
|
|
150
|
+
for (let nodesByKind of groupedByKind) {
|
|
151
|
+
sortedNodes = [...sortedNodes, ...sortNodes(nodesByKind, options)]
|
|
152
|
+
}
|
|
79
153
|
pairwise(nodes, (left, right) => {
|
|
80
|
-
|
|
154
|
+
let indexOfLeft = sortedNodes.indexOf(left)
|
|
155
|
+
let indexOfRight = sortedNodes.indexOf(right)
|
|
156
|
+
if (indexOfLeft > indexOfRight) {
|
|
81
157
|
context.report({
|
|
82
158
|
messageId: 'unexpectedExportsOrder',
|
|
83
159
|
data: {
|
|
@@ -86,12 +162,9 @@ const sortExports = createEslintRule({
|
|
|
86
162
|
},
|
|
87
163
|
node: right.node,
|
|
88
164
|
fix: fixer =>
|
|
89
|
-
makeFixes(
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
sortNodes(nodes, options),
|
|
93
|
-
sourceCode,
|
|
94
|
-
),
|
|
165
|
+
makeFixes(fixer, nodes, sortedNodes, sourceCode, {
|
|
166
|
+
partitionComment,
|
|
167
|
+
}),
|
|
95
168
|
})
|
|
96
169
|
}
|
|
97
170
|
})
|