eslint-plugin-perfectionist 5.10.1 → 5.11.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/package.json.js +1 -1
- package/dist/rules/sort-classes/compute-dependencies-by-sorting-node.js +26 -41
- package/dist/rules/sort-classes/types.d.ts +1 -1
- package/dist/rules/sort-enums/types.d.ts +1 -1
- package/dist/rules/sort-imports/compute-common-selectors.d.ts +1 -1
- package/dist/rules/sort-imports/compute-common-selectors.js +1 -1
- package/dist/rules/sort-imports/matches-tsconfig-paths.d.ts +1 -1
- package/dist/rules/sort-imports/types.d.ts +7 -16
- package/dist/rules/sort-imports.js +3 -16
- package/dist/rules/sort-modules/build-usage-comparator.js +1 -1
- package/dist/rules/sort-modules/compute-additional-module-statements.d.ts +19 -0
- package/dist/rules/sort-modules/compute-additional-module-statements.js +32 -0
- package/dist/rules/sort-modules/dependencies/compute-dependencies-by-sorting-node.d.ts +26 -0
- package/dist/rules/sort-modules/{compute-dependencies-by-sorting-node.js → dependencies/compute-dependencies-by-sorting-node.js} +33 -9
- package/dist/rules/sort-modules/dependencies/is-identifier-in-decorator-metadata-type-position.d.ts +13 -0
- package/dist/rules/sort-modules/dependencies/is-identifier-in-decorator-metadata-type-position.js +100 -0
- package/dist/rules/sort-modules/types.d.ts +13 -1
- package/dist/rules/sort-modules.js +39 -1
- package/dist/rules/sort-objects/sort-object.js +5 -2
- package/dist/rules/sort-objects/types.d.ts +6 -1
- package/dist/rules/sort-objects.js +2 -0
- package/dist/rules/sort-variable-declarations/sort-variable-declaration.js +5 -2
- package/dist/rules/sort-variable-declarations/types.d.ts +6 -1
- package/dist/rules/sort-variable-declarations.js +2 -0
- package/dist/types/ts-config-option.d.ts +10 -0
- package/dist/utils/{compute-dependencies-outside-functions-by-sorting-node.d.ts → compute-dependencies-outside-deferred-functions-by-sorting-node.d.ts} +4 -1
- package/dist/utils/compute-dependencies-outside-deferred-functions-by-sorting-node.js +22 -0
- package/dist/utils/get-settings.d.ts +4 -2
- package/dist/utils/get-settings.js +1 -0
- package/dist/utils/is-node-immediately-called.d.ts +4 -1
- package/dist/utils/is-node-inside-deferred-function.d.ts +26 -0
- package/dist/utils/is-node-inside-deferred-function.js +70 -0
- package/dist/utils/json-schemas/tsconfig-json-schema.d.ts +2 -0
- package/dist/utils/json-schemas/tsconfig-json-schema.js +16 -0
- package/dist/{rules/sort-imports → utils/tsconfig}/read-closest-ts-config-by-path.js +11 -2
- package/package.json +2 -2
- package/dist/rules/sort-modules/compute-dependencies-by-sorting-node.d.ts +0 -16
- package/dist/utils/compute-dependencies-outside-functions-by-sorting-node.js +0 -30
- /package/dist/{rules/sort-imports → utils/tsconfig}/get-typescript-import.d.ts +0 -0
- /package/dist/{rules/sort-imports → utils/tsconfig}/get-typescript-import.js +0 -0
- /package/dist/{rules/sort-imports → utils/tsconfig}/read-closest-ts-config-by-path.d.ts +0 -0
package/dist/package.json.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { UnreachableCaseError } from '../../utils/unreachable-case-error.js'
|
|
2
|
-
import { matches } from '../../utils/matches.js'
|
|
3
2
|
import { computeDependenciesBySortingNode as computeDependenciesBySortingNode$1 } from '../../utils/compute-dependencies-by-sorting-node.js'
|
|
4
|
-
import {
|
|
3
|
+
import { isNodeInsideDeferredFunction } from '../../utils/is-node-inside-deferred-function.js'
|
|
5
4
|
import { computeIdentifierNameDetails } from './compute-identifier-name-details.js'
|
|
6
5
|
import { AST_NODE_TYPES, AST_TOKEN_TYPES } from '@typescript-eslint/utils'
|
|
7
6
|
function computeDependenciesBySortingNode({
|
|
@@ -44,7 +43,14 @@ function computeIdentifierOrThisExpressionDependency({
|
|
|
44
43
|
classElement,
|
|
45
44
|
node,
|
|
46
45
|
}) {
|
|
47
|
-
if (
|
|
46
|
+
if (
|
|
47
|
+
classElement.type !== AST_NODE_TYPES.StaticBlock &&
|
|
48
|
+
isNodeInsideDeferredFunction({
|
|
49
|
+
ignoreCallbackDependenciesPatterns,
|
|
50
|
+
maxParent: classElement,
|
|
51
|
+
node,
|
|
52
|
+
})
|
|
53
|
+
) {
|
|
48
54
|
return null
|
|
49
55
|
}
|
|
50
56
|
let { parent } = node
|
|
@@ -73,23 +79,6 @@ function computeIdentifierOrThisExpressionDependency({
|
|
|
73
79
|
return null
|
|
74
80
|
}
|
|
75
81
|
}
|
|
76
|
-
function shouldIgnoreCallbackDependency() {
|
|
77
|
-
return computeParentNodesWithTypes({
|
|
78
|
-
allowedTypes: [AST_NODE_TYPES.CallExpression],
|
|
79
|
-
maxParent: classElement,
|
|
80
|
-
consecutiveOnly: false,
|
|
81
|
-
node,
|
|
82
|
-
}).some(shouldIgnoreCallExpression)
|
|
83
|
-
function shouldIgnoreCallExpression(callExpressionParent) {
|
|
84
|
-
return (
|
|
85
|
-
'name' in callExpressionParent.callee &&
|
|
86
|
-
matches(
|
|
87
|
-
callExpressionParent.callee.name,
|
|
88
|
-
ignoreCallbackDependenciesPatterns,
|
|
89
|
-
)
|
|
90
|
-
)
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
82
|
}
|
|
94
83
|
function computeThisExpressionDependenciesBySortingNode({
|
|
95
84
|
ignoreCallbackDependenciesPatterns,
|
|
@@ -146,27 +135,6 @@ function buildAdditionalIdentifierDependenciesComputer({
|
|
|
146
135
|
return dependency ? [dependency] : []
|
|
147
136
|
}
|
|
148
137
|
}
|
|
149
|
-
function shouldIgnoreDependencyComputation(node) {
|
|
150
|
-
switch (node.type) {
|
|
151
|
-
case AST_NODE_TYPES.TSAbstractPropertyDefinition:
|
|
152
|
-
case AST_NODE_TYPES.TSAbstractMethodDefinition:
|
|
153
|
-
case AST_NODE_TYPES.StaticBlock:
|
|
154
|
-
return false
|
|
155
|
-
case AST_NODE_TYPES.TSAbstractAccessorProperty:
|
|
156
|
-
case AST_NODE_TYPES.PropertyDefinition:
|
|
157
|
-
case AST_NODE_TYPES.AccessorProperty:
|
|
158
|
-
return (
|
|
159
|
-
node.value?.type === AST_NODE_TYPES.ArrowFunctionExpression ||
|
|
160
|
-
node.value?.type === AST_NODE_TYPES.FunctionExpression
|
|
161
|
-
)
|
|
162
|
-
case AST_NODE_TYPES.MethodDefinition:
|
|
163
|
-
case AST_NODE_TYPES.TSIndexSignature:
|
|
164
|
-
return true
|
|
165
|
-
/* v8 ignore next 2 -- @preserve Exhaustive guard. */
|
|
166
|
-
default:
|
|
167
|
-
throw new UnreachableCaseError(node)
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
138
|
function computeThisExpressionsInsideClassElement({
|
|
171
139
|
classElement,
|
|
172
140
|
sourceCode,
|
|
@@ -183,4 +151,21 @@ function computeThisExpressionsInsideClassElement({
|
|
|
183
151
|
return token.type === AST_TOKEN_TYPES.Keyword && token.value === 'this'
|
|
184
152
|
}
|
|
185
153
|
}
|
|
154
|
+
function shouldIgnoreDependencyComputation(node) {
|
|
155
|
+
switch (node.type) {
|
|
156
|
+
case AST_NODE_TYPES.TSAbstractPropertyDefinition:
|
|
157
|
+
case AST_NODE_TYPES.TSAbstractAccessorProperty:
|
|
158
|
+
case AST_NODE_TYPES.TSAbstractMethodDefinition:
|
|
159
|
+
case AST_NODE_TYPES.PropertyDefinition:
|
|
160
|
+
case AST_NODE_TYPES.AccessorProperty:
|
|
161
|
+
case AST_NODE_TYPES.StaticBlock:
|
|
162
|
+
return false
|
|
163
|
+
case AST_NODE_TYPES.MethodDefinition:
|
|
164
|
+
case AST_NODE_TYPES.TSIndexSignature:
|
|
165
|
+
return true
|
|
166
|
+
/* v8 ignore next 2 -- @preserve Exhaustive guard. */
|
|
167
|
+
default:
|
|
168
|
+
throw new UnreachableCaseError(node)
|
|
169
|
+
}
|
|
170
|
+
}
|
|
186
171
|
export { computeDependenciesBySortingNode }
|
|
@@ -50,7 +50,7 @@ export type Options = Partial<
|
|
|
50
50
|
*/
|
|
51
51
|
ignoreCallbackDependenciesPatterns: RegexOption
|
|
52
52
|
/**
|
|
53
|
-
* Enables experimental dependency detection.
|
|
53
|
+
* @deprecated Enables experimental dependency detection.
|
|
54
54
|
*/
|
|
55
55
|
useExperimentalDependencyDetection: boolean
|
|
56
56
|
} & AllCommonOptions<
|
|
@@ -47,7 +47,7 @@ export type Options = Partial<
|
|
|
47
47
|
*/
|
|
48
48
|
sortByValue: 'ifNumericEnum' | 'always' | 'never'
|
|
49
49
|
/**
|
|
50
|
-
* Enables experimental dependency detection.
|
|
50
|
+
* @deprecated Enables experimental dependency detection.
|
|
51
51
|
*/
|
|
52
52
|
useExperimentalDependencyDetection: boolean
|
|
53
53
|
} & AllCommonOptions<
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ReadClosestTsConfigByPathValue } from '
|
|
1
|
+
import { ReadClosestTsConfigByPathValue } from '../../utils/tsconfig/read-closest-ts-config-by-path.js'
|
|
2
2
|
import { RegexOption } from '../../types/common-options.js'
|
|
3
3
|
import { Selector } from './types.js'
|
|
4
4
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { matches } from '../../utils/matches.js'
|
|
2
|
-
import { getTypescriptImport } from '
|
|
2
|
+
import { getTypescriptImport } from '../../utils/tsconfig/get-typescript-import.js'
|
|
3
3
|
import { matchesTsconfigPaths } from './matches-tsconfig-paths.js'
|
|
4
4
|
import { builtinModules } from 'node:module'
|
|
5
5
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ReadClosestTsConfigByPathValue } from '
|
|
1
|
+
import { ReadClosestTsConfigByPathValue } from '../../utils/tsconfig/read-closest-ts-config-by-path.js'
|
|
2
2
|
/**
|
|
3
3
|
* Checks if an import matches any of the path mappings in tsconfig.json.
|
|
4
4
|
*
|
|
@@ -3,6 +3,7 @@ import { TSESTree } from '@typescript-eslint/types'
|
|
|
3
3
|
import { SortingNodeWithDependencies } from '../../utils/sort-nodes-by-dependencies.js'
|
|
4
4
|
import { RegexOption, TypeOption } from '../../types/common-options.js'
|
|
5
5
|
import { AllCommonOptions } from '../../types/all-common-options.js'
|
|
6
|
+
import { TsConfigOption } from '../../types/ts-config-option.js'
|
|
6
7
|
/**
|
|
7
8
|
* Configuration options for the sort-imports rule.
|
|
8
9
|
*
|
|
@@ -12,22 +13,7 @@ import { AllCommonOptions } from '../../types/all-common-options.js'
|
|
|
12
13
|
export type Options = Partial<
|
|
13
14
|
{
|
|
14
15
|
/**
|
|
15
|
-
*
|
|
16
|
-
* resolution based on tsconfig.json paths configuration.
|
|
17
|
-
*/
|
|
18
|
-
tsconfig: {
|
|
19
|
-
/**
|
|
20
|
-
* Optional filename of the TypeScript config file. `@default`
|
|
21
|
-
* tsconfig.json'.
|
|
22
|
-
*/
|
|
23
|
-
filename?: string
|
|
24
|
-
/**
|
|
25
|
-
* Root directory where to search for the TypeScript config file.
|
|
26
|
-
*/
|
|
27
|
-
rootDir: string
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Enables experimental dependency detection.
|
|
16
|
+
* @deprecated Enables experimental dependency detection.
|
|
31
17
|
*/
|
|
32
18
|
useExperimentalDependencyDetection: boolean
|
|
33
19
|
/**
|
|
@@ -42,6 +28,11 @@ export type Options = Partial<
|
|
|
42
28
|
* @default 'node'
|
|
43
29
|
*/
|
|
44
30
|
environment: 'node' | 'bun'
|
|
31
|
+
/**
|
|
32
|
+
* TypeScript configuration for resolving module paths. Enables path alias
|
|
33
|
+
* resolution based on tsconfig.json paths configuration.
|
|
34
|
+
*/
|
|
35
|
+
tsconfig: TsConfigOption
|
|
45
36
|
/**
|
|
46
37
|
* Controls whether side-effect imports should be sorted. When false,
|
|
47
38
|
* side-effect imports remain in their original positions.
|
|
@@ -45,14 +45,15 @@ import {
|
|
|
45
45
|
allSelectors,
|
|
46
46
|
} from './sort-imports/types.js'
|
|
47
47
|
import { isNonExternalReferenceTsImportEquals } from './sort-imports/is-non-external-reference-ts-import-equals.js'
|
|
48
|
+
import { readClosestTsConfigByPath } from '../utils/tsconfig/read-closest-ts-config-by-path.js'
|
|
48
49
|
import { comparatorByOptionsComputer } from './sort-imports/comparator-by-options-computer.js'
|
|
49
|
-
import { readClosestTsConfigByPath } from './sort-imports/read-closest-ts-config-by-path.js'
|
|
50
50
|
import { isSideEffectOnlyGroup } from './sort-imports/is-side-effect-only-group.js'
|
|
51
51
|
import { validateSideEffectsConfig } from './sort-imports/validate-side-effects-config.js'
|
|
52
52
|
import { computeSpecifierModifiers } from './sort-imports/compute-specifier-modifiers.js'
|
|
53
53
|
import { getOptionsWithCleanGroups } from '../utils/get-options-with-clean-groups.js'
|
|
54
54
|
import { computeCommonSelectors } from './sort-imports/compute-common-selectors.js'
|
|
55
55
|
import { computeDependencyNames } from './sort-imports/compute-dependency-names.js'
|
|
56
|
+
import { tsconfigJsonSchema } from '../utils/json-schemas/tsconfig-json-schema.js'
|
|
56
57
|
import { computeSpecifierName } from './sort-imports/compute-specifier-name.js'
|
|
57
58
|
import { computeDependencies } from './sort-imports/compute-dependencies.js'
|
|
58
59
|
import { isSideEffectImport } from './sort-imports/is-side-effect-import.js'
|
|
@@ -269,21 +270,6 @@ var sort_imports_default = createEslintRule({
|
|
|
269
270
|
allowedAdditionalTypeValues: [TYPE_IMPORT_FIRST_TYPE_OPTION],
|
|
270
271
|
additionalSortProperties: additionalSortOptionsJsonSchema,
|
|
271
272
|
}),
|
|
272
|
-
tsconfig: {
|
|
273
|
-
properties: {
|
|
274
|
-
rootDir: {
|
|
275
|
-
description: 'Specifies the tsConfig root directory.',
|
|
276
|
-
type: 'string',
|
|
277
|
-
},
|
|
278
|
-
filename: {
|
|
279
|
-
description: 'Specifies the tsConfig filename.',
|
|
280
|
-
type: 'string',
|
|
281
|
-
},
|
|
282
|
-
},
|
|
283
|
-
additionalProperties: false,
|
|
284
|
-
required: ['rootDir'],
|
|
285
|
-
type: 'object',
|
|
286
|
-
},
|
|
287
273
|
maxLineLength: {
|
|
288
274
|
description: 'Specifies the maximum line length.',
|
|
289
275
|
exclusiveMinimum: true,
|
|
@@ -305,6 +291,7 @@ var sort_imports_default = createEslintRule({
|
|
|
305
291
|
partitionByComment: partitionByCommentJsonSchema,
|
|
306
292
|
partitionByNewLine: partitionByNewlineJsonSchema,
|
|
307
293
|
internalPattern: buildRegexJsonSchema(),
|
|
294
|
+
tsconfig: tsconfigJsonSchema,
|
|
308
295
|
},
|
|
309
296
|
additionalProperties: false,
|
|
310
297
|
type: 'object',
|
|
@@ -2,7 +2,7 @@ import { populateSortingNodeGroupsWithDependencies } from '../../utils/populate-
|
|
|
2
2
|
import { isNodeDependentOnOtherNode } from '../../utils/is-node-dependent-on-other-node.js'
|
|
3
3
|
import { sortNodesByDependencies } from '../../utils/sort-nodes-by-dependencies.js'
|
|
4
4
|
import { buildSortingNodeByNodeMap } from '../../utils/build-sorting-node-by-node-map.js'
|
|
5
|
-
import { computeDependenciesBySortingNode } from './compute-dependencies-by-sorting-node.js'
|
|
5
|
+
import { computeDependenciesBySortingNode } from './dependencies/compute-dependencies-by-sorting-node.js'
|
|
6
6
|
import { computeDependencies } from './compute-dependencies.js'
|
|
7
7
|
/**
|
|
8
8
|
* Builds a comparator function for sorting module nodes based on their usage.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/types'
|
|
2
|
+
/**
|
|
3
|
+
* Computes the statement list of a node that must be analyzed as a module
|
|
4
|
+
* block.
|
|
5
|
+
*
|
|
6
|
+
* Some parsers wrap top-level code in a custom node instead of putting it
|
|
7
|
+
* directly in `Program.body`. Such a node can only be analyzed as a module
|
|
8
|
+
* block if it holds an array of statements in its `body` property.
|
|
9
|
+
*
|
|
10
|
+
* @param node - The AST node to compute the module block body of.
|
|
11
|
+
* @param additionalModuleBlockTypes - The node types to analyze as module
|
|
12
|
+
* blocks.
|
|
13
|
+
* @returns The statements of the module block, or null if the node is not a
|
|
14
|
+
* module block.
|
|
15
|
+
*/
|
|
16
|
+
export declare function computeAdditionalModuleStatements(
|
|
17
|
+
node: TSESTree.Node,
|
|
18
|
+
additionalModuleBlockTypes: string[],
|
|
19
|
+
): TSESTree.ProgramStatement[] | null
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
var BUILT_IN_MODULE_BLOCK_TYPES = ['SvelteScriptElement']
|
|
2
|
+
/**
|
|
3
|
+
* Computes the statement list of a node that must be analyzed as a module
|
|
4
|
+
* block.
|
|
5
|
+
*
|
|
6
|
+
* Some parsers wrap top-level code in a custom node instead of putting it
|
|
7
|
+
* directly in `Program.body`. Such a node can only be analyzed as a module
|
|
8
|
+
* block if it holds an array of statements in its `body` property.
|
|
9
|
+
*
|
|
10
|
+
* @param node - The AST node to compute the module block body of.
|
|
11
|
+
* @param additionalModuleBlockTypes - The node types to analyze as module
|
|
12
|
+
* blocks.
|
|
13
|
+
* @returns The statements of the module block, or null if the node is not a
|
|
14
|
+
* module block.
|
|
15
|
+
*/
|
|
16
|
+
function computeAdditionalModuleStatements(node, additionalModuleBlockTypes) {
|
|
17
|
+
if (
|
|
18
|
+
![...BUILT_IN_MODULE_BLOCK_TYPES, ...additionalModuleBlockTypes].includes(
|
|
19
|
+
node.type,
|
|
20
|
+
)
|
|
21
|
+
) {
|
|
22
|
+
return null
|
|
23
|
+
}
|
|
24
|
+
if (!('body' in node)) {
|
|
25
|
+
return null
|
|
26
|
+
}
|
|
27
|
+
if (!Array.isArray(node.body)) {
|
|
28
|
+
return null
|
|
29
|
+
}
|
|
30
|
+
return node.body
|
|
31
|
+
}
|
|
32
|
+
export { computeAdditionalModuleStatements }
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { TSESLint } from '@typescript-eslint/utils'
|
|
2
|
+
import { SortModulesSortingNodeWithoutDependencies } from './is-identifier-in-decorator-metadata-type-position.js'
|
|
3
|
+
type HardDependencyDetectionParameters = {
|
|
4
|
+
emitDecoratorMetadata: boolean
|
|
5
|
+
dependencyDetection: 'hard'
|
|
6
|
+
} & CommonParameters
|
|
7
|
+
type SoftDependencyDetectionParameters = {
|
|
8
|
+
emitDecoratorMetadata?: never
|
|
9
|
+
dependencyDetection: 'soft'
|
|
10
|
+
} & CommonParameters
|
|
11
|
+
interface CommonParameters {
|
|
12
|
+
sortingNodes: SortModulesSortingNodeWithoutDependencies[]
|
|
13
|
+
sourceCode: TSESLint.SourceCode
|
|
14
|
+
}
|
|
15
|
+
type Parameters =
|
|
16
|
+
SoftDependencyDetectionParameters | HardDependencyDetectionParameters
|
|
17
|
+
export declare function computeDependenciesBySortingNode({
|
|
18
|
+
emitDecoratorMetadata,
|
|
19
|
+
dependencyDetection,
|
|
20
|
+
sortingNodes,
|
|
21
|
+
sourceCode,
|
|
22
|
+
}: Parameters): Map<
|
|
23
|
+
SortModulesSortingNodeWithoutDependencies,
|
|
24
|
+
SortModulesSortingNodeWithoutDependencies[]
|
|
25
|
+
>
|
|
26
|
+
export {}
|
|
@@ -1,32 +1,56 @@
|
|
|
1
|
-
import { UnreachableCaseError } from '
|
|
2
|
-
import { computeDependenciesBySortingNode as computeDependenciesBySortingNode$1 } from '
|
|
3
|
-
import { computeParentNodesWithTypes } from '
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
1
|
+
import { UnreachableCaseError } from '../../../utils/unreachable-case-error.js'
|
|
2
|
+
import { computeDependenciesBySortingNode as computeDependenciesBySortingNode$1 } from '../../../utils/compute-dependencies-by-sorting-node.js'
|
|
3
|
+
import { computeParentNodesWithTypes } from '../../../utils/compute-parent-nodes-with-types.js'
|
|
4
|
+
import { isIdentifierInDecoratorMetadataTypePosition } from './is-identifier-in-decorator-metadata-type-position.js'
|
|
5
|
+
import { doesSortingNodeHaveOneOfDependencyNames } from '../../../utils/does-sorting-node-have-one-of-dependency-names.js'
|
|
6
|
+
import { isPropertyOrAccessorNode } from '../is-property-or-accessor-node.js'
|
|
7
|
+
import { isArrowFunctionNode } from '../is-arrow-function-node.js'
|
|
7
8
|
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
|
|
8
9
|
function computeDependenciesBySortingNode({
|
|
10
|
+
emitDecoratorMetadata,
|
|
9
11
|
dependencyDetection,
|
|
10
12
|
sortingNodes,
|
|
11
13
|
sourceCode,
|
|
12
14
|
}) {
|
|
13
15
|
return computeDependenciesBySortingNode$1({
|
|
16
|
+
shouldIgnoreIdentifierComputer: buildShouldIgnoreIdentifierComputer({
|
|
17
|
+
emitDecoratorMetadata,
|
|
18
|
+
dependencyDetection,
|
|
19
|
+
sortingNodes,
|
|
20
|
+
}),
|
|
14
21
|
additionalIdentifierDependenciesComputer:
|
|
15
22
|
buildAdditionalIdentifierDependenciesComputer({ sortingNodes }),
|
|
16
23
|
shouldIgnoreSortingNodeComputer:
|
|
17
24
|
buildShouldIgnoreSortingNodeComputer(dependencyDetection),
|
|
18
|
-
shouldIgnoreIdentifierComputer:
|
|
19
|
-
buildShouldIgnoreIdentifierComputer(dependencyDetection),
|
|
20
25
|
sortingNodes,
|
|
21
26
|
sourceCode,
|
|
22
27
|
})
|
|
23
28
|
}
|
|
24
|
-
function buildShouldIgnoreIdentifierComputer(
|
|
29
|
+
function buildShouldIgnoreIdentifierComputer({
|
|
30
|
+
emitDecoratorMetadata,
|
|
31
|
+
dependencyDetection,
|
|
32
|
+
sortingNodes,
|
|
33
|
+
}) {
|
|
25
34
|
return ({ referencingSortingNode, identifier }) => {
|
|
26
35
|
switch (dependencyDetection) {
|
|
27
36
|
case 'soft':
|
|
28
37
|
return false
|
|
29
38
|
case 'hard':
|
|
39
|
+
if (
|
|
40
|
+
emitDecoratorMetadata &&
|
|
41
|
+
isIdentifierInDecoratorMetadataTypePosition({
|
|
42
|
+
sortingNodes,
|
|
43
|
+
identifier,
|
|
44
|
+
})
|
|
45
|
+
) {
|
|
46
|
+
return false
|
|
47
|
+
}
|
|
48
|
+
if (
|
|
49
|
+
identifier.parent.type === AST_NODE_TYPES.TSTypeReference &&
|
|
50
|
+
identifier.parent.typeName === identifier
|
|
51
|
+
) {
|
|
52
|
+
return true
|
|
53
|
+
}
|
|
30
54
|
return !isInRelevantClassContext()
|
|
31
55
|
/* v8 ignore next 2 -- @preserve Exhaustive guard. */
|
|
32
56
|
default:
|
package/dist/rules/sort-modules/dependencies/is-identifier-in-decorator-metadata-type-position.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/types'
|
|
2
|
+
import { SortModulesSortingNode } from '../types.js'
|
|
3
|
+
export type SortModulesSortingNodeWithoutDependencies = Omit<
|
|
4
|
+
SortModulesSortingNode,
|
|
5
|
+
'dependencies'
|
|
6
|
+
>
|
|
7
|
+
export declare function isIdentifierInDecoratorMetadataTypePosition({
|
|
8
|
+
sortingNodes,
|
|
9
|
+
identifier,
|
|
10
|
+
}: {
|
|
11
|
+
sortingNodes: SortModulesSortingNodeWithoutDependencies[]
|
|
12
|
+
identifier: TSESTree.JSXIdentifier | TSESTree.Identifier
|
|
13
|
+
}): boolean
|
package/dist/rules/sort-modules/dependencies/is-identifier-in-decorator-metadata-type-position.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { UnreachableCaseError } from '../../../utils/unreachable-case-error.js'
|
|
2
|
+
import { computeParentNodesWithTypes } from '../../../utils/compute-parent-nodes-with-types.js'
|
|
3
|
+
import { getNodeDecorators } from '../../../utils/get-node-decorators.js'
|
|
4
|
+
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
|
|
5
|
+
function isIdentifierInDecoratorMetadataTypePosition({
|
|
6
|
+
sortingNodes,
|
|
7
|
+
identifier,
|
|
8
|
+
}) {
|
|
9
|
+
let { parent } = identifier
|
|
10
|
+
if (
|
|
11
|
+
parent.type !== AST_NODE_TYPES.TSTypeReference ||
|
|
12
|
+
parent.typeName !== identifier
|
|
13
|
+
) {
|
|
14
|
+
return false
|
|
15
|
+
}
|
|
16
|
+
if (isLocalNonClassReference(sortingNodes, identifier)) {
|
|
17
|
+
return false
|
|
18
|
+
}
|
|
19
|
+
let typeAnnotationHost = computeTypeAnnotationHost(parent)
|
|
20
|
+
if (!typeAnnotationHost) {
|
|
21
|
+
return false
|
|
22
|
+
}
|
|
23
|
+
switch (typeAnnotationHost.type) {
|
|
24
|
+
case AST_NODE_TYPES.TSAbstractPropertyDefinition:
|
|
25
|
+
case AST_NODE_TYPES.PropertyDefinition:
|
|
26
|
+
case AST_NODE_TYPES.AccessorProperty:
|
|
27
|
+
return getNodeDecorators(typeAnnotationHost).length > 0
|
|
28
|
+
case AST_NODE_TYPES.FunctionExpression: {
|
|
29
|
+
let methodDefinition = typeAnnotationHost.parent
|
|
30
|
+
if (methodDefinition.type !== AST_NODE_TYPES.MethodDefinition) {
|
|
31
|
+
return false
|
|
32
|
+
}
|
|
33
|
+
if (methodDefinition.kind === 'set') {
|
|
34
|
+
return getNodeDecorators(methodDefinition).length > 0
|
|
35
|
+
}
|
|
36
|
+
if (
|
|
37
|
+
methodDefinition.value.params.some(
|
|
38
|
+
parameter => getNodeDecorators(parameter).length > 0,
|
|
39
|
+
)
|
|
40
|
+
) {
|
|
41
|
+
return true
|
|
42
|
+
}
|
|
43
|
+
switch (methodDefinition.kind) {
|
|
44
|
+
case 'constructor':
|
|
45
|
+
return getNodeDecorators(methodDefinition.parent.parent).length > 0
|
|
46
|
+
case 'method':
|
|
47
|
+
case 'get':
|
|
48
|
+
return getNodeDecorators(methodDefinition).length > 0
|
|
49
|
+
/* v8 ignore next 2 -- @preserve Exhaustive guard. */
|
|
50
|
+
default:
|
|
51
|
+
throw new UnreachableCaseError(methodDefinition.kind)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
default:
|
|
55
|
+
return false
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function computeTypeAnnotationHost(typeReference) {
|
|
59
|
+
let annotation = typeReference.parent
|
|
60
|
+
if (
|
|
61
|
+
annotation.type === AST_NODE_TYPES.TSArrayType &&
|
|
62
|
+
annotation.parent.type === AST_NODE_TYPES.TSTypeAnnotation &&
|
|
63
|
+
annotation.parent.parent.type === AST_NODE_TYPES.RestElement
|
|
64
|
+
) {
|
|
65
|
+
annotation = annotation.parent
|
|
66
|
+
}
|
|
67
|
+
if (annotation.type !== AST_NODE_TYPES.TSTypeAnnotation) {
|
|
68
|
+
return null
|
|
69
|
+
}
|
|
70
|
+
return (
|
|
71
|
+
computeParentNodesWithTypes({
|
|
72
|
+
allowedTypes: [
|
|
73
|
+
AST_NODE_TYPES.Identifier,
|
|
74
|
+
AST_NODE_TYPES.ObjectPattern,
|
|
75
|
+
AST_NODE_TYPES.ArrayPattern,
|
|
76
|
+
AST_NODE_TYPES.RestElement,
|
|
77
|
+
AST_NODE_TYPES.AssignmentPattern,
|
|
78
|
+
AST_NODE_TYPES.TSParameterProperty,
|
|
79
|
+
],
|
|
80
|
+
consecutiveOnly: true,
|
|
81
|
+
node: annotation,
|
|
82
|
+
maxParent: null,
|
|
83
|
+
}).at(-1)?.parent ?? annotation.parent
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
function isLocalNonClassReference(sortingNodes, identifier) {
|
|
87
|
+
let matchingSortingNode = sortingNodes.find(
|
|
88
|
+
sortingNode => sortingNode.name === identifier.name,
|
|
89
|
+
)
|
|
90
|
+
if (!matchingSortingNode) {
|
|
91
|
+
return false
|
|
92
|
+
}
|
|
93
|
+
return (
|
|
94
|
+
(matchingSortingNode.node.type === AST_NODE_TYPES.ExportNamedDeclaration ?
|
|
95
|
+
matchingSortingNode.node.declaration
|
|
96
|
+
: matchingSortingNode.node
|
|
97
|
+
)?.type !== AST_NODE_TYPES.ClassDeclaration
|
|
98
|
+
)
|
|
99
|
+
}
|
|
100
|
+
export { isIdentifierInDecoratorMetadataTypePosition }
|
|
@@ -4,6 +4,7 @@ import { SortingNodeWithDependencies } from '../../utils/sort-nodes-by-dependenc
|
|
|
4
4
|
import { NewlinesBetweenOption } from '../../types/common-groups-options.js'
|
|
5
5
|
import { RegexOption, TypeOption } from '../../types/common-options.js'
|
|
6
6
|
import { AllCommonOptions } from '../../types/all-common-options.js'
|
|
7
|
+
import { TsConfigOption } from '../../types/ts-config-option.js'
|
|
7
8
|
/**
|
|
8
9
|
* Configuration options for the sort-modules rule.
|
|
9
10
|
*
|
|
@@ -19,9 +20,20 @@ export type Options = [
|
|
|
19
20
|
*/
|
|
20
21
|
newlinesBetweenOverloadSignatures: NewlinesBetweenOption
|
|
21
22
|
/**
|
|
22
|
-
* Enables experimental dependency detection.
|
|
23
|
+
* @deprecated Enables experimental dependency detection.
|
|
23
24
|
*/
|
|
24
25
|
useExperimentalDependencyDetection: boolean
|
|
26
|
+
/**
|
|
27
|
+
* Node types to analyze as module blocks, in addition to the built-in
|
|
28
|
+
* ones. Lets parsers that wrap top-level code in a custom node be
|
|
29
|
+
* supported.
|
|
30
|
+
*/
|
|
31
|
+
additionalModuleBlockTypes: string[]
|
|
32
|
+
/**
|
|
33
|
+
* TypeScript configuration used to read compiler options such as
|
|
34
|
+
* `emitDecoratorMetadata`.
|
|
35
|
+
*/
|
|
36
|
+
tsconfig: TsConfigOption
|
|
25
37
|
} & AllCommonOptions<
|
|
26
38
|
CustomTypeOption,
|
|
27
39
|
AdditionalSortOptions,
|
|
@@ -40,13 +40,16 @@ import { getDecoratorName } from '../utils/get-decorator-name.js'
|
|
|
40
40
|
import { buildOverloadSignatureNewlinesBetweenValueGetter } from '../utils/overload-signature/build-overload-signature-newlines-between-value-getter.js'
|
|
41
41
|
import { populateSortingNodeGroupsWithOverloadSignature } from '../utils/overload-signature/populate-sorting-node-groups-with-overload-signature.js'
|
|
42
42
|
import '../utils/assert-is-never.js'
|
|
43
|
+
import { readClosestTsConfigByPath } from '../utils/tsconfig/read-closest-ts-config-by-path.js'
|
|
44
|
+
import { tsconfigJsonSchema } from '../utils/json-schemas/tsconfig-json-schema.js'
|
|
43
45
|
import {
|
|
44
46
|
USAGE_TYPE_OPTION,
|
|
45
47
|
additionalCustomGroupMatchOptionsJsonSchema,
|
|
46
48
|
allModifiers,
|
|
47
49
|
allSelectors,
|
|
48
50
|
} from './sort-modules/types.js'
|
|
49
|
-
import { computeDependenciesBySortingNode } from './sort-modules/compute-dependencies-by-sorting-node.js'
|
|
51
|
+
import { computeDependenciesBySortingNode } from './sort-modules/dependencies/compute-dependencies-by-sorting-node.js'
|
|
52
|
+
import { computeAdditionalModuleStatements } from './sort-modules/compute-additional-module-statements.js'
|
|
50
53
|
import { buildComparatorByOptionsComputer } from './sort-modules/build-comparator-by-options-computer.js'
|
|
51
54
|
import { computeOverloadSignatureGroups } from './sort-modules/compute-overload-signature-groups.js'
|
|
52
55
|
import { computeNodeDetails } from './sort-modules/compute-node-details.js'
|
|
@@ -79,6 +82,8 @@ var defaultOptions = {
|
|
|
79
82
|
newlinesBetweenOverloadSignatures: 0,
|
|
80
83
|
fallbackSort: { type: 'unsorted' },
|
|
81
84
|
newlinesInside: 'newlinesBetween',
|
|
85
|
+
additionalModuleBlockTypes: [],
|
|
86
|
+
tsconfig: { rootDir: '' },
|
|
82
87
|
partitionByComment: false,
|
|
83
88
|
partitionByNewLine: false,
|
|
84
89
|
newlinesBetween: 'ignore',
|
|
@@ -103,11 +108,18 @@ var sort_modules_default = createEslintRule({
|
|
|
103
108
|
additionalCustomGroupMatchOptionsJsonSchema,
|
|
104
109
|
allowedAdditionalTypeValues: [USAGE_TYPE_OPTION],
|
|
105
110
|
}),
|
|
111
|
+
additionalModuleBlockTypes: {
|
|
112
|
+
description:
|
|
113
|
+
'Specifies the node types to analyze as module blocks, in addition to the built-in ones.',
|
|
114
|
+
items: { type: 'string' },
|
|
115
|
+
type: 'array',
|
|
116
|
+
},
|
|
106
117
|
useExperimentalDependencyDetection:
|
|
107
118
|
useExperimentalDependencyDetectionJsonSchema,
|
|
108
119
|
newlinesBetweenOverloadSignatures: newlinesBetweenJsonSchema,
|
|
109
120
|
partitionByComment: partitionByCommentJsonSchema,
|
|
110
121
|
partitionByNewLine: partitionByNewlineJsonSchema,
|
|
122
|
+
tsconfig: tsconfigJsonSchema,
|
|
111
123
|
},
|
|
112
124
|
additionalProperties: false,
|
|
113
125
|
type: 'object',
|
|
@@ -171,6 +183,21 @@ function analyzeModule({
|
|
|
171
183
|
)
|
|
172
184
|
let sortingNodeGroupsWithoutOverloadSignature = [[]]
|
|
173
185
|
for (let node of module.body) {
|
|
186
|
+
let additionalModuleStatements = computeAdditionalModuleStatements(
|
|
187
|
+
node,
|
|
188
|
+
options.additionalModuleBlockTypes,
|
|
189
|
+
)
|
|
190
|
+
if (additionalModuleStatements) {
|
|
191
|
+
sortingNodeGroupsWithoutOverloadSignature.push([])
|
|
192
|
+
analyzeModule({
|
|
193
|
+
module: { body: additionalModuleStatements },
|
|
194
|
+
eslintDisabledLines,
|
|
195
|
+
sourceCode,
|
|
196
|
+
options,
|
|
197
|
+
context,
|
|
198
|
+
})
|
|
199
|
+
continue
|
|
200
|
+
}
|
|
174
201
|
switch (node.type) {
|
|
175
202
|
case AST_NODE_TYPES.TSNamespaceExportDeclaration:
|
|
176
203
|
case AST_NODE_TYPES.ExportAllDeclaration:
|
|
@@ -304,7 +331,18 @@ function analyzeModule({
|
|
|
304
331
|
overloadSignatureGroups,
|
|
305
332
|
})
|
|
306
333
|
if (options.useExperimentalDependencyDetection) {
|
|
334
|
+
let tsConfigOutput =
|
|
335
|
+
options.tsconfig.rootDir ?
|
|
336
|
+
readClosestTsConfigByPath({
|
|
337
|
+
tsconfigFilename: options.tsconfig.filename ?? 'tsconfig.json',
|
|
338
|
+
tsconfigRootDir: options.tsconfig.rootDir,
|
|
339
|
+
filePath: context.physicalFilename,
|
|
340
|
+
contextCwd: context.cwd,
|
|
341
|
+
})
|
|
342
|
+
: null
|
|
307
343
|
let dependenciesBySortingNode = computeDependenciesBySortingNode({
|
|
344
|
+
emitDecoratorMetadata:
|
|
345
|
+
tsConfigOutput?.compilerOptions.emitDecoratorMetadata ?? false,
|
|
308
346
|
sortingNodes: sortingNodeGroups.flat(),
|
|
309
347
|
dependencyDetection: 'hard',
|
|
310
348
|
sourceCode,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { computeDependenciesOutsideDeferredFunctionsBySortingNode } from '../../utils/compute-dependencies-outside-deferred-functions-by-sorting-node.js'
|
|
2
2
|
import { populateSortingNodeGroupsWithDependencies } from '../../utils/populate-sorting-node-groups-with-dependencies.js'
|
|
3
3
|
import { validateNewlinesAndPartitionConfig } from '../../utils/validate-newlines-and-partition-config.js'
|
|
4
4
|
import { buildOptionsByGroupIndexComputer } from '../../utils/build-options-by-group-index-computer.js'
|
|
@@ -41,6 +41,7 @@ import { AST_NODE_TYPES } from '@typescript-eslint/utils'
|
|
|
41
41
|
var cachedGroupsByModifiersAndSelectors = /* @__PURE__ */ new Map()
|
|
42
42
|
var defaultOptions = {
|
|
43
43
|
useExperimentalDependencyDetection: true,
|
|
44
|
+
ignoreCallbackDependenciesPatterns: [],
|
|
44
45
|
fallbackSort: { type: 'unsorted' },
|
|
45
46
|
newlinesInside: 'newlinesBetween',
|
|
46
47
|
partitionByComputedKey: false,
|
|
@@ -181,7 +182,9 @@ function sortObject({ matchedAstSelectors, context, node }) {
|
|
|
181
182
|
}
|
|
182
183
|
if (options.useExperimentalDependencyDetection) {
|
|
183
184
|
let dependenciesBySortingNode =
|
|
184
|
-
|
|
185
|
+
computeDependenciesOutsideDeferredFunctionsBySortingNode({
|
|
186
|
+
ignoreCallbackDependenciesPatterns:
|
|
187
|
+
options.ignoreCallbackDependenciesPatterns,
|
|
185
188
|
sortingNodes: sortingNodeGroups.flat(),
|
|
186
189
|
sourceCode,
|
|
187
190
|
})
|
|
@@ -69,7 +69,12 @@ export type Options = Partial<
|
|
|
69
69
|
matchesAstSelector?: string
|
|
70
70
|
}
|
|
71
71
|
/**
|
|
72
|
-
*
|
|
72
|
+
* Regex patterns for function names whose callback argument dependencies
|
|
73
|
+
* should be ignored.
|
|
74
|
+
*/
|
|
75
|
+
ignoreCallbackDependenciesPatterns: RegexOption
|
|
76
|
+
/**
|
|
77
|
+
* @deprecated Enables experimental dependency detection.
|
|
73
78
|
*/
|
|
74
79
|
useExperimentalDependencyDetection: boolean
|
|
75
80
|
/**
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
buildCommonJsonSchemas,
|
|
3
|
+
buildRegexJsonSchema,
|
|
3
4
|
buildUseConfigIfJsonSchema,
|
|
4
5
|
matchesAstSelectorJsonSchema,
|
|
5
6
|
useExperimentalDependencyDetectionJsonSchema,
|
|
@@ -73,6 +74,7 @@ var sort_objects_default = createEslintRule({
|
|
|
73
74
|
},
|
|
74
75
|
useExperimentalDependencyDetection:
|
|
75
76
|
useExperimentalDependencyDetectionJsonSchema,
|
|
77
|
+
ignoreCallbackDependenciesPatterns: buildRegexJsonSchema(),
|
|
76
78
|
partitionByComment: partitionByCommentJsonSchema,
|
|
77
79
|
partitionByNewLine: partitionByNewlineJsonSchema,
|
|
78
80
|
},
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
ORDER_ERROR_ID,
|
|
7
7
|
allSelectors,
|
|
8
8
|
} from './types.js'
|
|
9
|
-
import {
|
|
9
|
+
import { computeDependenciesOutsideDeferredFunctionsBySortingNode } from '../../utils/compute-dependencies-outside-deferred-functions-by-sorting-node.js'
|
|
10
10
|
import { populateSortingNodeGroupsWithDependencies } from '../../utils/populate-sorting-node-groups-with-dependencies.js'
|
|
11
11
|
import { defaultComparatorByOptionsComputer } from '../../utils/compare/default-comparator-by-options-computer.js'
|
|
12
12
|
import { validateNewlinesAndPartitionConfig } from '../../utils/validate-newlines-and-partition-config.js'
|
|
@@ -35,6 +35,7 @@ import { complete } from '../../utils/complete.js'
|
|
|
35
35
|
var cachedGroupsByModifiersAndSelectors = /* @__PURE__ */ new Map()
|
|
36
36
|
var defaultOptions = {
|
|
37
37
|
useExperimentalDependencyDetection: true,
|
|
38
|
+
ignoreCallbackDependenciesPatterns: [],
|
|
38
39
|
fallbackSort: { type: 'unsorted' },
|
|
39
40
|
newlinesInside: 'newlinesBetween',
|
|
40
41
|
specialCharacters: 'keep',
|
|
@@ -133,7 +134,9 @@ function sortVariableDeclaration({ matchedAstSelectors, context, node }) {
|
|
|
133
134
|
)
|
|
134
135
|
if (options.useExperimentalDependencyDetection) {
|
|
135
136
|
let dependenciesBySortingNode =
|
|
136
|
-
|
|
137
|
+
computeDependenciesOutsideDeferredFunctionsBySortingNode({
|
|
138
|
+
ignoreCallbackDependenciesPatterns:
|
|
139
|
+
options.ignoreCallbackDependenciesPatterns,
|
|
137
140
|
sortingNodes: sortingNodeGroups.flat(),
|
|
138
141
|
sourceCode,
|
|
139
142
|
})
|
|
@@ -41,7 +41,12 @@ export type Options = Partial<
|
|
|
41
41
|
matchesAstSelector?: string
|
|
42
42
|
}
|
|
43
43
|
/**
|
|
44
|
-
*
|
|
44
|
+
* Regex patterns for function names whose callback argument dependencies
|
|
45
|
+
* should be ignored.
|
|
46
|
+
*/
|
|
47
|
+
ignoreCallbackDependenciesPatterns: RegexOption
|
|
48
|
+
/**
|
|
49
|
+
* @deprecated Enables experimental dependency detection.
|
|
45
50
|
*/
|
|
46
51
|
useExperimentalDependencyDetection: boolean
|
|
47
52
|
} & AllCommonOptions<
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
buildCommonJsonSchemas,
|
|
3
|
+
buildRegexJsonSchema,
|
|
3
4
|
buildUseConfigIfJsonSchema,
|
|
4
5
|
matchesAstSelectorJsonSchema,
|
|
5
6
|
useExperimentalDependencyDetectionJsonSchema,
|
|
@@ -48,6 +49,7 @@ var sort_variable_declarations_default = createEslintRule({
|
|
|
48
49
|
}),
|
|
49
50
|
useExperimentalDependencyDetection:
|
|
50
51
|
useExperimentalDependencyDetectionJsonSchema,
|
|
52
|
+
ignoreCallbackDependenciesPatterns: buildRegexJsonSchema(),
|
|
51
53
|
partitionByComment: partitionByCommentJsonSchema,
|
|
52
54
|
partitionByNewLine: partitionByNewlineJsonSchema,
|
|
53
55
|
},
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import { TSESTree } from '@typescript-eslint/types'
|
|
2
2
|
import { TSESLint } from '@typescript-eslint/utils'
|
|
3
3
|
import { SortingNodeWithDependencies } from './sort-nodes-by-dependencies.js'
|
|
4
|
-
|
|
4
|
+
import { RegexOption } from '../types/common-options.js'
|
|
5
|
+
export declare function computeDependenciesOutsideDeferredFunctionsBySortingNode<
|
|
5
6
|
Node extends TSESTree.Node,
|
|
6
7
|
T extends Pick<SortingNodeWithDependencies<Node>, 'dependencyNames' | 'node'>,
|
|
7
8
|
>({
|
|
9
|
+
ignoreCallbackDependenciesPatterns,
|
|
8
10
|
sortingNodes,
|
|
9
11
|
sourceCode,
|
|
10
12
|
}: {
|
|
13
|
+
ignoreCallbackDependenciesPatterns: RegexOption
|
|
11
14
|
sourceCode: TSESLint.SourceCode
|
|
12
15
|
sortingNodes: T[]
|
|
13
16
|
}): Map<T, T[]>
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { computeDependenciesBySortingNode } from './compute-dependencies-by-sorting-node.js'
|
|
2
|
+
import { isNodeInsideDeferredFunction } from './is-node-inside-deferred-function.js'
|
|
3
|
+
function computeDependenciesOutsideDeferredFunctionsBySortingNode({
|
|
4
|
+
ignoreCallbackDependenciesPatterns,
|
|
5
|
+
sortingNodes,
|
|
6
|
+
sourceCode,
|
|
7
|
+
}) {
|
|
8
|
+
return computeDependenciesBySortingNode({
|
|
9
|
+
shouldIgnoreIdentifierComputer: buildShouldIgnoreIdentifierComputer(),
|
|
10
|
+
sortingNodes,
|
|
11
|
+
sourceCode,
|
|
12
|
+
})
|
|
13
|
+
function buildShouldIgnoreIdentifierComputer() {
|
|
14
|
+
return ({ referencingSortingNode, identifier }) =>
|
|
15
|
+
isNodeInsideDeferredFunction({
|
|
16
|
+
maxParent: referencingSortingNode.node,
|
|
17
|
+
ignoreCallbackDependenciesPatterns,
|
|
18
|
+
node: identifier,
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export { computeDependenciesOutsideDeferredFunctionsBySortingNode }
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { TSESLint } from '@typescript-eslint/utils'
|
|
2
2
|
import { CommonPartitionOptions } from '../types/common-partition-options.js'
|
|
3
3
|
import { CommonGroupsOptions } from '../types/common-groups-options.js'
|
|
4
|
+
import { TsConfigOption } from '../types/ts-config-option.js'
|
|
4
5
|
import { CommonOptions } from '../types/common-options.js'
|
|
5
6
|
/**
|
|
6
7
|
* Global settings for the Perfectionist plugin.
|
|
@@ -13,8 +14,9 @@ export type Settings = Partial<
|
|
|
13
14
|
Pick<
|
|
14
15
|
CommonGroupsOptions<string, unknown, unknown>,
|
|
15
16
|
'newlinesBetween' | 'newlinesInside'
|
|
16
|
-
> &
|
|
17
|
-
|
|
17
|
+
> & {
|
|
18
|
+
tsconfig: TsConfigOption
|
|
19
|
+
} & CommonPartitionOptions &
|
|
18
20
|
CommonOptions
|
|
19
21
|
>
|
|
20
22
|
/**
|
|
@@ -10,5 +10,8 @@ import { TSESTree } from '@typescript-eslint/types'
|
|
|
10
10
|
* @returns Whether the node is immediately called.
|
|
11
11
|
*/
|
|
12
12
|
export declare function isNodeImmediatelyCalled(
|
|
13
|
-
node:
|
|
13
|
+
node:
|
|
14
|
+
| TSESTree.ArrowFunctionExpression
|
|
15
|
+
| TSESTree.FunctionDeclaration
|
|
16
|
+
| TSESTree.FunctionExpression,
|
|
14
17
|
): boolean
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/types'
|
|
2
|
+
import { RegexOption } from '../types/common-options.js'
|
|
3
|
+
/**
|
|
4
|
+
* Checks whether a node sits inside a function body that is deferred.
|
|
5
|
+
*
|
|
6
|
+
* Used to distinguish between references that are evaluated immediately (like
|
|
7
|
+
* IIFEs or synchronous callbacks) and those whose execution is delayed (like
|
|
8
|
+
* assigned functions or explicitly ignored callback wrappers).
|
|
9
|
+
*
|
|
10
|
+
* @param params - The parameters object.
|
|
11
|
+
* @param params.ignoreCallbackDependenciesPatterns - Optional regex pattern to
|
|
12
|
+
* explicitly treat matching callback wrappers as deferred.
|
|
13
|
+
* @param params.maxParent - Maximum exclusive parent node to stop the search
|
|
14
|
+
* at.
|
|
15
|
+
* @param params.node - The AST node whose enclosing functions are inspected.
|
|
16
|
+
* @returns Whether the node lives inside a deferred function.
|
|
17
|
+
*/
|
|
18
|
+
export declare function isNodeInsideDeferredFunction({
|
|
19
|
+
ignoreCallbackDependenciesPatterns,
|
|
20
|
+
maxParent,
|
|
21
|
+
node,
|
|
22
|
+
}: {
|
|
23
|
+
ignoreCallbackDependenciesPatterns: RegexOption
|
|
24
|
+
maxParent: TSESTree.Node
|
|
25
|
+
node: TSESTree.Node
|
|
26
|
+
}): boolean
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { matches } from './matches.js'
|
|
2
|
+
import { computeParentNodesWithTypes } from './compute-parent-nodes-with-types.js'
|
|
3
|
+
import { isNodeImmediatelyCalled } from './is-node-immediately-called.js'
|
|
4
|
+
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
|
|
5
|
+
/**
|
|
6
|
+
* Checks whether a node sits inside a function body that is deferred.
|
|
7
|
+
*
|
|
8
|
+
* Used to distinguish between references that are evaluated immediately (like
|
|
9
|
+
* IIFEs or synchronous callbacks) and those whose execution is delayed (like
|
|
10
|
+
* assigned functions or explicitly ignored callback wrappers).
|
|
11
|
+
*
|
|
12
|
+
* @param params - The parameters object.
|
|
13
|
+
* @param params.ignoreCallbackDependenciesPatterns - Optional regex pattern to
|
|
14
|
+
* explicitly treat matching callback wrappers as deferred.
|
|
15
|
+
* @param params.maxParent - Maximum exclusive parent node to stop the search
|
|
16
|
+
* at.
|
|
17
|
+
* @param params.node - The AST node whose enclosing functions are inspected.
|
|
18
|
+
* @returns Whether the node lives inside a deferred function.
|
|
19
|
+
*/
|
|
20
|
+
function isNodeInsideDeferredFunction({
|
|
21
|
+
ignoreCallbackDependenciesPatterns,
|
|
22
|
+
maxParent,
|
|
23
|
+
node,
|
|
24
|
+
}) {
|
|
25
|
+
if (
|
|
26
|
+
ignoreCallbackDependenciesPatterns &&
|
|
27
|
+
matchesIgnoreCallbackDependencyPattern({
|
|
28
|
+
ignoreCallbackDependenciesPatterns,
|
|
29
|
+
maxParent,
|
|
30
|
+
node,
|
|
31
|
+
})
|
|
32
|
+
) {
|
|
33
|
+
return true
|
|
34
|
+
}
|
|
35
|
+
return computeParentNodesWithTypes({
|
|
36
|
+
allowedTypes: [
|
|
37
|
+
AST_NODE_TYPES.FunctionExpression,
|
|
38
|
+
AST_NODE_TYPES.ArrowFunctionExpression,
|
|
39
|
+
],
|
|
40
|
+
consecutiveOnly: false,
|
|
41
|
+
maxParent,
|
|
42
|
+
node,
|
|
43
|
+
}).some(isFunctionDeferred)
|
|
44
|
+
}
|
|
45
|
+
function matchesIgnoreCallbackDependencyPattern({
|
|
46
|
+
ignoreCallbackDependenciesPatterns,
|
|
47
|
+
maxParent,
|
|
48
|
+
node,
|
|
49
|
+
}) {
|
|
50
|
+
return computeParentNodesWithTypes({
|
|
51
|
+
allowedTypes: [AST_NODE_TYPES.CallExpression],
|
|
52
|
+
consecutiveOnly: false,
|
|
53
|
+
maxParent,
|
|
54
|
+
node,
|
|
55
|
+
}).some(callExpressionMatchesCallbackDependencyPattern)
|
|
56
|
+
function callExpressionMatchesCallbackDependencyPattern(callExpression) {
|
|
57
|
+
return (
|
|
58
|
+
'name' in callExpression.callee &&
|
|
59
|
+
matches(callExpression.callee.name, ignoreCallbackDependenciesPatterns)
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function isFunctionDeferred(functionNode) {
|
|
64
|
+
let { parent } = functionNode
|
|
65
|
+
if (isNodeImmediatelyCalled(functionNode)) {
|
|
66
|
+
return false
|
|
67
|
+
}
|
|
68
|
+
return parent.type !== AST_NODE_TYPES.CallExpression
|
|
69
|
+
}
|
|
70
|
+
export { isNodeInsideDeferredFunction }
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
var tsconfigJsonSchema = {
|
|
2
|
+
properties: {
|
|
3
|
+
rootDir: {
|
|
4
|
+
description: 'Specifies the tsConfig root directory.',
|
|
5
|
+
type: 'string',
|
|
6
|
+
},
|
|
7
|
+
filename: {
|
|
8
|
+
description: 'Specifies the tsConfig filename.',
|
|
9
|
+
type: 'string',
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
additionalProperties: false,
|
|
13
|
+
required: ['rootDir'],
|
|
14
|
+
type: 'object',
|
|
15
|
+
}
|
|
16
|
+
export { tsconfigJsonSchema }
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { getTypescriptImport } from './get-typescript-import.js'
|
|
2
2
|
import * as path from 'node:path'
|
|
3
3
|
import * as fs from 'node:fs'
|
|
4
|
+
/**
|
|
5
|
+
* Diagnostics that don't affect compiler options resolution. They only concern
|
|
6
|
+
* the list of input files, which is irrelevant here, and are commonly emitted
|
|
7
|
+
* for empty or solution-style tsconfig files.
|
|
8
|
+
*/
|
|
9
|
+
var IGNORED_DIAGNOSTIC_CODES = /* @__PURE__ */ new Set([18002, 18003])
|
|
4
10
|
/**
|
|
5
11
|
* Cache mapping directories to their nearest tsconfig file path. Speeds up
|
|
6
12
|
* config resolution for files in the same directory.
|
|
@@ -88,9 +94,12 @@ function getCompilerOptions(typescriptImport, contextCwd, filePath) {
|
|
|
88
94
|
typescriptImport.sys,
|
|
89
95
|
path.dirname(filePath),
|
|
90
96
|
)
|
|
91
|
-
|
|
97
|
+
let fatalErrors = parsedContent.errors.filter(
|
|
98
|
+
error => !IGNORED_DIAGNOSTIC_CODES.has(error.code),
|
|
99
|
+
)
|
|
100
|
+
if (fatalErrors.length > 0) {
|
|
92
101
|
throw new Error(
|
|
93
|
-
`Error getting compiler options: ${JSON.stringify(
|
|
102
|
+
`Error getting compiler options: ${JSON.stringify(fatalErrors)}`,
|
|
94
103
|
)
|
|
95
104
|
}
|
|
96
105
|
let cache = typescriptImport.createModuleResolutionCache(
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-perfectionist",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.11.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",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"./dist"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@typescript-eslint/utils": "^8.
|
|
32
|
+
"@typescript-eslint/utils": "^8.68.0",
|
|
33
33
|
"natural-orderby": "^5.0.0"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { TSESLint } from '@typescript-eslint/utils'
|
|
2
|
-
import { SortModulesSortingNode, DependencyDetection } from './types.js'
|
|
3
|
-
type SortingNodeWithoutDependencies = Omit<
|
|
4
|
-
SortModulesSortingNode,
|
|
5
|
-
'dependencies'
|
|
6
|
-
>
|
|
7
|
-
export declare function computeDependenciesBySortingNode({
|
|
8
|
-
dependencyDetection,
|
|
9
|
-
sortingNodes,
|
|
10
|
-
sourceCode,
|
|
11
|
-
}: {
|
|
12
|
-
sortingNodes: SortingNodeWithoutDependencies[]
|
|
13
|
-
dependencyDetection: DependencyDetection
|
|
14
|
-
sourceCode: TSESLint.SourceCode
|
|
15
|
-
}): Map<SortingNodeWithoutDependencies, SortingNodeWithoutDependencies[]>
|
|
16
|
-
export {}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { computeDependenciesBySortingNode } from './compute-dependencies-by-sorting-node.js'
|
|
2
|
-
import { computeParentNodesWithTypes } from './compute-parent-nodes-with-types.js'
|
|
3
|
-
import { isNodeImmediatelyCalled } from './is-node-immediately-called.js'
|
|
4
|
-
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
|
|
5
|
-
function computeDependenciesOutsideFunctionsBySortingNode({
|
|
6
|
-
sortingNodes,
|
|
7
|
-
sourceCode,
|
|
8
|
-
}) {
|
|
9
|
-
return computeDependenciesBySortingNode({
|
|
10
|
-
shouldIgnoreIdentifierComputer: buildShouldIgnoreIdentifierComputer(),
|
|
11
|
-
sortingNodes,
|
|
12
|
-
sourceCode,
|
|
13
|
-
})
|
|
14
|
-
function buildShouldIgnoreIdentifierComputer() {
|
|
15
|
-
return ({ referencingSortingNode, identifier }) => {
|
|
16
|
-
return computeParentNodesWithTypes({
|
|
17
|
-
allowedTypes: [
|
|
18
|
-
AST_NODE_TYPES.FunctionExpression,
|
|
19
|
-
AST_NODE_TYPES.ArrowFunctionExpression,
|
|
20
|
-
],
|
|
21
|
-
maxParent: referencingSortingNode.node,
|
|
22
|
-
consecutiveOnly: false,
|
|
23
|
-
node: identifier,
|
|
24
|
-
}).some(
|
|
25
|
-
functionParentNode => !isNodeImmediatelyCalled(functionParentNode),
|
|
26
|
-
)
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
export { computeDependenciesOutsideFunctionsBySortingNode }
|
|
File without changes
|
|
File without changes
|
|
File without changes
|