eslint-plugin-perfectionist 5.1.0 → 5.2.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 +15 -3
- package/dist/package.json.js +1 -1
- package/dist/rules/sort-array-includes.d.ts +3 -1
- package/dist/rules/sort-classes.d.ts +3 -1
- package/dist/rules/sort-decorators.d.ts +3 -1
- package/dist/rules/sort-enums.d.ts +3 -1
- package/dist/rules/sort-export-attributes.d.ts +3 -1
- package/dist/rules/sort-exports.d.ts +3 -1
- package/dist/rules/sort-heritage-clauses.d.ts +3 -1
- package/dist/rules/sort-import-attributes.d.ts +3 -1
- package/dist/rules/sort-imports/comparator-by-options-computer.js +48 -4
- package/dist/rules/sort-imports/compute-specifier-name.d.ts +12 -0
- package/dist/rules/sort-imports/compute-specifier-name.js +64 -0
- package/dist/rules/sort-imports/types.d.ts +12 -2
- package/dist/rules/sort-imports/types.js +5 -0
- package/dist/rules/sort-imports.d.ts +3 -1
- package/dist/rules/sort-imports.js +6 -0
- package/dist/rules/sort-interfaces.d.ts +3 -1
- package/dist/rules/sort-intersection-types.d.ts +3 -1
- package/dist/rules/sort-jsx-props.d.ts +3 -1
- package/dist/rules/sort-maps.d.ts +3 -1
- package/dist/rules/sort-modules/build-comparator-by-options-computer.d.ts +12 -0
- package/dist/rules/sort-modules/build-comparator-by-options-computer.js +66 -0
- package/dist/rules/sort-modules/compute-dependencies.d.ts +27 -0
- package/dist/rules/sort-modules/compute-dependencies.js +195 -0
- package/dist/rules/sort-modules/is-arrow-function-node.d.ts +12 -0
- package/dist/rules/sort-modules/is-arrow-function-node.js +10 -0
- package/dist/rules/sort-modules/is-property-or-accessor-node.d.ts +11 -0
- package/dist/rules/sort-modules/is-property-or-accessor-node.js +8 -0
- package/dist/rules/sort-modules/types.d.ts +13 -2
- package/dist/rules/sort-modules/types.js +7 -1
- package/dist/rules/sort-modules.d.ts +3 -1
- package/dist/rules/sort-modules.js +33 -141
- package/dist/rules/sort-named-exports.d.ts +3 -1
- package/dist/rules/sort-named-imports.d.ts +3 -1
- package/dist/rules/sort-object-types/types.d.ts +8 -29
- package/dist/rules/sort-object-types/types.js +0 -1
- package/dist/rules/sort-object-types.d.ts +3 -1
- package/dist/rules/sort-object-types.js +9 -11
- package/dist/rules/sort-objects.d.ts +3 -1
- package/dist/rules/sort-sets.d.ts +3 -1
- package/dist/rules/sort-switch-case.d.ts +3 -1
- package/dist/rules/sort-union-types.d.ts +3 -1
- package/dist/rules/sort-variable-declarations.d.ts +3 -1
- package/dist/types/common-groups-options.d.ts +52 -40
- package/dist/types/common-options.d.ts +11 -5
- package/dist/utils/compute-group.d.ts +6 -6
- package/dist/utils/compute-group.js +22 -19
- package/dist/utils/compute-nodes-in-circular-dependencies.d.ts +4 -1
- package/dist/utils/compute-overridden-options-by-group-index.js +38 -19
- package/dist/utils/create-eslint-rule.d.ts +3 -1
- package/dist/utils/is-group-with-overrides-option.d.ts +9 -2
- package/dist/utils/is-node-dependent-on-other-node.d.ts +8 -2
- package/dist/utils/json-schemas/common-groups-json-schemas.d.ts +12 -11
- package/dist/utils/json-schemas/common-groups-json-schemas.js +63 -52
- package/dist/utils/json-schemas/common-json-schemas.d.ts +4 -4
- package/dist/utils/json-schemas/common-json-schemas.js +3 -2
- package/dist/utils/sort-nodes-by-dependencies.d.ts +4 -1
- package/package.json +2 -2
- package/dist/rules/sort-object-types/build-options-by-group-index-computer.d.ts +0 -18
- package/dist/rules/sort-object-types/build-options-by-group-index-computer.js +0 -33
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
|
|
2
|
+
import { UnreachableCaseError } from '../../utils/unreachable-case-error.js'
|
|
3
|
+
import { isArrowFunctionNode } from './is-arrow-function-node.js'
|
|
4
|
+
function computeDependencies(node, dependencyDetection) {
|
|
5
|
+
let dependencies = []
|
|
6
|
+
checkNode(node)
|
|
7
|
+
return dependencies
|
|
8
|
+
function checkNode(nodeToCheck, options) {
|
|
9
|
+
switch (dependencyDetection.type) {
|
|
10
|
+
case 'hard':
|
|
11
|
+
if (
|
|
12
|
+
(nodeToCheck.type === AST_NODE_TYPES.MethodDefinition ||
|
|
13
|
+
isArrowFunctionNode(nodeToCheck)) &&
|
|
14
|
+
(!nodeToCheck.static ||
|
|
15
|
+
!dependencyDetection.searchStaticMethodsAndFunctionProperties)
|
|
16
|
+
) {
|
|
17
|
+
return
|
|
18
|
+
}
|
|
19
|
+
break
|
|
20
|
+
case 'soft':
|
|
21
|
+
checkNodesForSoftDependencies()
|
|
22
|
+
break
|
|
23
|
+
/* v8 ignore next 2 -- @preserve Exhaustive guard. */
|
|
24
|
+
default:
|
|
25
|
+
throw new UnreachableCaseError(dependencyDetection)
|
|
26
|
+
}
|
|
27
|
+
checkNodesForCommonDependencies()
|
|
28
|
+
function checkNodesForCommonDependencies() {
|
|
29
|
+
if (
|
|
30
|
+
!options?.ignoreNextIdentifier &&
|
|
31
|
+
nodeToCheck.type === AST_NODE_TYPES.Identifier
|
|
32
|
+
) {
|
|
33
|
+
dependencies.push(nodeToCheck.name)
|
|
34
|
+
}
|
|
35
|
+
if ('alternate' in nodeToCheck && nodeToCheck.alternate) {
|
|
36
|
+
checkNode(nodeToCheck.alternate)
|
|
37
|
+
}
|
|
38
|
+
if ('argument' in nodeToCheck && nodeToCheck.argument) {
|
|
39
|
+
checkNode(nodeToCheck.argument)
|
|
40
|
+
}
|
|
41
|
+
if ('arguments' in nodeToCheck) {
|
|
42
|
+
traverseNode(nodeToCheck.arguments)
|
|
43
|
+
}
|
|
44
|
+
if ('body' in nodeToCheck && nodeToCheck.body) {
|
|
45
|
+
traverseNode(nodeToCheck.body)
|
|
46
|
+
}
|
|
47
|
+
if ('callee' in nodeToCheck) {
|
|
48
|
+
checkNode(nodeToCheck.callee)
|
|
49
|
+
}
|
|
50
|
+
if ('consequent' in nodeToCheck) {
|
|
51
|
+
traverseNode(nodeToCheck.consequent)
|
|
52
|
+
}
|
|
53
|
+
if ('declarations' in nodeToCheck) {
|
|
54
|
+
traverseNode(nodeToCheck.declarations)
|
|
55
|
+
}
|
|
56
|
+
if ('decorators' in nodeToCheck) {
|
|
57
|
+
traverseNode(nodeToCheck.decorators)
|
|
58
|
+
}
|
|
59
|
+
if ('elements' in nodeToCheck) {
|
|
60
|
+
let elements = nodeToCheck.elements.filter(
|
|
61
|
+
currentNode => currentNode !== null,
|
|
62
|
+
)
|
|
63
|
+
traverseNode(elements)
|
|
64
|
+
}
|
|
65
|
+
if (
|
|
66
|
+
'expression' in nodeToCheck &&
|
|
67
|
+
typeof nodeToCheck.expression !== 'boolean'
|
|
68
|
+
) {
|
|
69
|
+
checkNode(nodeToCheck.expression)
|
|
70
|
+
}
|
|
71
|
+
if ('expressions' in nodeToCheck) {
|
|
72
|
+
traverseNode(nodeToCheck.expressions)
|
|
73
|
+
}
|
|
74
|
+
if ('init' in nodeToCheck && nodeToCheck.init) {
|
|
75
|
+
checkNode(nodeToCheck.init)
|
|
76
|
+
}
|
|
77
|
+
if ('initializer' in nodeToCheck && nodeToCheck.initializer) {
|
|
78
|
+
checkNode(nodeToCheck.initializer)
|
|
79
|
+
}
|
|
80
|
+
if ('left' in nodeToCheck) {
|
|
81
|
+
checkNode(nodeToCheck.left)
|
|
82
|
+
}
|
|
83
|
+
if ('members' in nodeToCheck) {
|
|
84
|
+
traverseNode(nodeToCheck.members)
|
|
85
|
+
}
|
|
86
|
+
if ('object' in nodeToCheck) {
|
|
87
|
+
checkNode(nodeToCheck.object)
|
|
88
|
+
}
|
|
89
|
+
if ('properties' in nodeToCheck) {
|
|
90
|
+
traverseNode(nodeToCheck.properties)
|
|
91
|
+
}
|
|
92
|
+
if ('right' in nodeToCheck) {
|
|
93
|
+
checkNode(nodeToCheck.right)
|
|
94
|
+
}
|
|
95
|
+
if ('superClass' in nodeToCheck && nodeToCheck.superClass) {
|
|
96
|
+
checkNode(nodeToCheck.superClass)
|
|
97
|
+
}
|
|
98
|
+
if ('test' in nodeToCheck && nodeToCheck.test) {
|
|
99
|
+
checkNode(nodeToCheck.test)
|
|
100
|
+
}
|
|
101
|
+
if (
|
|
102
|
+
'value' in nodeToCheck &&
|
|
103
|
+
nodeToCheck.value &&
|
|
104
|
+
typeof nodeToCheck.value === 'object' &&
|
|
105
|
+
'type' in nodeToCheck.value
|
|
106
|
+
) {
|
|
107
|
+
checkNode(nodeToCheck.value)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function checkNodesForSoftDependencies() {
|
|
111
|
+
if ('checkType' in nodeToCheck) {
|
|
112
|
+
checkNode(nodeToCheck.checkType)
|
|
113
|
+
}
|
|
114
|
+
if ('constraint' in nodeToCheck && nodeToCheck.constraint) {
|
|
115
|
+
checkNode(nodeToCheck.constraint)
|
|
116
|
+
}
|
|
117
|
+
if ('declaration' in nodeToCheck && nodeToCheck.declaration) {
|
|
118
|
+
checkNode(nodeToCheck.declaration)
|
|
119
|
+
}
|
|
120
|
+
if ('elementTypes' in nodeToCheck) {
|
|
121
|
+
traverseNode(nodeToCheck.elementTypes)
|
|
122
|
+
}
|
|
123
|
+
if ('elementType' in nodeToCheck) {
|
|
124
|
+
checkNode(nodeToCheck.elementType)
|
|
125
|
+
}
|
|
126
|
+
if ('extends' in nodeToCheck) {
|
|
127
|
+
traverseNode(nodeToCheck.extends)
|
|
128
|
+
}
|
|
129
|
+
if ('extendsType' in nodeToCheck) {
|
|
130
|
+
checkNode(nodeToCheck.extendsType)
|
|
131
|
+
}
|
|
132
|
+
if ('exprName' in nodeToCheck) {
|
|
133
|
+
checkNode(nodeToCheck.exprName)
|
|
134
|
+
}
|
|
135
|
+
if ('falseType' in nodeToCheck) {
|
|
136
|
+
checkNode(nodeToCheck.falseType)
|
|
137
|
+
}
|
|
138
|
+
if ('key' in nodeToCheck) {
|
|
139
|
+
checkNode(nodeToCheck.key, { ignoreNextIdentifier: true })
|
|
140
|
+
}
|
|
141
|
+
if ('id' in nodeToCheck && nodeToCheck.id) {
|
|
142
|
+
checkNode(nodeToCheck.id)
|
|
143
|
+
}
|
|
144
|
+
if ('implements' in nodeToCheck) {
|
|
145
|
+
traverseNode(nodeToCheck.implements)
|
|
146
|
+
}
|
|
147
|
+
if ('indexType' in nodeToCheck) {
|
|
148
|
+
checkNode(nodeToCheck.indexType)
|
|
149
|
+
}
|
|
150
|
+
if ('name' in nodeToCheck && typeof nodeToCheck.name !== 'string') {
|
|
151
|
+
checkNode(nodeToCheck.name)
|
|
152
|
+
}
|
|
153
|
+
if ('objectType' in nodeToCheck) {
|
|
154
|
+
checkNode(nodeToCheck.objectType)
|
|
155
|
+
}
|
|
156
|
+
if ('params' in nodeToCheck) {
|
|
157
|
+
traverseNode(nodeToCheck.params, { ignoreNextIdentifier: true })
|
|
158
|
+
}
|
|
159
|
+
if ('returnType' in nodeToCheck && nodeToCheck.returnType) {
|
|
160
|
+
checkNode(nodeToCheck.returnType)
|
|
161
|
+
}
|
|
162
|
+
if ('trueType' in nodeToCheck) {
|
|
163
|
+
checkNode(nodeToCheck.trueType)
|
|
164
|
+
}
|
|
165
|
+
if ('typeAnnotation' in nodeToCheck && nodeToCheck.typeAnnotation) {
|
|
166
|
+
checkNode(nodeToCheck.typeAnnotation)
|
|
167
|
+
}
|
|
168
|
+
if ('typeArguments' in nodeToCheck && nodeToCheck.typeArguments) {
|
|
169
|
+
checkNode(nodeToCheck.typeArguments)
|
|
170
|
+
}
|
|
171
|
+
if ('typeName' in nodeToCheck) {
|
|
172
|
+
checkNode(nodeToCheck.typeName)
|
|
173
|
+
}
|
|
174
|
+
if ('typeParameters' in nodeToCheck && nodeToCheck.typeParameters) {
|
|
175
|
+
traverseNode(nodeToCheck.typeParameters)
|
|
176
|
+
}
|
|
177
|
+
if ('typeParameter' in nodeToCheck) {
|
|
178
|
+
checkNode(nodeToCheck.typeParameter)
|
|
179
|
+
}
|
|
180
|
+
if ('types' in nodeToCheck) {
|
|
181
|
+
traverseNode(nodeToCheck.types)
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
function traverseNode(nodeToTraverse, options) {
|
|
186
|
+
if (Array.isArray(nodeToTraverse)) {
|
|
187
|
+
for (let nodeItem of nodeToTraverse) {
|
|
188
|
+
traverseNode(nodeItem, options)
|
|
189
|
+
}
|
|
190
|
+
} else {
|
|
191
|
+
checkNode(nodeToTraverse, options)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
export { computeDependencies }
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/types'
|
|
2
|
+
/**
|
|
3
|
+
* Checks whether a node is a property or accessor node with an
|
|
4
|
+
* ArrowFunctionExpression value.
|
|
5
|
+
*
|
|
6
|
+
* @param node - The AST node to check.
|
|
7
|
+
* @returns True if the node is a property or accessor node with an
|
|
8
|
+
* ArrowFunctionExpression value, false otherwise.
|
|
9
|
+
*/
|
|
10
|
+
export declare function isArrowFunctionNode(
|
|
11
|
+
node: TSESTree.Node,
|
|
12
|
+
): node is TSESTree.PropertyDefinition | TSESTree.AccessorProperty
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AST_NODE_TYPES } from '@typescript-eslint/types'
|
|
2
|
+
import { isPropertyOrAccessorNode } from './is-property-or-accessor-node.js'
|
|
3
|
+
function isArrowFunctionNode(node) {
|
|
4
|
+
return (
|
|
5
|
+
isPropertyOrAccessorNode(node) &&
|
|
6
|
+
node.value !== null &&
|
|
7
|
+
node.value.type === AST_NODE_TYPES.ArrowFunctionExpression
|
|
8
|
+
)
|
|
9
|
+
}
|
|
10
|
+
export { isArrowFunctionNode }
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/types'
|
|
2
|
+
/**
|
|
3
|
+
* Checks whether a node is a PropertyDefinition or AccessorProperty.
|
|
4
|
+
*
|
|
5
|
+
* @param node - The AST node to check.
|
|
6
|
+
* @returns True if the node is a PropertyDefinition or AccessorProperty, false
|
|
7
|
+
* otherwise.
|
|
8
|
+
*/
|
|
9
|
+
export declare function isPropertyOrAccessorNode(
|
|
10
|
+
node: TSESTree.Node,
|
|
11
|
+
): node is TSESTree.PropertyDefinition | TSESTree.AccessorProperty
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { JSONSchema4 } from '@typescript-eslint/utils/json-schema'
|
|
2
|
+
import { TSESTree } from '@typescript-eslint/types'
|
|
2
3
|
import {
|
|
3
4
|
CommonOptions,
|
|
4
5
|
RegexOption,
|
|
5
6
|
TypeOption,
|
|
6
7
|
} from '../../types/common-options.js'
|
|
8
|
+
import { SortingNodeWithDependencies } from '../../utils/sort-nodes-by-dependencies.js'
|
|
7
9
|
import { CommonPartitionOptions } from '../../types/common-partition-options.js'
|
|
8
10
|
import { CommonGroupsOptions } from '../../types/common-groups-options.js'
|
|
9
11
|
/**
|
|
@@ -14,11 +16,18 @@ import { CommonGroupsOptions } from '../../types/common-groups-options.js'
|
|
|
14
16
|
*/
|
|
15
17
|
export type SortModulesOptions = [
|
|
16
18
|
Partial<
|
|
17
|
-
CommonGroupsOptions<
|
|
18
|
-
|
|
19
|
+
CommonGroupsOptions<
|
|
20
|
+
SingleCustomGroup,
|
|
21
|
+
Record<string, never>,
|
|
22
|
+
CustomTypeOption
|
|
23
|
+
> &
|
|
24
|
+
CommonOptions<CustomTypeOption> &
|
|
19
25
|
CommonPartitionOptions
|
|
20
26
|
>,
|
|
21
27
|
]
|
|
28
|
+
/** Represents a sorting node for a module statement. */
|
|
29
|
+
export type SortModulesSortingNode =
|
|
30
|
+
SortingNodeWithDependencies<TSESTree.ProgramStatement>
|
|
22
31
|
/**
|
|
23
32
|
* Union type of all available module member selectors. Used to categorize
|
|
24
33
|
* different types of module-level declarations.
|
|
@@ -46,6 +55,7 @@ interface SingleCustomGroup {
|
|
|
46
55
|
/** The type of module member this group applies to. */
|
|
47
56
|
selector?: Selector
|
|
48
57
|
}
|
|
58
|
+
type CustomTypeOption = typeof USAGE_TYPE_OPTION | TypeOption
|
|
49
59
|
/**
|
|
50
60
|
* Complete list of available module member selectors. Used for validation and
|
|
51
61
|
* JSON schema generation.
|
|
@@ -73,4 +83,5 @@ export declare let allModifiers: readonly [
|
|
|
73
83
|
* ensure that users do not enter invalid modifiers for a given selector.
|
|
74
84
|
*/
|
|
75
85
|
export declare let singleCustomGroupJsonSchema: Record<string, JSONSchema4>
|
|
86
|
+
export declare const USAGE_TYPE_OPTION = 'usage'
|
|
76
87
|
export {}
|
|
@@ -18,4 +18,10 @@ let singleCustomGroupJsonSchema = {
|
|
|
18
18
|
selector: buildCustomGroupSelectorJsonSchema(allSelectors),
|
|
19
19
|
decoratorNamePattern: buildRegexJsonSchema(),
|
|
20
20
|
}
|
|
21
|
-
|
|
21
|
+
const USAGE_TYPE_OPTION = 'usage'
|
|
22
|
+
export {
|
|
23
|
+
USAGE_TYPE_OPTION,
|
|
24
|
+
allModifiers,
|
|
25
|
+
allSelectors,
|
|
26
|
+
singleCustomGroupJsonSchema,
|
|
27
|
+
}
|
|
@@ -10,21 +10,25 @@ import {
|
|
|
10
10
|
partitionByNewLineJsonSchema,
|
|
11
11
|
partitionByCommentJsonSchema,
|
|
12
12
|
} from '../utils/json-schemas/common-partition-json-schemas.js'
|
|
13
|
-
import { validateNewlinesAndPartitionConfiguration } from '../utils/validate-newlines-and-partition-configuration.js'
|
|
14
|
-
import { buildDefaultOptionsByGroupIndexComputer } from '../utils/build-default-options-by-group-index-computer.js'
|
|
15
|
-
import { defaultComparatorByOptionsComputer } from '../utils/compare/default-comparator-by-options-computer.js'
|
|
16
13
|
import {
|
|
17
14
|
allSelectors,
|
|
18
15
|
allModifiers,
|
|
19
16
|
singleCustomGroupJsonSchema,
|
|
17
|
+
USAGE_TYPE_OPTION,
|
|
20
18
|
} from './sort-modules/types.js'
|
|
19
|
+
import { validateNewlinesAndPartitionConfiguration } from '../utils/validate-newlines-and-partition-configuration.js'
|
|
20
|
+
import { buildDefaultOptionsByGroupIndexComputer } from '../utils/build-default-options-by-group-index-computer.js'
|
|
21
|
+
import { buildComparatorByOptionsComputer } from './sort-modules/build-comparator-by-options-computer.js'
|
|
21
22
|
import { buildCommonGroupsJsonSchemas } from '../utils/json-schemas/common-groups-json-schemas.js'
|
|
22
23
|
import { validateCustomSortConfiguration } from '../utils/validate-custom-sort-configuration.js'
|
|
24
|
+
import { isPropertyOrAccessorNode } from './sort-modules/is-property-or-accessor-node.js'
|
|
23
25
|
import { validateGroupsConfiguration } from '../utils/validate-groups-configuration.js'
|
|
24
26
|
import { buildCommonJsonSchemas } from '../utils/json-schemas/common-json-schemas.js'
|
|
25
27
|
import { generatePredefinedGroups } from '../utils/generate-predefined-groups.js'
|
|
26
28
|
import { sortNodesByDependencies } from '../utils/sort-nodes-by-dependencies.js'
|
|
27
29
|
import { getEslintDisabledLines } from '../utils/get-eslint-disabled-lines.js'
|
|
30
|
+
import { isArrowFunctionNode } from './sort-modules/is-arrow-function-node.js'
|
|
31
|
+
import { computeDependencies } from './sort-modules/compute-dependencies.js'
|
|
28
32
|
import { isNodeEslintDisabled } from '../utils/is-node-eslint-disabled.js'
|
|
29
33
|
import { doesCustomGroupMatch } from '../utils/does-custom-group-match.js'
|
|
30
34
|
import { sortNodesByGroups } from '../utils/sort-nodes-by-groups.js'
|
|
@@ -33,7 +37,6 @@ import { createEslintRule } from '../utils/create-eslint-rule.js'
|
|
|
33
37
|
import { getDecoratorName } from '../utils/get-decorator-name.js'
|
|
34
38
|
import { reportAllErrors } from '../utils/report-all-errors.js'
|
|
35
39
|
import { shouldPartition } from '../utils/should-partition.js'
|
|
36
|
-
import { getEnumMembers } from '../utils/get-enum-members.js'
|
|
37
40
|
import { getGroupIndex } from '../utils/get-group-index.js'
|
|
38
41
|
import { computeGroup } from '../utils/compute-group.js'
|
|
39
42
|
import { rangeToDiff } from '../utils/range-to-diff.js'
|
|
@@ -79,8 +82,11 @@ const sortModules = createEslintRule({
|
|
|
79
82
|
schema: [
|
|
80
83
|
{
|
|
81
84
|
properties: {
|
|
82
|
-
...buildCommonJsonSchemas(
|
|
85
|
+
...buildCommonJsonSchemas({
|
|
86
|
+
allowedAdditionalTypeValues: [USAGE_TYPE_OPTION],
|
|
87
|
+
}),
|
|
83
88
|
...buildCommonGroupsJsonSchemas({
|
|
89
|
+
allowedAdditionalTypeValues: [USAGE_TYPE_OPTION],
|
|
84
90
|
singleCustomGroupJsonSchema,
|
|
85
91
|
}),
|
|
86
92
|
partitionByComment: partitionByCommentJsonSchema,
|
|
@@ -125,9 +131,9 @@ const sortModules = createEslintRule({
|
|
|
125
131
|
if (isSortable(program.body)) {
|
|
126
132
|
return analyzeModule({
|
|
127
133
|
eslintDisabledLines,
|
|
134
|
+
module: program,
|
|
128
135
|
sourceCode,
|
|
129
136
|
options,
|
|
130
|
-
program,
|
|
131
137
|
context,
|
|
132
138
|
})
|
|
133
139
|
}
|
|
@@ -141,11 +147,13 @@ function analyzeModule({
|
|
|
141
147
|
eslintDisabledLines,
|
|
142
148
|
sourceCode,
|
|
143
149
|
options,
|
|
144
|
-
program,
|
|
145
150
|
context,
|
|
151
|
+
module,
|
|
146
152
|
}) {
|
|
153
|
+
let optionsByGroupIndexComputer =
|
|
154
|
+
buildDefaultOptionsByGroupIndexComputer(options)
|
|
147
155
|
let formattedNodes = [[]]
|
|
148
|
-
for (let node of
|
|
156
|
+
for (let node of module.body) {
|
|
149
157
|
let parseNode = function (nodeToParse) {
|
|
150
158
|
if ('declare' in nodeToParse && nodeToParse.declare) {
|
|
151
159
|
modifiers.push('declare')
|
|
@@ -185,7 +193,7 @@ function analyzeModule({
|
|
|
185
193
|
formattedNodes.push([])
|
|
186
194
|
if (nodeToParse.body) {
|
|
187
195
|
analyzeModule({
|
|
188
|
-
|
|
196
|
+
module: nodeToParse.body,
|
|
189
197
|
eslintDisabledLines,
|
|
190
198
|
sourceCode,
|
|
191
199
|
options,
|
|
@@ -200,10 +208,7 @@ function analyzeModule({
|
|
|
200
208
|
case AST_NODE_TYPES.TSEnumDeclaration:
|
|
201
209
|
selector = 'enum'
|
|
202
210
|
;({ name } = nodeToParse.id)
|
|
203
|
-
dependencies = [
|
|
204
|
-
...dependencies,
|
|
205
|
-
...getEnumMembers(nodeToParse).flatMap(extractDependencies),
|
|
206
|
-
]
|
|
211
|
+
dependencies = [...dependencies, ...extractDependencies(nodeToParse)]
|
|
207
212
|
break
|
|
208
213
|
case AST_NODE_TYPES.ClassDeclaration:
|
|
209
214
|
selector = 'class'
|
|
@@ -218,13 +223,7 @@ function analyzeModule({
|
|
|
218
223
|
decorator,
|
|
219
224
|
}),
|
|
220
225
|
)
|
|
221
|
-
dependencies = [
|
|
222
|
-
...dependencies,
|
|
223
|
-
...(nodeToParse.superClass && 'name' in nodeToParse.superClass
|
|
224
|
-
? [nodeToParse.superClass.name]
|
|
225
|
-
: []),
|
|
226
|
-
...extractDependencies(nodeToParse.body),
|
|
227
|
-
]
|
|
226
|
+
dependencies = [...dependencies, ...extractDependencies(nodeToParse)]
|
|
228
227
|
break
|
|
229
228
|
}
|
|
230
229
|
}
|
|
@@ -291,11 +290,13 @@ function analyzeModule({
|
|
|
291
290
|
function sortNodesExcludingEslintDisabled(ignoreEslintDisabledNodes) {
|
|
292
291
|
let nodesSortedByGroups = formattedNodes.flatMap(nodes2 =>
|
|
293
292
|
sortNodesByGroups({
|
|
293
|
+
comparatorByOptionsComputer: buildComparatorByOptionsComputer({
|
|
294
|
+
ignoreEslintDisabledNodes,
|
|
295
|
+
sortingNodes: nodes2,
|
|
296
|
+
}),
|
|
294
297
|
isNodeIgnored: sortingNode =>
|
|
295
298
|
getGroupIndex(options.groups, sortingNode) === options.groups.length,
|
|
296
|
-
optionsByGroupIndexComputer
|
|
297
|
-
buildDefaultOptionsByGroupIndexComputer(options),
|
|
298
|
-
comparatorByOptionsComputer: defaultComparatorByOptionsComputer,
|
|
299
|
+
optionsByGroupIndexComputer,
|
|
299
300
|
ignoreEslintDisabledNodes,
|
|
300
301
|
groups: options.groups,
|
|
301
302
|
nodes: nodes2,
|
|
@@ -321,127 +322,18 @@ function analyzeModule({
|
|
|
321
322
|
})
|
|
322
323
|
}
|
|
323
324
|
function extractDependencies(expression) {
|
|
324
|
-
let dependencies = []
|
|
325
325
|
let searchStaticMethodsAndFunctionProperties =
|
|
326
|
-
expression.type ===
|
|
327
|
-
expression.body.some(
|
|
326
|
+
expression.type === AST_NODE_TYPES.ClassDeclaration &&
|
|
327
|
+
expression.body.body.some(
|
|
328
328
|
classElement =>
|
|
329
|
-
classElement.type ===
|
|
329
|
+
classElement.type === AST_NODE_TYPES.StaticBlock ||
|
|
330
330
|
(classElement.static &&
|
|
331
|
-
|
|
332
|
-
!
|
|
331
|
+
isPropertyOrAccessorNode(classElement) &&
|
|
332
|
+
!isArrowFunctionNode(classElement)),
|
|
333
333
|
)
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
) {
|
|
339
|
-
return
|
|
340
|
-
}
|
|
341
|
-
if ('decorators' in nodeValue) {
|
|
342
|
-
traverseNode(nodeValue.decorators)
|
|
343
|
-
}
|
|
344
|
-
if (
|
|
345
|
-
nodeValue.type === 'NewExpression' &&
|
|
346
|
-
nodeValue.callee.type === 'Identifier'
|
|
347
|
-
) {
|
|
348
|
-
dependencies.push(nodeValue.callee.name)
|
|
349
|
-
}
|
|
350
|
-
if (nodeValue.type === 'Identifier') {
|
|
351
|
-
dependencies.push(nodeValue.name)
|
|
352
|
-
}
|
|
353
|
-
if (nodeValue.type === 'ConditionalExpression') {
|
|
354
|
-
checkNode(nodeValue.test)
|
|
355
|
-
checkNode(nodeValue.consequent)
|
|
356
|
-
checkNode(nodeValue.alternate)
|
|
357
|
-
}
|
|
358
|
-
if (
|
|
359
|
-
'expression' in nodeValue &&
|
|
360
|
-
typeof nodeValue.expression !== 'boolean'
|
|
361
|
-
) {
|
|
362
|
-
checkNode(nodeValue.expression)
|
|
363
|
-
}
|
|
364
|
-
if ('object' in nodeValue) {
|
|
365
|
-
checkNode(nodeValue.object)
|
|
366
|
-
}
|
|
367
|
-
if ('callee' in nodeValue) {
|
|
368
|
-
checkNode(nodeValue.callee)
|
|
369
|
-
}
|
|
370
|
-
if ('init' in nodeValue && nodeValue.init) {
|
|
371
|
-
checkNode(nodeValue.init)
|
|
372
|
-
}
|
|
373
|
-
if ('body' in nodeValue && nodeValue.body) {
|
|
374
|
-
traverseNode(nodeValue.body)
|
|
375
|
-
}
|
|
376
|
-
if ('left' in nodeValue) {
|
|
377
|
-
checkNode(nodeValue.left)
|
|
378
|
-
}
|
|
379
|
-
if ('right' in nodeValue) {
|
|
380
|
-
checkNode(nodeValue.right)
|
|
381
|
-
}
|
|
382
|
-
if ('initializer' in nodeValue && nodeValue.initializer) {
|
|
383
|
-
checkNode(nodeValue.initializer)
|
|
384
|
-
}
|
|
385
|
-
if ('elements' in nodeValue) {
|
|
386
|
-
let elements = nodeValue.elements.filter(
|
|
387
|
-
currentNode => currentNode !== null,
|
|
388
|
-
)
|
|
389
|
-
for (let element of elements) {
|
|
390
|
-
traverseNode(element)
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
if ('argument' in nodeValue && nodeValue.argument) {
|
|
394
|
-
checkNode(nodeValue.argument)
|
|
395
|
-
}
|
|
396
|
-
if ('arguments' in nodeValue) {
|
|
397
|
-
for (let argument of nodeValue.arguments) {
|
|
398
|
-
checkNode(argument)
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
if ('declarations' in nodeValue) {
|
|
402
|
-
for (let declaration of nodeValue.declarations) {
|
|
403
|
-
checkNode(declaration)
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
if ('properties' in nodeValue) {
|
|
407
|
-
for (let property of nodeValue.properties) {
|
|
408
|
-
checkNode(property)
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
if (
|
|
412
|
-
'value' in nodeValue &&
|
|
413
|
-
nodeValue.value &&
|
|
414
|
-
typeof nodeValue.value === 'object' &&
|
|
415
|
-
'type' in nodeValue.value
|
|
416
|
-
) {
|
|
417
|
-
checkNode(nodeValue.value)
|
|
418
|
-
}
|
|
419
|
-
if ('expressions' in nodeValue) {
|
|
420
|
-
for (let nodeExpression of nodeValue.expressions) {
|
|
421
|
-
checkNode(nodeExpression)
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
function traverseNode(nodeValue) {
|
|
426
|
-
if (Array.isArray(nodeValue)) {
|
|
427
|
-
for (let nodeItem of nodeValue) {
|
|
428
|
-
traverseNode(nodeItem)
|
|
429
|
-
}
|
|
430
|
-
} else {
|
|
431
|
-
checkNode(nodeValue)
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
checkNode(expression)
|
|
435
|
-
return dependencies
|
|
436
|
-
}
|
|
437
|
-
function isArrowFunction(node) {
|
|
438
|
-
return (
|
|
439
|
-
isPropertyOrAccessor(node) &&
|
|
440
|
-
node.value !== null &&
|
|
441
|
-
node.value.type === 'ArrowFunctionExpression'
|
|
442
|
-
)
|
|
443
|
-
}
|
|
444
|
-
function isPropertyOrAccessor(node) {
|
|
445
|
-
return node.type === 'PropertyDefinition' || node.type === 'AccessorProperty'
|
|
334
|
+
return computeDependencies(expression, {
|
|
335
|
+
searchStaticMethodsAndFunctionProperties,
|
|
336
|
+
type: 'hard',
|
|
337
|
+
})
|
|
446
338
|
}
|
|
447
339
|
export { sortModules as default }
|
|
@@ -13,5 +13,7 @@ declare const _default: import('@typescript-eslint/utils/ts-eslint').RuleModule<
|
|
|
13
13
|
Options,
|
|
14
14
|
import('../utils/create-eslint-rule.js').ESLintPluginDocumentation,
|
|
15
15
|
import('@typescript-eslint/utils/ts-eslint').RuleListener
|
|
16
|
-
>
|
|
16
|
+
> & {
|
|
17
|
+
name: string
|
|
18
|
+
}
|
|
17
19
|
export default _default
|
|
@@ -13,5 +13,7 @@ declare const _default: import('@typescript-eslint/utils/ts-eslint').RuleModule<
|
|
|
13
13
|
Options,
|
|
14
14
|
import('../utils/create-eslint-rule.js').ESLintPluginDocumentation,
|
|
15
15
|
import('@typescript-eslint/utils/ts-eslint').RuleListener
|
|
16
|
-
>
|
|
16
|
+
> & {
|
|
17
|
+
name: string
|
|
18
|
+
}
|
|
17
19
|
export default _default
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { JSONSchema4 } from '@typescript-eslint/utils/json-schema'
|
|
2
2
|
import { TSESTree } from '@typescript-eslint/types'
|
|
3
3
|
import {
|
|
4
|
-
FallbackSortOption,
|
|
5
4
|
CommonOptions,
|
|
6
5
|
RegexOption,
|
|
7
6
|
TypeOption,
|
|
@@ -46,33 +45,19 @@ export type Options = Partial<
|
|
|
46
45
|
*/
|
|
47
46
|
hasNumericKeysOnly?: boolean
|
|
48
47
|
}
|
|
49
|
-
/**
|
|
50
|
-
* Fallback sorting configuration for elements that don't match any group.
|
|
51
|
-
* Includes an additional option to sort by member value or name.
|
|
52
|
-
*/
|
|
53
|
-
fallbackSort: FallbackSortOption<TypeOption> & {
|
|
54
|
-
sortBy?: 'value' | 'name'
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Determines what to sort by when comparing object type members.
|
|
58
|
-
*
|
|
59
|
-
* - 'name': Sort by the member's property/method name
|
|
60
|
-
* - 'value': Sort by the member's type annotation value.
|
|
61
|
-
*
|
|
62
|
-
* @default 'name'
|
|
63
|
-
*/
|
|
64
|
-
sortBy: 'value' | 'name'
|
|
65
48
|
} & CommonGroupsOptions<
|
|
66
49
|
SingleCustomGroup,
|
|
67
50
|
{
|
|
68
|
-
|
|
69
|
-
fallbackSort?: {
|
|
70
|
-
sortBy?: 'value' | 'name'
|
|
71
|
-
} & FallbackSortOption<TypeOption>
|
|
51
|
+
sortBy?: SortByOption
|
|
72
52
|
},
|
|
73
53
|
TypeOption
|
|
74
54
|
> &
|
|
75
|
-
|
|
55
|
+
CommonOptions<
|
|
56
|
+
TypeOption,
|
|
57
|
+
{
|
|
58
|
+
sortBy: SortByOption
|
|
59
|
+
}
|
|
60
|
+
> &
|
|
76
61
|
CommonPartitionOptions
|
|
77
62
|
>[]
|
|
78
63
|
/**
|
|
@@ -89,6 +74,7 @@ export interface SortObjectTypesSortingNode extends SortingNode<TSESTree.TypeEle
|
|
|
89
74
|
*/
|
|
90
75
|
value: string
|
|
91
76
|
}
|
|
77
|
+
type SortByOption = 'value' | 'name'
|
|
92
78
|
export declare let objectTypeParentTypes: readonly [
|
|
93
79
|
TSESTree.AST_NODE_TYPES.TSTypeAliasDeclaration,
|
|
94
80
|
TSESTree.AST_NODE_TYPES.TSInterfaceDeclaration,
|
|
@@ -119,13 +105,6 @@ interface SingleCustomGroup {
|
|
|
119
105
|
* value. Only applicable to properties.
|
|
120
106
|
*/
|
|
121
107
|
elementValuePattern?: RegexOption
|
|
122
|
-
/**
|
|
123
|
-
* Override sorting method for this specific group.
|
|
124
|
-
*
|
|
125
|
-
* - 'name': Sort by member name
|
|
126
|
-
* - 'value': Sort by type annotation value.
|
|
127
|
-
*/
|
|
128
|
-
sortBy?: 'value' | 'name'
|
|
129
108
|
/**
|
|
130
109
|
* Array of modifiers that members must have to match this group. Only
|
|
131
110
|
* modifiers allowed for the specified selector type are valid.
|
|
@@ -21,7 +21,6 @@ let singleCustomGroupJsonSchema = {
|
|
|
21
21
|
modifiers: buildCustomGroupModifiersJsonSchema(allModifiers),
|
|
22
22
|
selector: buildCustomGroupSelectorJsonSchema(allSelectors),
|
|
23
23
|
elementValuePattern: buildRegexJsonSchema(),
|
|
24
|
-
sortBy: sortByJsonSchema,
|
|
25
24
|
}
|
|
26
25
|
export {
|
|
27
26
|
allModifiers,
|
|
@@ -18,7 +18,9 @@ declare const _default: import('@typescript-eslint/utils/ts-eslint').RuleModule<
|
|
|
18
18
|
Options,
|
|
19
19
|
import('../utils/create-eslint-rule.js').ESLintPluginDocumentation,
|
|
20
20
|
import('@typescript-eslint/utils/ts-eslint').RuleListener
|
|
21
|
-
>
|
|
21
|
+
> & {
|
|
22
|
+
name: string
|
|
23
|
+
}
|
|
22
24
|
export default _default
|
|
23
25
|
export declare function sortObjectTypeElements<MessageIds extends string>({
|
|
24
26
|
availableMessageIds,
|