@checkdigit/eslint-plugin 6.6.0-PR.75-edd9 → 6.6.0-PR.75-2a52
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-cjs/index.cjs +846 -616
- package/dist-cjs/metafile.json +153 -37
- package/dist-mjs/agent/no-unused-function-argument.mjs +71 -0
- package/dist-mjs/index.mjs +23 -15
- package/dist-mjs/no-duplicated-imports.mjs +87 -0
- package/dist-mjs/require-fixed-services-import.mjs +46 -0
- package/dist-mjs/require-type-out-of-type-only-imports.mjs +48 -0
- package/dist-types/agent/no-unused-function-argument.d.ts +4 -0
- package/dist-types/index.d.ts +9 -11
- package/dist-types/no-duplicated-imports.d.ts +4 -0
- package/dist-types/require-fixed-services-import.d.ts +4 -0
- package/dist-types/require-type-out-of-type-only-imports.d.ts +4 -0
- package/package.json +1 -1
- package/src/agent/add-url-domain.ts +1 -1
- package/src/agent/fetch-response-body-json.ts +1 -1
- package/src/agent/fetch-response-header-getter.ts +1 -1
- package/src/agent/fetch-then.ts +1 -1
- package/src/agent/fetch.ts +1 -1
- package/src/agent/no-fixture.ts +1 -1
- package/src/agent/no-full-response.ts +1 -1
- package/src/agent/no-mapped-response.ts +1 -1
- package/src/agent/no-service-wrapper.ts +1 -1
- package/src/agent/no-status-code.ts +1 -1
- package/src/agent/no-unused-function-argument.ts +83 -0
- package/src/agent/response-reference.ts +1 -1
- package/src/agent/url.ts +1 -1
- package/src/index.ts +19 -11
- package/src/library/format.ts +1 -1
- package/src/library/tree.ts +1 -1
- package/src/library/ts-tree.ts +1 -1
- package/src/library/variable.ts +1 -1
- package/src/no-duplicated-imports.ts +116 -0
- package/src/require-fixed-services-import.ts +52 -0
- package/src/require-type-out-of-type-only-imports.ts +63 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// no-duplicated-imports.ts
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright (c) 2021-2024 Check Digit, LLC
|
|
5
|
+
*
|
|
6
|
+
* This code is licensed under the MIT license (see LICENSE.txt for details).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { ESLintUtils, TSESTree } from '@typescript-eslint/utils';
|
|
10
|
+
import { strict as assert } from 'node:assert';
|
|
11
|
+
import getDocumentationUrl from './get-documentation-url';
|
|
12
|
+
|
|
13
|
+
export const ruleId = 'no-duplicated-imports';
|
|
14
|
+
|
|
15
|
+
const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
|
|
16
|
+
|
|
17
|
+
const rule = createRule({
|
|
18
|
+
name: ruleId,
|
|
19
|
+
meta: {
|
|
20
|
+
type: 'suggestion',
|
|
21
|
+
docs: {
|
|
22
|
+
description: 'Merge duplicated import statements with the same "from".',
|
|
23
|
+
},
|
|
24
|
+
messages: {
|
|
25
|
+
mergeDuplicatedImports: 'Merge duplicated import statements with the same "from".',
|
|
26
|
+
},
|
|
27
|
+
fixable: 'code',
|
|
28
|
+
schema: [],
|
|
29
|
+
},
|
|
30
|
+
defaultOptions: [],
|
|
31
|
+
create(context) {
|
|
32
|
+
const sourceCode = context.sourceCode;
|
|
33
|
+
const importDeclarations = new Map<string, TSESTree.ImportDeclaration[]>();
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
ImportDeclaration(node) {
|
|
37
|
+
const moduleName = node.source.value;
|
|
38
|
+
let declarations = importDeclarations.get(moduleName);
|
|
39
|
+
if (declarations === undefined) {
|
|
40
|
+
declarations = [];
|
|
41
|
+
importDeclarations.set(moduleName, declarations);
|
|
42
|
+
}
|
|
43
|
+
declarations.push(node);
|
|
44
|
+
},
|
|
45
|
+
'Program:exit'() {
|
|
46
|
+
for (const [moduleName, declarations] of importDeclarations.entries()) {
|
|
47
|
+
if (declarations.length <= 1) {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const firstDeclaration = declarations[0];
|
|
52
|
+
assert.ok(firstDeclaration);
|
|
53
|
+
|
|
54
|
+
const isAllTypeOnly = declarations.every(
|
|
55
|
+
(declaration) =>
|
|
56
|
+
declaration.importKind === 'type' ||
|
|
57
|
+
declaration.specifiers.every(
|
|
58
|
+
(specifier) =>
|
|
59
|
+
specifier.type === TSESTree.AST_NODE_TYPES.ImportSpecifier && specifier.importKind === 'type',
|
|
60
|
+
),
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
context.report({
|
|
64
|
+
messageId: 'mergeDuplicatedImports',
|
|
65
|
+
node: firstDeclaration,
|
|
66
|
+
fix(fixer) {
|
|
67
|
+
const fixes = [];
|
|
68
|
+
|
|
69
|
+
const defaultSpecifier = declarations
|
|
70
|
+
.flatMap((declaration) =>
|
|
71
|
+
declaration.specifiers.map((specifier) =>
|
|
72
|
+
specifier.type === TSESTree.AST_NODE_TYPES.ImportDefaultSpecifier ? specifier : undefined,
|
|
73
|
+
),
|
|
74
|
+
)
|
|
75
|
+
.filter(Boolean);
|
|
76
|
+
const defaultSpecifierText = defaultSpecifier[0] ? sourceCode.getText(defaultSpecifier[0]) : undefined;
|
|
77
|
+
|
|
78
|
+
const mergedSpecifiers = declarations.flatMap((declaration) => {
|
|
79
|
+
const isCurrentDeclarationTypeOnly =
|
|
80
|
+
declaration.importKind === 'type' ||
|
|
81
|
+
declaration.specifiers.every(
|
|
82
|
+
(specifier) =>
|
|
83
|
+
specifier.type === TSESTree.AST_NODE_TYPES.ImportSpecifier && specifier.importKind === 'type',
|
|
84
|
+
);
|
|
85
|
+
return declaration.specifiers
|
|
86
|
+
.filter((specifier) => specifier.type !== TSESTree.AST_NODE_TYPES.ImportDefaultSpecifier)
|
|
87
|
+
.map((specifier) =>
|
|
88
|
+
// eslint-disable-next-line no-nested-ternary
|
|
89
|
+
isAllTypeOnly
|
|
90
|
+
? sourceCode.getText(specifier).replace('type ', '')
|
|
91
|
+
: isCurrentDeclarationTypeOnly
|
|
92
|
+
? `type ${sourceCode.getText(specifier)}`
|
|
93
|
+
: sourceCode.getText(specifier),
|
|
94
|
+
);
|
|
95
|
+
});
|
|
96
|
+
const mergedSpecifiersText = `${isAllTypeOnly ? 'type ' : ''}{ ${mergedSpecifiers.join(', ')} }`;
|
|
97
|
+
|
|
98
|
+
// Replace the first import with the merged import
|
|
99
|
+
const mergedImport = `import ${[defaultSpecifierText, mergedSpecifiersText].filter(Boolean).join(', ')} from '${moduleName}';`;
|
|
100
|
+
fixes.push(fixer.replaceText(firstDeclaration, mergedImport));
|
|
101
|
+
|
|
102
|
+
// Remove the remaining imports
|
|
103
|
+
declarations.slice(1).forEach((declaration) => {
|
|
104
|
+
fixes.push(fixer.removeRange([declaration.range[0], declaration.range[1] + 1]));
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
return fixes;
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
export default rule;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// require-fixed-services-import.ts
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright (c) 2021-2024 Check Digit, LLC
|
|
5
|
+
*
|
|
6
|
+
* This code is licensed under the MIT license (see LICENSE.txt for details).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
10
|
+
import getDocumentationUrl from './get-documentation-url';
|
|
11
|
+
|
|
12
|
+
export const ruleId = 'require-fixed-services-import';
|
|
13
|
+
|
|
14
|
+
const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
|
|
15
|
+
const SERVICE_TYPINGS_IMPORT_PATH_PREFIX = /(?<path>\.\.\/)+services\/.*/u;
|
|
16
|
+
|
|
17
|
+
const rule = createRule({
|
|
18
|
+
name: ruleId,
|
|
19
|
+
meta: {
|
|
20
|
+
type: 'suggestion',
|
|
21
|
+
docs: {
|
|
22
|
+
description: 'Require fixed "from" with service typing imports from "src/services".',
|
|
23
|
+
},
|
|
24
|
+
messages: {
|
|
25
|
+
updateServicesImportFrom: 'Update service typing imports to be from the fixed "src/services" path.',
|
|
26
|
+
},
|
|
27
|
+
fixable: 'code',
|
|
28
|
+
schema: [],
|
|
29
|
+
},
|
|
30
|
+
defaultOptions: [],
|
|
31
|
+
create(context) {
|
|
32
|
+
return {
|
|
33
|
+
ImportDeclaration(node) {
|
|
34
|
+
const moduleName = node.source.value;
|
|
35
|
+
if (SERVICE_TYPINGS_IMPORT_PATH_PREFIX.test(moduleName)) {
|
|
36
|
+
context.report({
|
|
37
|
+
messageId: 'updateServicesImportFrom',
|
|
38
|
+
node: node.source,
|
|
39
|
+
*fix(fixer) {
|
|
40
|
+
yield fixer.replaceText(
|
|
41
|
+
node.source,
|
|
42
|
+
`'${moduleName.slice(0, moduleName.indexOf('../services') + '../services'.length)}'`,
|
|
43
|
+
);
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
export default rule;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// require-type-out-of-type-only-imports.ts
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright (c) 2021-2024 Check Digit, LLC
|
|
5
|
+
*
|
|
6
|
+
* This code is licensed under the MIT license (see LICENSE.txt for details).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { ESLintUtils, TSESTree } from '@typescript-eslint/utils';
|
|
10
|
+
import getDocumentationUrl from './get-documentation-url';
|
|
11
|
+
|
|
12
|
+
export const ruleId = 'require-type-out-of-type-only-imports';
|
|
13
|
+
|
|
14
|
+
const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
|
|
15
|
+
|
|
16
|
+
const rule = createRule({
|
|
17
|
+
name: ruleId,
|
|
18
|
+
meta: {
|
|
19
|
+
type: 'suggestion',
|
|
20
|
+
docs: {
|
|
21
|
+
description: 'Require "type" to be out side of type-only imports.',
|
|
22
|
+
},
|
|
23
|
+
messages: {
|
|
24
|
+
moveTypeOutside: 'Update the type-only imports to use "tpe" outside of the curly braces.',
|
|
25
|
+
},
|
|
26
|
+
fixable: 'code',
|
|
27
|
+
schema: [],
|
|
28
|
+
},
|
|
29
|
+
defaultOptions: [],
|
|
30
|
+
create(context) {
|
|
31
|
+
const sourceCode = context.sourceCode;
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
ImportDeclaration(declaration) {
|
|
35
|
+
if (
|
|
36
|
+
declaration.importKind === 'type' ||
|
|
37
|
+
!declaration.specifiers.every(
|
|
38
|
+
(specifier) =>
|
|
39
|
+
specifier.type === TSESTree.AST_NODE_TYPES.ImportSpecifier && specifier.importKind === 'type',
|
|
40
|
+
)
|
|
41
|
+
) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
context.report({
|
|
46
|
+
messageId: 'moveTypeOutside',
|
|
47
|
+
node: declaration,
|
|
48
|
+
*fix(fixer) {
|
|
49
|
+
const moduleName = declaration.source.value;
|
|
50
|
+
const mergedSpecifiers = declaration.specifiers
|
|
51
|
+
.filter((specifier) => specifier.type !== TSESTree.AST_NODE_TYPES.ImportDefaultSpecifier)
|
|
52
|
+
.map((specifier) => sourceCode.getText(specifier).replace('type ', ''));
|
|
53
|
+
const updatedImportDeclaration = `import type { ${mergedSpecifiers.join(', ')} } from '${moduleName}';`;
|
|
54
|
+
|
|
55
|
+
yield fixer.replaceText(declaration, updatedImportDeclaration);
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
export default rule;
|