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
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
|
-
import { ESLint } from 'eslint'
|
|
1
|
+
import { ESLint, Linter } from 'eslint'
|
|
2
|
+
interface PluginConfigs {
|
|
3
|
+
'recommended-alphabetical-legacy': Linter.LegacyConfig
|
|
4
|
+
'recommended-line-length-legacy': Linter.LegacyConfig
|
|
5
|
+
'recommended-natural-legacy': Linter.LegacyConfig
|
|
6
|
+
'recommended-custom-legacy': Linter.LegacyConfig
|
|
7
|
+
'recommended-alphabetical': Linter.Config
|
|
8
|
+
'recommended-line-length': Linter.Config
|
|
9
|
+
'recommended-natural': Linter.Config
|
|
10
|
+
'recommended-custom': Linter.Config
|
|
11
|
+
}
|
|
2
12
|
export declare let rules: ESLint.Plugin['rules']
|
|
3
|
-
export declare let configs:
|
|
4
|
-
declare const _default:
|
|
13
|
+
export declare let configs: PluginConfigs
|
|
14
|
+
declare const _default: {
|
|
15
|
+
configs: PluginConfigs
|
|
16
|
+
} & ESLint.Plugin
|
|
5
17
|
export default _default
|
package/dist/package.json.js
CHANGED
|
@@ -20,7 +20,9 @@ declare const _default: TSESLint.RuleModule<
|
|
|
20
20
|
Options,
|
|
21
21
|
import('../utils/create-eslint-rule.js').ESLintPluginDocumentation,
|
|
22
22
|
TSESLint.RuleListener
|
|
23
|
-
>
|
|
23
|
+
> & {
|
|
24
|
+
name: string
|
|
25
|
+
}
|
|
24
26
|
export default _default
|
|
25
27
|
export declare function sortArray<MessageIds extends string>({
|
|
26
28
|
availableMessageIds,
|
|
@@ -15,5 +15,7 @@ declare const _default: import('@typescript-eslint/utils/ts-eslint').RuleModule<
|
|
|
15
15
|
SortClassesOptions,
|
|
16
16
|
import('../utils/create-eslint-rule.js').ESLintPluginDocumentation,
|
|
17
17
|
import('@typescript-eslint/utils/ts-eslint').RuleListener
|
|
18
|
-
>
|
|
18
|
+
> & {
|
|
19
|
+
name: string
|
|
20
|
+
}
|
|
19
21
|
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
|
|
@@ -15,5 +15,7 @@ declare const _default: import('@typescript-eslint/utils/ts-eslint').RuleModule<
|
|
|
15
15
|
Options,
|
|
16
16
|
import('../utils/create-eslint-rule.js').ESLintPluginDocumentation,
|
|
17
17
|
import('@typescript-eslint/utils/ts-eslint').RuleListener
|
|
18
|
-
>
|
|
18
|
+
> & {
|
|
19
|
+
name: string
|
|
20
|
+
}
|
|
19
21
|
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,6 +1,11 @@
|
|
|
1
1
|
import { defaultComparatorByOptionsComputer } from '../../utils/compare/default-comparator-by-options-computer.js'
|
|
2
|
+
import { buildLineLengthComparator } from '../../utils/compare/build-line-length-comparator.js'
|
|
3
|
+
import { compareAlphabetically } from '../../utils/compare/compare-alphabetically.js'
|
|
4
|
+
import { compareByCustomSort } from '../../utils/compare/compare-by-custom-sort.js'
|
|
2
5
|
import { computeOrderedValue } from '../../utils/compare/compute-ordered-value.js'
|
|
6
|
+
import { unsortedComparator } from '../../utils/compare/unsorted-comparator.js'
|
|
3
7
|
import { UnreachableCaseError } from '../../utils/unreachable-case-error.js'
|
|
8
|
+
import { compareNaturally } from '../../utils/compare/compare-naturally.js'
|
|
4
9
|
let comparatorByOptionsComputer = options => {
|
|
5
10
|
switch (options.type) {
|
|
6
11
|
case 'type-import-first':
|
|
@@ -10,10 +15,21 @@ let comparatorByOptionsComputer = options => {
|
|
|
10
15
|
case 'unsorted':
|
|
11
16
|
case 'natural':
|
|
12
17
|
case 'custom':
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
18
|
+
switch (options.sortBy) {
|
|
19
|
+
case 'specifier':
|
|
20
|
+
return bySpecifierComparatorByOptionsComputer({
|
|
21
|
+
...options,
|
|
22
|
+
type: options.type,
|
|
23
|
+
})
|
|
24
|
+
case 'path':
|
|
25
|
+
return defaultComparatorByOptionsComputer({
|
|
26
|
+
...options,
|
|
27
|
+
type: options.type,
|
|
28
|
+
})
|
|
29
|
+
/* v8 ignore next 2 -- @preserve Exhaustive guard. */
|
|
30
|
+
default:
|
|
31
|
+
throw new UnreachableCaseError(options.sortBy)
|
|
32
|
+
}
|
|
17
33
|
/* v8 ignore next 2 -- @preserve Exhaustive guard. */
|
|
18
34
|
default:
|
|
19
35
|
throw new UnreachableCaseError(options.type)
|
|
@@ -28,4 +44,32 @@ function compareTypeImportFirst(a, b, options) {
|
|
|
28
44
|
}
|
|
29
45
|
return computeOrderedValue(a.isTypeImport ? -1 : 1, options.order)
|
|
30
46
|
}
|
|
47
|
+
let bySpecifierComparatorByOptionsComputer = options => {
|
|
48
|
+
switch (options.type) {
|
|
49
|
+
case 'alphabetical':
|
|
50
|
+
return (a, b) =>
|
|
51
|
+
compareAlphabetically(
|
|
52
|
+
a.specifierName ?? '',
|
|
53
|
+
b.specifierName ?? '',
|
|
54
|
+
options,
|
|
55
|
+
)
|
|
56
|
+
case 'line-length':
|
|
57
|
+
return buildLineLengthComparator(options)
|
|
58
|
+
case 'unsorted':
|
|
59
|
+
return unsortedComparator
|
|
60
|
+
case 'natural':
|
|
61
|
+
return (a, b) =>
|
|
62
|
+
compareNaturally(a.specifierName ?? '', b.specifierName ?? '', options)
|
|
63
|
+
case 'custom':
|
|
64
|
+
return (a, b) =>
|
|
65
|
+
compareByCustomSort(
|
|
66
|
+
a.specifierName ?? '',
|
|
67
|
+
b.specifierName ?? '',
|
|
68
|
+
options,
|
|
69
|
+
)
|
|
70
|
+
/* v8 ignore next 2 -- @preserve Exhaustive guard. */
|
|
71
|
+
default:
|
|
72
|
+
throw new UnreachableCaseError(options.type)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
31
75
|
export { comparatorByOptionsComputer }
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { TSESLint } from '@typescript-eslint/utils'
|
|
2
|
+
import { TSESTree } from '@typescript-eslint/types'
|
|
3
|
+
export declare function computeSpecifierName({
|
|
4
|
+
sourceCode,
|
|
5
|
+
node,
|
|
6
|
+
}: {
|
|
7
|
+
node:
|
|
8
|
+
| TSESTree.TSImportEqualsDeclaration
|
|
9
|
+
| TSESTree.VariableDeclaration
|
|
10
|
+
| TSESTree.ImportDeclaration
|
|
11
|
+
sourceCode: TSESLint.SourceCode
|
|
12
|
+
}): string | null
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
|
|
2
|
+
import { UnreachableCaseError } from '../../utils/unreachable-case-error.js'
|
|
3
|
+
function computeSpecifierName({ sourceCode, node }) {
|
|
4
|
+
switch (node.type) {
|
|
5
|
+
case AST_NODE_TYPES.TSImportEqualsDeclaration:
|
|
6
|
+
return node.id.name
|
|
7
|
+
case AST_NODE_TYPES.VariableDeclaration:
|
|
8
|
+
return computeVariableDeclarationSpecifierName(node, sourceCode)
|
|
9
|
+
case AST_NODE_TYPES.ImportDeclaration:
|
|
10
|
+
return computeImportDeclarationSpecifierName(node)
|
|
11
|
+
/* v8 ignore next 2 -- @preserve Exhaustive guard. */
|
|
12
|
+
default:
|
|
13
|
+
throw new UnreachableCaseError(node)
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function computeVariableDeclarationSpecifierName(node, sourceCode) {
|
|
17
|
+
let [declaration] = node.declarations
|
|
18
|
+
switch (declaration.id.type) {
|
|
19
|
+
case AST_NODE_TYPES.ObjectPattern:
|
|
20
|
+
return computeObjectPatternPropertyName(declaration.id.properties[0])
|
|
21
|
+
case AST_NODE_TYPES.ArrayPattern:
|
|
22
|
+
if (!declaration.id.elements[0]) {
|
|
23
|
+
return null
|
|
24
|
+
}
|
|
25
|
+
return sourceCode.getText(declaration.id.elements[0])
|
|
26
|
+
case AST_NODE_TYPES.Identifier:
|
|
27
|
+
return declaration.id.name
|
|
28
|
+
/* v8 ignore next 2 -- @preserve Exhaustive guard. */
|
|
29
|
+
default:
|
|
30
|
+
throw new UnreachableCaseError(declaration.id)
|
|
31
|
+
}
|
|
32
|
+
function computeObjectPatternPropertyName(property) {
|
|
33
|
+
if (!property) {
|
|
34
|
+
return null
|
|
35
|
+
}
|
|
36
|
+
switch (property.type) {
|
|
37
|
+
case AST_NODE_TYPES.RestElement:
|
|
38
|
+
return sourceCode.getText(property.argument)
|
|
39
|
+
case AST_NODE_TYPES.Property:
|
|
40
|
+
return sourceCode.getText(property.value)
|
|
41
|
+
/* v8 ignore next 2 -- @preserve Exhaustive guard. */
|
|
42
|
+
default:
|
|
43
|
+
throw new UnreachableCaseError(property)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function computeImportDeclarationSpecifierName(node) {
|
|
48
|
+
let [specifier] = node.specifiers
|
|
49
|
+
if (!specifier) {
|
|
50
|
+
return null
|
|
51
|
+
}
|
|
52
|
+
switch (specifier.type) {
|
|
53
|
+
case AST_NODE_TYPES.ImportNamespaceSpecifier:
|
|
54
|
+
return specifier.local.name
|
|
55
|
+
case AST_NODE_TYPES.ImportDefaultSpecifier:
|
|
56
|
+
return specifier.local.name
|
|
57
|
+
case AST_NODE_TYPES.ImportSpecifier:
|
|
58
|
+
return specifier.local.name
|
|
59
|
+
/* v8 ignore next 2 -- @preserve Exhaustive guard. */
|
|
60
|
+
default:
|
|
61
|
+
throw new UnreachableCaseError(specifier)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
export { computeSpecifierName }
|
|
@@ -55,10 +55,17 @@ export type Options = Partial<
|
|
|
55
55
|
maxLineLength: number
|
|
56
56
|
} & CommonGroupsOptions<
|
|
57
57
|
SingleCustomGroup,
|
|
58
|
-
|
|
58
|
+
{
|
|
59
|
+
sortBy: 'specifier' | 'path'
|
|
60
|
+
},
|
|
59
61
|
CustomTypeOption
|
|
60
62
|
> &
|
|
61
|
-
CommonOptions<
|
|
63
|
+
CommonOptions<
|
|
64
|
+
CustomTypeOption,
|
|
65
|
+
{
|
|
66
|
+
sortBy: 'specifier' | 'path'
|
|
67
|
+
}
|
|
68
|
+
> &
|
|
62
69
|
CommonPartitionOptions
|
|
63
70
|
>[]
|
|
64
71
|
/**
|
|
@@ -70,6 +77,8 @@ export interface SortImportsSortingNode extends SortingNodeWithDependencies<
|
|
|
70
77
|
| TSESTree.VariableDeclaration
|
|
71
78
|
| TSESTree.ImportDeclaration
|
|
72
79
|
> {
|
|
80
|
+
/** The name of the import specifier for sorting purposes. */
|
|
81
|
+
specifierName: string | null
|
|
73
82
|
/** Whether this import is a type-only import. */
|
|
74
83
|
isTypeImport: boolean
|
|
75
84
|
/**
|
|
@@ -144,5 +153,6 @@ export declare let allModifiers: readonly [
|
|
|
144
153
|
* ensure that users do not enter invalid modifiers for a given selector.
|
|
145
154
|
*/
|
|
146
155
|
export declare let singleCustomGroupJsonSchema: Record<string, JSONSchema4>
|
|
156
|
+
export declare let sortByJsonSchema: JSONSchema4
|
|
147
157
|
export declare const TYPE_IMPORT_FIRST_TYPE_OPTION = 'type-import-first'
|
|
148
158
|
export {}
|
|
@@ -33,10 +33,15 @@ let singleCustomGroupJsonSchema = {
|
|
|
33
33
|
modifiers: buildCustomGroupModifiersJsonSchema(allModifiers),
|
|
34
34
|
selector: buildCustomGroupSelectorJsonSchema(allSelectors),
|
|
35
35
|
}
|
|
36
|
+
let sortByJsonSchema = {
|
|
37
|
+
enum: ['specifier', 'path'],
|
|
38
|
+
type: 'string',
|
|
39
|
+
}
|
|
36
40
|
const TYPE_IMPORT_FIRST_TYPE_OPTION = 'type-import-first'
|
|
37
41
|
export {
|
|
38
42
|
TYPE_IMPORT_FIRST_TYPE_OPTION,
|
|
39
43
|
allModifiers,
|
|
40
44
|
allSelectors,
|
|
41
45
|
singleCustomGroupJsonSchema,
|
|
46
|
+
sortByJsonSchema,
|
|
42
47
|
}
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
import {
|
|
10
10
|
singleCustomGroupJsonSchema,
|
|
11
11
|
TYPE_IMPORT_FIRST_TYPE_OPTION,
|
|
12
|
+
sortByJsonSchema,
|
|
12
13
|
allSelectors,
|
|
13
14
|
allModifiers,
|
|
14
15
|
} from './sort-imports/types.js'
|
|
@@ -36,6 +37,7 @@ import { isSideEffectOnlyGroup } from './sort-imports/is-side-effect-only-group.
|
|
|
36
37
|
import { computeDependencyNames } from './sort-imports/compute-dependency-names.js'
|
|
37
38
|
import { generatePredefinedGroups } from '../utils/generate-predefined-groups.js'
|
|
38
39
|
import { sortNodesByDependencies } from '../utils/sort-nodes-by-dependencies.js'
|
|
40
|
+
import { computeSpecifierName } from './sort-imports/compute-specifier-name.js'
|
|
39
41
|
import { getEslintDisabledLines } from '../utils/get-eslint-disabled-lines.js'
|
|
40
42
|
import { computeDependencies } from './sort-imports/compute-dependencies.js'
|
|
41
43
|
import { isSideEffectImport } from './sort-imports/is-side-effect-import.js'
|
|
@@ -85,6 +87,7 @@ let defaultOptions = {
|
|
|
85
87
|
customGroups: [],
|
|
86
88
|
ignoreCase: true,
|
|
87
89
|
locales: 'en-US',
|
|
90
|
+
sortBy: 'path',
|
|
88
91
|
alphabet: '',
|
|
89
92
|
order: 'asc',
|
|
90
93
|
}
|
|
@@ -193,6 +196,7 @@ const sortImports = createEslintRule({
|
|
|
193
196
|
(!isStyleSideEffect || !shouldRegroupSideEffectStyleNodes),
|
|
194
197
|
isEslintDisabled: isNodeEslintDisabled(node, eslintDisabledLines),
|
|
195
198
|
dependencyNames: computeDependencyNames({ sourceCode, node }),
|
|
199
|
+
specifierName: computeSpecifierName({ sourceCode, node }),
|
|
196
200
|
isTypeImport: modifiers.includes('type'),
|
|
197
201
|
dependencies: computeDependencies(node),
|
|
198
202
|
addSafetySemicolonWhenInline: true,
|
|
@@ -230,9 +234,11 @@ const sortImports = createEslintRule({
|
|
|
230
234
|
properties: {
|
|
231
235
|
...buildCommonJsonSchemas({
|
|
232
236
|
allowedAdditionalTypeValues: [TYPE_IMPORT_FIRST_TYPE_OPTION],
|
|
237
|
+
additionalSortProperties: { sortBy: sortByJsonSchema },
|
|
233
238
|
}),
|
|
234
239
|
...buildCommonGroupsJsonSchemas({
|
|
235
240
|
allowedAdditionalTypeValues: [TYPE_IMPORT_FIRST_TYPE_OPTION],
|
|
241
|
+
additionalSortProperties: { sortBy: sortByJsonSchema },
|
|
236
242
|
singleCustomGroupJsonSchema,
|
|
237
243
|
}),
|
|
238
244
|
tsconfig: {
|
|
@@ -13,5 +13,7 @@ declare const _default: import('@typescript-eslint/utils/ts-eslint').RuleModule<
|
|
|
13
13
|
SortObjectTypesOptions,
|
|
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
|
SortUnionTypesOptions,
|
|
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
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ComparatorByOptionsComputer } from '../../utils/compare/default-comparator-by-options-computer.js'
|
|
2
|
+
import { SortModulesSortingNode, SortModulesOptions } from './types.js'
|
|
3
|
+
export declare function buildComparatorByOptionsComputer({
|
|
4
|
+
ignoreEslintDisabledNodes,
|
|
5
|
+
sortingNodes,
|
|
6
|
+
}: {
|
|
7
|
+
sortingNodes: SortModulesSortingNode[]
|
|
8
|
+
ignoreEslintDisabledNodes: boolean
|
|
9
|
+
}): ComparatorByOptionsComputer<
|
|
10
|
+
Required<SortModulesOptions[number]>,
|
|
11
|
+
SortModulesSortingNode
|
|
12
|
+
>
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { defaultComparatorByOptionsComputer } from '../../utils/compare/default-comparator-by-options-computer.js'
|
|
2
|
+
import { sortNodesByDependencies } from '../../utils/sort-nodes-by-dependencies.js'
|
|
3
|
+
import { computeOrderedValue } from '../../utils/compare/compute-ordered-value.js'
|
|
4
|
+
import { UnreachableCaseError } from '../../utils/unreachable-case-error.js'
|
|
5
|
+
import { computeDependencies } from './compute-dependencies.js'
|
|
6
|
+
function buildComparatorByOptionsComputer({
|
|
7
|
+
ignoreEslintDisabledNodes,
|
|
8
|
+
sortingNodes,
|
|
9
|
+
}) {
|
|
10
|
+
return options => {
|
|
11
|
+
switch (options.type) {
|
|
12
|
+
case 'alphabetical':
|
|
13
|
+
case 'line-length':
|
|
14
|
+
case 'unsorted':
|
|
15
|
+
case 'natural':
|
|
16
|
+
case 'custom':
|
|
17
|
+
return defaultComparatorByOptionsComputer({
|
|
18
|
+
...options,
|
|
19
|
+
type: options.type,
|
|
20
|
+
})
|
|
21
|
+
case 'usage':
|
|
22
|
+
return buildUsageComparator({
|
|
23
|
+
ignoreEslintDisabledNodes,
|
|
24
|
+
sortingNodes,
|
|
25
|
+
options,
|
|
26
|
+
})
|
|
27
|
+
/* v8 ignore next 2 -- @preserve Exhaustive guard. */
|
|
28
|
+
default:
|
|
29
|
+
throw new UnreachableCaseError(options.type)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function buildUsageComparator({
|
|
34
|
+
ignoreEslintDisabledNodes,
|
|
35
|
+
sortingNodes,
|
|
36
|
+
options,
|
|
37
|
+
}) {
|
|
38
|
+
let orderByNode = buildOrderByNodeMap()
|
|
39
|
+
return (a, b) => {
|
|
40
|
+
let nodeA = a.node
|
|
41
|
+
let nodeB = b.node
|
|
42
|
+
let orderA = orderByNode.get(nodeA)
|
|
43
|
+
let orderB = orderByNode.get(nodeB)
|
|
44
|
+
return computeOrderedValue(orderA - orderB, options.order)
|
|
45
|
+
}
|
|
46
|
+
function buildOrderByNodeMap() {
|
|
47
|
+
let sortingNodesWithUpdatedDependencies = sortingNodes.map(
|
|
48
|
+
({ isEslintDisabled, dependencyNames, node }) => ({
|
|
49
|
+
dependencies: computeDependencies(node, { type: 'soft' }),
|
|
50
|
+
isEslintDisabled,
|
|
51
|
+
dependencyNames,
|
|
52
|
+
node,
|
|
53
|
+
}),
|
|
54
|
+
)
|
|
55
|
+
let sortedSortingNodes = sortNodesByDependencies(
|
|
56
|
+
sortingNodesWithUpdatedDependencies,
|
|
57
|
+
{ ignoreEslintDisabledNodes },
|
|
58
|
+
)
|
|
59
|
+
let orderByNodeMap = /* @__PURE__ */ new Map()
|
|
60
|
+
for (let [i, { node }] of sortedSortingNodes.entries()) {
|
|
61
|
+
orderByNodeMap.set(node, i)
|
|
62
|
+
}
|
|
63
|
+
return orderByNodeMap
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
export { buildComparatorByOptionsComputer }
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/types'
|
|
2
|
+
interface HardDependencyDetection {
|
|
3
|
+
searchStaticMethodsAndFunctionProperties: boolean
|
|
4
|
+
type: 'hard'
|
|
5
|
+
}
|
|
6
|
+
interface SoftDependencyDetection {
|
|
7
|
+
searchStaticMethodsAndFunctionProperties?: never
|
|
8
|
+
type: 'soft'
|
|
9
|
+
}
|
|
10
|
+
type DependencyDetection = HardDependencyDetection | SoftDependencyDetection
|
|
11
|
+
/**
|
|
12
|
+
* Computes the dependencies of a given AST node.
|
|
13
|
+
*
|
|
14
|
+
* @param node - The AST node to analyze.
|
|
15
|
+
* @param dependencyDetection - The dependency detection strategy. Hard
|
|
16
|
+
* dependencies consider dependencies impacting compilation, while soft
|
|
17
|
+
* dependencies also consider type references.
|
|
18
|
+
* @returns The names of the dependencies.
|
|
19
|
+
*/
|
|
20
|
+
export declare function computeDependencies(
|
|
21
|
+
node:
|
|
22
|
+
| TSESTree.ClassDeclaration
|
|
23
|
+
| TSESTree.ProgramStatement
|
|
24
|
+
| TSESTree.TSEnumMember,
|
|
25
|
+
dependencyDetection: DependencyDetection,
|
|
26
|
+
): string[]
|
|
27
|
+
export {}
|