eslint-plugin-perfectionist 4.1.2 → 4.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/alphabet.d.ts +176 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +9 -1
- package/dist/rules/sort-array-includes.js +6 -1
- package/dist/rules/sort-classes-utils.js +0 -23
- package/dist/rules/sort-classes.js +19 -43
- package/dist/rules/sort-classes.types.js +5 -37
- package/dist/rules/sort-decorators.js +9 -1
- package/dist/rules/sort-enums.js +11 -1
- package/dist/rules/sort-exports.js +8 -1
- package/dist/rules/sort-heritage-clauses.js +4 -0
- package/dist/rules/sort-imports.js +8 -3
- package/dist/rules/sort-interfaces.js +17 -260
- package/dist/rules/sort-intersection-types.js +9 -1
- package/dist/rules/sort-jsx-props.js +4 -0
- package/dist/rules/sort-maps.js +8 -1
- package/dist/rules/sort-modules-utils.js +0 -23
- package/dist/rules/sort-modules.js +20 -44
- package/dist/rules/sort-modules.types.js +5 -41
- package/dist/rules/sort-named-exports.js +8 -1
- package/dist/rules/sort-named-imports.js +8 -1
- package/dist/rules/sort-object-types-utils.js +37 -0
- package/dist/rules/sort-object-types.js +345 -223
- package/dist/rules/sort-object-types.types.js +20 -0
- package/dist/rules/sort-objects.js +36 -10
- package/dist/rules/sort-switch-case.js +4 -0
- package/dist/rules/sort-union-types.js +9 -1
- package/dist/rules/sort-variable-declarations.js +8 -1
- package/dist/utils/alphabet.js +385 -0
- package/dist/utils/common-json-schemas.js +94 -1
- package/dist/utils/compare.js +88 -39
- package/dist/utils/convert-boolean-to-sign.js +4 -0
- package/dist/utils/get-comments-before.js +4 -4
- package/dist/utils/get-custom-groups-compare-options.js +29 -0
- package/dist/utils/get-matching-context-options.js +18 -0
- package/dist/utils/get-node-range.js +18 -7
- package/dist/utils/get-settings.js +1 -0
- package/dist/utils/is-node-function-type.js +15 -0
- package/dist/utils/make-fixes.js +24 -3
- package/dist/utils/make-newlines-fixes.js +7 -4
- package/dist/utils/validate-custom-sort-configuration.js +11 -0
- package/dist/{rules → utils}/validate-generated-groups-configuration.js +4 -2
- package/package.json +9 -5
- package/readme.md +4 -3
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
|
|
3
3
|
let typeJsonSchema = {
|
|
4
|
-
enum: ['alphabetical', 'natural', 'line-length'],
|
|
4
|
+
enum: ['alphabetical', 'natural', 'line-length', 'custom'],
|
|
5
5
|
description: 'Specifies the sorting method.',
|
|
6
6
|
type: 'string',
|
|
7
7
|
}
|
|
@@ -11,6 +11,10 @@ let orderJsonSchema = {
|
|
|
11
11
|
enum: ['asc', 'desc'],
|
|
12
12
|
type: 'string',
|
|
13
13
|
}
|
|
14
|
+
let alphabetJsonSchema = {
|
|
15
|
+
description: 'Alphabet to use for the `custom` sort type.',
|
|
16
|
+
type: 'string',
|
|
17
|
+
}
|
|
14
18
|
let localesJsonSchema = {
|
|
15
19
|
oneOf: [
|
|
16
20
|
{
|
|
@@ -96,7 +100,95 @@ let newlinesBetweenJsonSchema = {
|
|
|
96
100
|
enum: ['ignore', 'always', 'never'],
|
|
97
101
|
type: 'string',
|
|
98
102
|
}
|
|
103
|
+
let useConfigurationIfJsonSchema = {
|
|
104
|
+
properties: {
|
|
105
|
+
allNamesMatchPattern: {
|
|
106
|
+
type: 'string',
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
additionalProperties: false,
|
|
110
|
+
type: 'object',
|
|
111
|
+
}
|
|
112
|
+
let customGroupSortJsonSchema = {
|
|
113
|
+
type: {
|
|
114
|
+
enum: ['alphabetical', 'line-length', 'natural', 'unsorted'],
|
|
115
|
+
description: 'Custom group sort type.',
|
|
116
|
+
type: 'string',
|
|
117
|
+
},
|
|
118
|
+
order: {
|
|
119
|
+
description: 'Custom group sort order.',
|
|
120
|
+
enum: ['desc', 'asc'],
|
|
121
|
+
type: 'string',
|
|
122
|
+
},
|
|
123
|
+
}
|
|
124
|
+
let customGroupNameJsonSchema = {
|
|
125
|
+
groupName: {
|
|
126
|
+
description: 'Custom group name.',
|
|
127
|
+
type: 'string',
|
|
128
|
+
},
|
|
129
|
+
}
|
|
130
|
+
let buildCustomGroupsArrayJsonSchema = ({ singleCustomGroupJsonSchema }) => ({
|
|
131
|
+
items: {
|
|
132
|
+
oneOf: [
|
|
133
|
+
{
|
|
134
|
+
properties: {
|
|
135
|
+
...customGroupNameJsonSchema,
|
|
136
|
+
...customGroupSortJsonSchema,
|
|
137
|
+
anyOf: {
|
|
138
|
+
items: {
|
|
139
|
+
properties: {
|
|
140
|
+
...singleCustomGroupJsonSchema,
|
|
141
|
+
},
|
|
142
|
+
description: 'Custom group.',
|
|
143
|
+
additionalProperties: false,
|
|
144
|
+
type: 'object',
|
|
145
|
+
},
|
|
146
|
+
type: 'array',
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
description: 'Custom group block.',
|
|
150
|
+
additionalProperties: false,
|
|
151
|
+
type: 'object',
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
properties: {
|
|
155
|
+
...customGroupNameJsonSchema,
|
|
156
|
+
...customGroupSortJsonSchema,
|
|
157
|
+
...singleCustomGroupJsonSchema,
|
|
158
|
+
},
|
|
159
|
+
description: 'Custom group.',
|
|
160
|
+
additionalProperties: false,
|
|
161
|
+
type: 'object',
|
|
162
|
+
},
|
|
163
|
+
],
|
|
164
|
+
},
|
|
165
|
+
description: 'Specifies custom groups.',
|
|
166
|
+
type: 'array',
|
|
167
|
+
})
|
|
168
|
+
let buildCustomGroupModifiersJsonSchema = modifiers => ({
|
|
169
|
+
items: {
|
|
170
|
+
enum: modifiers,
|
|
171
|
+
type: 'string',
|
|
172
|
+
},
|
|
173
|
+
description: 'Modifier filters.',
|
|
174
|
+
type: 'array',
|
|
175
|
+
})
|
|
176
|
+
let buildCustomGroupSelectorJsonSchema = selectors => ({
|
|
177
|
+
description: 'Selector filter.',
|
|
178
|
+
enum: selectors,
|
|
179
|
+
type: 'string',
|
|
180
|
+
})
|
|
181
|
+
let elementNamePatternJsonSchema = {
|
|
182
|
+
description: 'Element name pattern filter.',
|
|
183
|
+
type: 'string',
|
|
184
|
+
}
|
|
185
|
+
exports.alphabetJsonSchema = alphabetJsonSchema
|
|
186
|
+
exports.buildCustomGroupModifiersJsonSchema =
|
|
187
|
+
buildCustomGroupModifiersJsonSchema
|
|
188
|
+
exports.buildCustomGroupSelectorJsonSchema = buildCustomGroupSelectorJsonSchema
|
|
189
|
+
exports.buildCustomGroupsArrayJsonSchema = buildCustomGroupsArrayJsonSchema
|
|
99
190
|
exports.customGroupsJsonSchema = customGroupsJsonSchema
|
|
191
|
+
exports.elementNamePatternJsonSchema = elementNamePatternJsonSchema
|
|
100
192
|
exports.groupsJsonSchema = groupsJsonSchema
|
|
101
193
|
exports.ignoreCaseJsonSchema = ignoreCaseJsonSchema
|
|
102
194
|
exports.localesJsonSchema = localesJsonSchema
|
|
@@ -106,3 +198,4 @@ exports.partitionByCommentJsonSchema = partitionByCommentJsonSchema
|
|
|
106
198
|
exports.partitionByNewLineJsonSchema = partitionByNewLineJsonSchema
|
|
107
199
|
exports.specialCharactersJsonSchema = specialCharactersJsonSchema
|
|
108
200
|
exports.typeJsonSchema = typeJsonSchema
|
|
201
|
+
exports.useConfigurationIfJsonSchema = useConfigurationIfJsonSchema
|
package/dist/utils/compare.js
CHANGED
|
@@ -1,54 +1,103 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
|
|
3
3
|
const naturalOrderby = require('natural-orderby')
|
|
4
|
+
const convertBooleanToSign = require('./convert-boolean-to-sign.js')
|
|
5
|
+
let alphabetCache = /* @__PURE__ */ new Map()
|
|
4
6
|
let compare = (a, b, options) => {
|
|
5
|
-
let orderCoefficient = options.order === 'asc' ? 1 : -1
|
|
6
7
|
let sortingFunction
|
|
7
8
|
let nodeValueGetter = options.nodeValueGetter ?? (node => node.name)
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
options
|
|
11
|
-
|
|
9
|
+
switch (options.type) {
|
|
10
|
+
case 'alphabetical':
|
|
11
|
+
sortingFunction = getAlphabeticalSortingFunction(options, nodeValueGetter)
|
|
12
|
+
break
|
|
13
|
+
case 'natural':
|
|
14
|
+
sortingFunction = getNaturalSortingFunction(options, nodeValueGetter)
|
|
15
|
+
break
|
|
16
|
+
case 'custom':
|
|
17
|
+
sortingFunction = getCustomSortingFunction(options, nodeValueGetter)
|
|
18
|
+
break
|
|
19
|
+
case 'line-length':
|
|
20
|
+
sortingFunction = getLineLengthSortingFunction(options, nodeValueGetter)
|
|
21
|
+
}
|
|
22
|
+
return (
|
|
23
|
+
convertBooleanToSign.convertBooleanToSign(options.order === 'asc') *
|
|
24
|
+
sortingFunction(a, b)
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
let getAlphabeticalSortingFunction = (
|
|
28
|
+
{ specialCharacters, ignoreCase, locales },
|
|
29
|
+
nodeValueGetter,
|
|
30
|
+
) => {
|
|
31
|
+
let formatString = getFormatStringFunction(ignoreCase, specialCharacters)
|
|
32
|
+
return (aNode, bNode) =>
|
|
33
|
+
formatString(nodeValueGetter(aNode)).localeCompare(
|
|
34
|
+
formatString(nodeValueGetter(bNode)),
|
|
35
|
+
locales,
|
|
12
36
|
)
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
37
|
+
}
|
|
38
|
+
let getNaturalSortingFunction = (
|
|
39
|
+
{ specialCharacters, ignoreCase, locales },
|
|
40
|
+
nodeValueGetter,
|
|
41
|
+
) => {
|
|
42
|
+
let naturalCompare = naturalOrderby.compare({
|
|
43
|
+
locale: locales.toString(),
|
|
44
|
+
})
|
|
45
|
+
let formatString = getFormatStringFunction(ignoreCase, specialCharacters)
|
|
46
|
+
return (aNode, bNode) =>
|
|
47
|
+
naturalCompare(
|
|
48
|
+
formatString(nodeValueGetter(aNode)),
|
|
49
|
+
formatString(nodeValueGetter(bNode)),
|
|
26
50
|
)
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
51
|
+
}
|
|
52
|
+
let getCustomSortingFunction = (
|
|
53
|
+
{ specialCharacters, ignoreCase, alphabet },
|
|
54
|
+
nodeValueGetter,
|
|
55
|
+
) => {
|
|
56
|
+
let formatString = getFormatStringFunction(ignoreCase, specialCharacters)
|
|
57
|
+
let indexByCharacters = alphabetCache.get(alphabet)
|
|
58
|
+
if (!indexByCharacters) {
|
|
59
|
+
indexByCharacters = /* @__PURE__ */ new Map()
|
|
60
|
+
for (let [index, character] of [...alphabet].entries()) {
|
|
61
|
+
indexByCharacters.set(character, index)
|
|
62
|
+
}
|
|
63
|
+
alphabetCache.set(alphabet, indexByCharacters)
|
|
64
|
+
}
|
|
65
|
+
return (aNode, bNode) => {
|
|
66
|
+
let aValue = formatString(nodeValueGetter(aNode))
|
|
67
|
+
let bValue = formatString(nodeValueGetter(bNode))
|
|
68
|
+
for (let i = 0; i < aValue.length; i++) {
|
|
69
|
+
let aCharacter = aValue[i]
|
|
70
|
+
let bCharacter = bValue[i]
|
|
71
|
+
let indexOfA = indexByCharacters.get(aCharacter)
|
|
72
|
+
let indexOfB = indexByCharacters.get(bCharacter)
|
|
73
|
+
indexOfA ?? (indexOfA = Infinity)
|
|
74
|
+
indexOfB ?? (indexOfB = Infinity)
|
|
75
|
+
if (indexOfA !== indexOfB) {
|
|
76
|
+
return convertBooleanToSign.convertBooleanToSign(
|
|
77
|
+
indexOfA - indexOfB > 0,
|
|
78
|
+
)
|
|
46
79
|
}
|
|
47
|
-
return aSize - bSize
|
|
48
80
|
}
|
|
81
|
+
return 0
|
|
49
82
|
}
|
|
50
|
-
return orderCoefficient * sortingFunction(a, b)
|
|
51
83
|
}
|
|
84
|
+
let getLineLengthSortingFunction =
|
|
85
|
+
({ maxLineLength }, nodeValueGetter) =>
|
|
86
|
+
(aNode, bNode) => {
|
|
87
|
+
let aSize = aNode.size
|
|
88
|
+
let bSize = bNode.size
|
|
89
|
+
if (maxLineLength) {
|
|
90
|
+
let isTooLong = (size, node) =>
|
|
91
|
+
size > maxLineLength && node.hasMultipleImportDeclarations
|
|
92
|
+
if (isTooLong(aSize, aNode)) {
|
|
93
|
+
aSize = nodeValueGetter(aNode).length + 10
|
|
94
|
+
}
|
|
95
|
+
if (isTooLong(bSize, bNode)) {
|
|
96
|
+
bSize = nodeValueGetter(bNode).length + 10
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return aSize - bSize
|
|
100
|
+
}
|
|
52
101
|
let getFormatStringFunction = (ignoreCase, specialCharacters) => value => {
|
|
53
102
|
let valueToCompare = value
|
|
54
103
|
if (ignoreCase) {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
|
|
3
|
-
let getCommentsBefore = (
|
|
4
|
-
let commentsBefore = getCommentsBeforeNodeOrToken(
|
|
5
|
-
let tokenBeforeNode =
|
|
3
|
+
let getCommentsBefore = ({ tokenValueToIgnoreBefore, sourceCode, node }) => {
|
|
4
|
+
let commentsBefore = getCommentsBeforeNodeOrToken(sourceCode, node)
|
|
5
|
+
let tokenBeforeNode = sourceCode.getTokenBefore(node)
|
|
6
6
|
if (
|
|
7
7
|
commentsBefore.length ||
|
|
8
8
|
!tokenValueToIgnoreBefore ||
|
|
@@ -11,7 +11,7 @@ let getCommentsBefore = (node, source, tokenValueToIgnoreBefore) => {
|
|
|
11
11
|
) {
|
|
12
12
|
return commentsBefore
|
|
13
13
|
}
|
|
14
|
-
return getCommentsBeforeNodeOrToken(
|
|
14
|
+
return getCommentsBeforeNodeOrToken(sourceCode, tokenBeforeNode)
|
|
15
15
|
}
|
|
16
16
|
let getCommentsBeforeNodeOrToken = (source, node) =>
|
|
17
17
|
source.getCommentsBefore(node).filter(comment => {
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
|
|
3
|
+
let getCustomGroupsCompareOptions = (options, groupNumber) => {
|
|
4
|
+
if (!Array.isArray(options.customGroups)) {
|
|
5
|
+
return options
|
|
6
|
+
}
|
|
7
|
+
let group = options.groups[groupNumber]
|
|
8
|
+
let customGroup =
|
|
9
|
+
typeof group === 'string'
|
|
10
|
+
? options.customGroups.find(
|
|
11
|
+
currentGroup => group === currentGroup.groupName,
|
|
12
|
+
)
|
|
13
|
+
: null
|
|
14
|
+
if ((customGroup == null ? void 0 : customGroup.type) === 'unsorted') {
|
|
15
|
+
return null
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
order:
|
|
19
|
+
customGroup && 'order' in customGroup && customGroup.order
|
|
20
|
+
? customGroup.order
|
|
21
|
+
: options.order,
|
|
22
|
+
specialCharacters: options.specialCharacters,
|
|
23
|
+
type: (customGroup == null ? void 0 : customGroup.type) ?? options.type,
|
|
24
|
+
ignoreCase: options.ignoreCase,
|
|
25
|
+
alphabet: options.alphabet,
|
|
26
|
+
locales: options.locales,
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.getCustomGroupsCompareOptions = getCustomGroupsCompareOptions
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
|
|
3
|
+
const matches = require('./matches.js')
|
|
4
|
+
let getMatchingContextOptions = ({ contextOptions, nodeNames }) =>
|
|
5
|
+
contextOptions.find(options => {
|
|
6
|
+
var _a
|
|
7
|
+
let allNamesMatchPattern =
|
|
8
|
+
(_a = options.useConfigurationIf) == null
|
|
9
|
+
? void 0
|
|
10
|
+
: _a.allNamesMatchPattern
|
|
11
|
+
return (
|
|
12
|
+
!allNamesMatchPattern ||
|
|
13
|
+
nodeNames.every(nodeName =>
|
|
14
|
+
matches.matches(nodeName, allNamesMatchPattern),
|
|
15
|
+
)
|
|
16
|
+
)
|
|
17
|
+
})
|
|
18
|
+
exports.getMatchingContextOptions = getMatchingContextOptions
|
|
@@ -4,7 +4,12 @@ const utils = require('@typescript-eslint/utils')
|
|
|
4
4
|
const getEslintDisabledRules = require('./get-eslint-disabled-rules.js')
|
|
5
5
|
const isPartitionComment = require('./is-partition-comment.js')
|
|
6
6
|
const getCommentsBefore = require('./get-comments-before.js')
|
|
7
|
-
let getNodeRange = (
|
|
7
|
+
let getNodeRange = ({
|
|
8
|
+
ignoreHighestBlockComment,
|
|
9
|
+
sourceCode,
|
|
10
|
+
options,
|
|
11
|
+
node,
|
|
12
|
+
}) => {
|
|
8
13
|
let start = node.range.at(0)
|
|
9
14
|
let end = node.range.at(1)
|
|
10
15
|
if (utils.ASTUtils.isParenthesized(node, sourceCode)) {
|
|
@@ -19,11 +24,11 @@ let getNodeRange = (node, sourceCode, additionalOptions) => {
|
|
|
19
24
|
start = bodyOpeningParen.range.at(0)
|
|
20
25
|
end = bodyClosingParen.range.at(1)
|
|
21
26
|
}
|
|
22
|
-
let comments = getCommentsBefore.getCommentsBefore(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
+
let comments = getCommentsBefore.getCommentsBefore({
|
|
28
|
+
sourceCode,
|
|
29
|
+
node,
|
|
30
|
+
})
|
|
31
|
+
let highestBlockComment = comments.find(comment => comment.type === 'Block')
|
|
27
32
|
let relevantTopComment
|
|
28
33
|
for (let i = comments.length - 1; i >= 0; i--) {
|
|
29
34
|
let comment = comments[i]
|
|
@@ -31,7 +36,10 @@ let getNodeRange = (node, sourceCode, additionalOptions) => {
|
|
|
31
36
|
comment.value,
|
|
32
37
|
)
|
|
33
38
|
if (
|
|
34
|
-
isPartitionComment.isPartitionComment(
|
|
39
|
+
isPartitionComment.isPartitionComment(
|
|
40
|
+
(options == null ? void 0 : options.partitionByComment) ?? false,
|
|
41
|
+
comment.value,
|
|
42
|
+
) ||
|
|
35
43
|
(eslintDisabledRules == null
|
|
36
44
|
? void 0
|
|
37
45
|
: eslintDisabledRules.eslintDisableDirective) === 'eslint-disable' ||
|
|
@@ -48,6 +56,9 @@ let getNodeRange = (node, sourceCode, additionalOptions) => {
|
|
|
48
56
|
if (comment.loc.end.line !== previousCommentOrNodeStartLine - 1) {
|
|
49
57
|
break
|
|
50
58
|
}
|
|
59
|
+
if (ignoreHighestBlockComment && comment === highestBlockComment) {
|
|
60
|
+
break
|
|
61
|
+
}
|
|
51
62
|
relevantTopComment = comment
|
|
52
63
|
}
|
|
53
64
|
if (relevantTopComment) {
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
|
|
3
|
+
let isNodeFunctionType = node => {
|
|
4
|
+
if (node.type === 'TSMethodSignature' || node.type === 'TSFunctionType') {
|
|
5
|
+
return true
|
|
6
|
+
}
|
|
7
|
+
if (node.type === 'TSUnionType' || node.type === 'TSIntersectionType') {
|
|
8
|
+
return node.types.every(isNodeFunctionType)
|
|
9
|
+
}
|
|
10
|
+
if (node.type === 'TSPropertySignature' && node.typeAnnotation) {
|
|
11
|
+
return isNodeFunctionType(node.typeAnnotation.typeAnnotation)
|
|
12
|
+
}
|
|
13
|
+
return false
|
|
14
|
+
}
|
|
15
|
+
exports.isNodeFunctionType = isNodeFunctionType
|
package/dist/utils/make-fixes.js
CHANGED
|
@@ -2,18 +2,33 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
|
|
3
3
|
const makeCommentAfterFixes = require('./make-comment-after-fixes.js')
|
|
4
4
|
const getNodeRange = require('./get-node-range.js')
|
|
5
|
-
let makeFixes = ({
|
|
5
|
+
let makeFixes = ({
|
|
6
|
+
ignoreFirstNodeHighestBlockComment,
|
|
7
|
+
sortedNodes,
|
|
8
|
+
sourceCode,
|
|
9
|
+
options,
|
|
10
|
+
fixer,
|
|
11
|
+
nodes,
|
|
12
|
+
}) => {
|
|
6
13
|
let fixes = []
|
|
7
14
|
for (let max = nodes.length, i = 0; i < max; i++) {
|
|
8
15
|
let sortingNode = nodes.at(i)
|
|
9
16
|
let sortedSortingNode = sortedNodes.at(i)
|
|
10
17
|
let { node } = sortingNode
|
|
11
18
|
let { addSafetySemicolonWhenInline, node: sortedNode } = sortedSortingNode
|
|
19
|
+
let isNodeFirstNode = node === nodes.at(0).node
|
|
20
|
+
let isSortedNodeFirstNode = sortedNode === nodes.at(0).node
|
|
12
21
|
if (node === sortedNode) {
|
|
13
22
|
continue
|
|
14
23
|
}
|
|
15
24
|
let sortedNodeCode = sourceCode.text.slice(
|
|
16
|
-
...getNodeRange.getNodeRange(
|
|
25
|
+
...getNodeRange.getNodeRange({
|
|
26
|
+
ignoreHighestBlockComment:
|
|
27
|
+
ignoreFirstNodeHighestBlockComment && isSortedNodeFirstNode,
|
|
28
|
+
node: sortedNode,
|
|
29
|
+
sourceCode,
|
|
30
|
+
options,
|
|
31
|
+
}),
|
|
17
32
|
)
|
|
18
33
|
let sortedNodeText = sourceCode.getText(sortedNode)
|
|
19
34
|
let tokensAfter = sourceCode.getTokensAfter(node, {
|
|
@@ -39,7 +54,13 @@ let makeFixes = ({ sortedNodes, sourceCode, options, fixer, nodes }) => {
|
|
|
39
54
|
}
|
|
40
55
|
fixes.push(
|
|
41
56
|
fixer.replaceTextRange(
|
|
42
|
-
getNodeRange.getNodeRange(
|
|
57
|
+
getNodeRange.getNodeRange({
|
|
58
|
+
ignoreHighestBlockComment:
|
|
59
|
+
ignoreFirstNodeHighestBlockComment && isNodeFirstNode,
|
|
60
|
+
sourceCode,
|
|
61
|
+
options,
|
|
62
|
+
node,
|
|
63
|
+
}),
|
|
43
64
|
sortedNodeCode,
|
|
44
65
|
),
|
|
45
66
|
)
|
|
@@ -25,12 +25,15 @@ let makeNewlinesFixes = ({
|
|
|
25
25
|
options.groups,
|
|
26
26
|
nextSortingNode,
|
|
27
27
|
)
|
|
28
|
-
let currentNodeRange = getNodeRange.getNodeRange(
|
|
29
|
-
nodes.at(i).node,
|
|
28
|
+
let currentNodeRange = getNodeRange.getNodeRange({
|
|
29
|
+
node: nodes.at(i).node,
|
|
30
30
|
sourceCode,
|
|
31
|
-
)
|
|
31
|
+
})
|
|
32
32
|
let nextNodeRangeStart = getNodeRange
|
|
33
|
-
.getNodeRange(
|
|
33
|
+
.getNodeRange({
|
|
34
|
+
node: nodes.at(i + 1).node,
|
|
35
|
+
sourceCode,
|
|
36
|
+
})
|
|
34
37
|
.at(0)
|
|
35
38
|
let rangeToReplace = [currentNodeRange.at(1), nextNodeRangeStart]
|
|
36
39
|
let textBetweenNodes = sourceCode.text.slice(
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
|
|
3
|
+
let validateCustomSortConfiguration = ({ alphabet, type }) => {
|
|
4
|
+
if (type !== 'custom') {
|
|
5
|
+
return
|
|
6
|
+
}
|
|
7
|
+
if (!alphabet.length) {
|
|
8
|
+
throw new Error('`alphabet` option must not be empty')
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.validateCustomSortConfiguration = validateCustomSortConfiguration
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
|
|
3
|
-
const validateGroupsConfiguration = require('
|
|
3
|
+
const validateGroupsConfiguration = require('./validate-groups-configuration.js')
|
|
4
4
|
let validateGeneratedGroupsConfiguration = ({
|
|
5
5
|
customGroups,
|
|
6
6
|
selectors,
|
|
@@ -8,7 +8,9 @@ let validateGeneratedGroupsConfiguration = ({
|
|
|
8
8
|
groups,
|
|
9
9
|
}) => {
|
|
10
10
|
let availableCustomGroupNames = new Set(
|
|
11
|
-
|
|
11
|
+
Array.isArray(customGroups)
|
|
12
|
+
? customGroups.map(customGroup => customGroup.groupName)
|
|
13
|
+
: Object.keys(customGroups),
|
|
12
14
|
)
|
|
13
15
|
let invalidGroups = groups
|
|
14
16
|
.flat()
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-perfectionist",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.0",
|
|
4
4
|
"description": "ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -14,8 +14,12 @@
|
|
|
14
14
|
"type": "commonjs",
|
|
15
15
|
"exports": {
|
|
16
16
|
".": {
|
|
17
|
-
"
|
|
18
|
-
"
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./alphabet": {
|
|
21
|
+
"types": "./dist/alphabet.d.ts",
|
|
22
|
+
"default": "./dist/utils/alphabet.js"
|
|
19
23
|
},
|
|
20
24
|
"./package.json": "./package.json"
|
|
21
25
|
},
|
|
@@ -23,8 +27,8 @@
|
|
|
23
27
|
"./dist"
|
|
24
28
|
],
|
|
25
29
|
"dependencies": {
|
|
26
|
-
"@typescript-eslint/types": "^8.
|
|
27
|
-
"@typescript-eslint/utils": "^8.
|
|
30
|
+
"@typescript-eslint/types": "^8.18.0",
|
|
31
|
+
"@typescript-eslint/utils": "^8.18.0",
|
|
28
32
|
"natural-orderby": "^5.0.0"
|
|
29
33
|
},
|
|
30
34
|
"peerDependencies": {
|
package/readme.md
CHANGED
|
@@ -165,6 +165,7 @@ module.exports = {
|
|
|
165
165
|
| [recommended-alphabetical](https://perfectionist.dev/configs/recommended-alphabetical) | All plugin rules with alphabetical sorting in ascending order |
|
|
166
166
|
| [recommended-natural](https://perfectionist.dev/configs/recommended-natural) | All plugin rules with natural sorting in ascending order |
|
|
167
167
|
| [recommended-line-length](https://perfectionist.dev/configs/recommended-line-length) | All plugin rules with sorting by line length in descending order |
|
|
168
|
+
| [recommended-custom](https://perfectionist.dev/configs/recommended-custom) | All plugin rules with sorting by your own custom order |
|
|
168
169
|
|
|
169
170
|
## Rules
|
|
170
171
|
|
|
@@ -201,15 +202,15 @@ module.exports = {
|
|
|
201
202
|
|
|
202
203
|
### Can I automatically fix problems in the editor?
|
|
203
204
|
|
|
204
|
-
Yes. To do this, you need to enable autofix in ESLint when you save the file in your editor.
|
|
205
|
+
Yes. To do this, you need to enable autofix in ESLint when you save the file in your editor. You may find instructions for your editor can be found [here](https://perfectionist.dev/guide/integrations).
|
|
205
206
|
|
|
206
207
|
### Is it safe?
|
|
207
208
|
|
|
208
|
-
|
|
209
|
+
Overall, yes. We want to make sure that the work of the plugin does not negatively affect the behavior of the code. For example, the plugin takes into account spread operators in JSX and objects, comments to the code. Safety is our priority. If you encounter any problem, you can create an [issue](https://github.com/azat-io/eslint-plugin-perfectionist/issues/new/choose).
|
|
209
210
|
|
|
210
211
|
### Why not Prettier?
|
|
211
212
|
|
|
212
|
-
I love Prettier. However, this is not
|
|
213
|
+
I love Prettier. However, this is not its area of responsibility. Prettier is used for formatting, and ESLint is also used for styling. For example, changing the order of imports can affect how the code works (console.log calls, fetch, style loading). Prettier should not change the AST. There is a cool article about this: ["The Blurry Line Between Formatting and Style"](https://blog.joshuakgoldberg.com/the-blurry-line-between-formatting-and-style) by **@joshuakgoldberg**.
|
|
213
214
|
|
|
214
215
|
## Versioning Policy
|
|
215
216
|
|