@checkdigit/eslint-plugin 6.6.0-PR.79-7a20 → 6.7.0-PR.80-48a3
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/README.md +0 -1
- package/dist-cjs/index.cjs +909 -691
- package/dist-cjs/metafile.json +103 -40
- package/dist-mjs/index.mjs +13 -7
- package/dist-mjs/library/format.mjs +14 -0
- package/dist-mjs/library/tree.mjs +64 -0
- package/dist-mjs/library/ts-tree.mjs +72 -0
- package/dist-mjs/library/variable.mjs +8 -0
- package/dist-mjs/require-resolve-full-response.mjs +159 -0
- package/dist-mjs/require-type-out-of-type-only-imports.mjs +48 -0
- package/dist-types/index.d.ts +4 -3
- package/dist-types/library/format.d.ts +4 -0
- package/dist-types/library/tree.d.ts +8 -0
- package/dist-types/library/ts-tree.d.ts +9 -0
- package/dist-types/library/variable.d.ts +1 -0
- package/dist-types/require-resolve-full-response.d.ts +6 -0
- package/dist-types/require-type-out-of-type-only-imports.d.ts +4 -0
- package/package.json +1 -1
- package/src/index.ts +10 -4
- package/src/library/format.ts +20 -0
- package/src/library/tree.ts +90 -0
- package/src/library/ts-tree.ts +101 -0
- package/src/library/variable.ts +5 -0
- package/src/require-resolve-full-response.ts +200 -0
- package/src/require-type-out-of-type-only-imports.ts +63 -0
- package/dist-mjs/no-enum.mjs +0 -43
- package/dist-types/no-enum.d.ts +0 -4
- package/src/no-enum.ts +0 -53
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// require-resolve-full-response.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 { AST_NODE_TYPES, ESLintUtils, TSESTree } from '@typescript-eslint/utils';
|
|
10
|
+
import { DefinitionType, type Scope } from '@typescript-eslint/scope-manager';
|
|
11
|
+
import { strict as assert } from 'node:assert';
|
|
12
|
+
import getDocumentationUrl from './get-documentation-url';
|
|
13
|
+
import { getEnclosingScopeNode } from './library/ts-tree';
|
|
14
|
+
|
|
15
|
+
export const ruleId = 'require-resolve-full-response';
|
|
16
|
+
export const PLAIN_URL_REGEXP = /^[`']\/\w+(?<serviceNamePart>-\w+)*\/v\d+\/(?<any>.|\r|\n)+[`']$/u;
|
|
17
|
+
export const TOKENIZED_URL_REGEXP = /^`\$\{(?<serviceNamePart>[A-Z]+_)*BASE_PATH\}\/(?<any>.|\r|\n)+`$/u;
|
|
18
|
+
|
|
19
|
+
const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
|
|
20
|
+
|
|
21
|
+
const rule = createRule({
|
|
22
|
+
name: ruleId,
|
|
23
|
+
meta: {
|
|
24
|
+
type: 'suggestion',
|
|
25
|
+
docs: {
|
|
26
|
+
description: 'Prefer native fetch over customized service wrapper.',
|
|
27
|
+
},
|
|
28
|
+
messages: {
|
|
29
|
+
invalidOptions:
|
|
30
|
+
'"options" argument should be provided with "resolveWithFullResponse" property set as "true". Otherwise, it indicates that the response body will be obtained without status code assertion which could result in unexpected issue.',
|
|
31
|
+
unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}.',
|
|
32
|
+
},
|
|
33
|
+
schema: [],
|
|
34
|
+
},
|
|
35
|
+
defaultOptions: [],
|
|
36
|
+
create(context) {
|
|
37
|
+
const sourceCode = context.sourceCode;
|
|
38
|
+
const scopeManager = sourceCode.scopeManager;
|
|
39
|
+
const parserService = ESLintUtils.getParserServices(context);
|
|
40
|
+
const typeChecker = parserService.program.getTypeChecker();
|
|
41
|
+
|
|
42
|
+
function isUrlArgumentValid(urlArgument: TSESTree.Node | undefined, scope: Scope) {
|
|
43
|
+
if (
|
|
44
|
+
(urlArgument?.type === AST_NODE_TYPES.Literal && typeof urlArgument.value === 'string') ||
|
|
45
|
+
urlArgument?.type === AST_NODE_TYPES.TemplateLiteral
|
|
46
|
+
) {
|
|
47
|
+
const urlText = sourceCode.getText(urlArgument);
|
|
48
|
+
return PLAIN_URL_REGEXP.test(urlText) || TOKENIZED_URL_REGEXP.test(urlText);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (urlArgument?.type === AST_NODE_TYPES.Identifier) {
|
|
52
|
+
const foundVariable = scope.variables.find((variable) => variable.name === urlArgument.name);
|
|
53
|
+
if (foundVariable) {
|
|
54
|
+
const variableDefinition = foundVariable.defs.find((def) => def.type === DefinitionType.Variable);
|
|
55
|
+
assert.ok(variableDefinition, `Variable "${urlArgument.name}" not defined in scope`);
|
|
56
|
+
const variableDefinitionNode = variableDefinition.node;
|
|
57
|
+
assert.ok(variableDefinitionNode.type === AST_NODE_TYPES.VariableDeclarator);
|
|
58
|
+
assert.ok(variableDefinitionNode.init, 'Variable definition node has no init property');
|
|
59
|
+
return isUrlArgumentValid(variableDefinitionNode.init, scope);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function getType(identifier: TSESTree.Identifier) {
|
|
67
|
+
const variable = parserService.esTreeNodeToTSNodeMap.get(identifier);
|
|
68
|
+
const variableType = typeChecker.getTypeAtLocation(variable);
|
|
69
|
+
return typeChecker.typeToString(variableType);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function isServiceLikeName(name: string) {
|
|
73
|
+
return /.*[Ss]ervice$/u.test(name);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function isCalleeServiceWrapper(serviceCall: TSESTree.CallExpression) {
|
|
77
|
+
const callee = serviceCall.callee;
|
|
78
|
+
if (callee.type !== AST_NODE_TYPES.MemberExpression) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const endpoint = callee.object;
|
|
83
|
+
if (endpoint.type === AST_NODE_TYPES.Identifier) {
|
|
84
|
+
return getType(endpoint) === 'Endpoint' || isServiceLikeName(endpoint.name);
|
|
85
|
+
}
|
|
86
|
+
if (endpoint.type !== AST_NODE_TYPES.CallExpression) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const [contextArgument] = endpoint.arguments;
|
|
91
|
+
if (contextArgument?.type !== AST_NODE_TYPES.Identifier) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
if (contextArgument.name !== 'EMPTY_CONTEXT' && getType(contextArgument) !== 'InboundContext') {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
const service = endpoint.callee;
|
|
98
|
+
if (service.type === AST_NODE_TYPES.Identifier) {
|
|
99
|
+
return getType(service) === 'ResolvedService';
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (service.type !== AST_NODE_TYPES.MemberExpression) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
const services = service.object;
|
|
106
|
+
if (services.type === AST_NODE_TYPES.Identifier) {
|
|
107
|
+
return getType(services) === 'ResolvedServices';
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (services.type !== AST_NODE_TYPES.MemberExpression) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
const configuration = services.object;
|
|
114
|
+
if (configuration.type === AST_NODE_TYPES.Identifier) {
|
|
115
|
+
return ['Configuration', 'Configuration<ResolvedServices>'].includes(getType(configuration));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// following applies only to test code (fixture)
|
|
119
|
+
if (configuration.type !== AST_NODE_TYPES.MemberExpression) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
const fixture = configuration.object;
|
|
123
|
+
if (fixture.type === AST_NODE_TYPES.Identifier) {
|
|
124
|
+
return fixture.name === 'fixture' || getType(fixture) === 'Fixture';
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
'CallExpression[callee.property.name=/^(head|get|put|post|del|patch)$/]': (
|
|
132
|
+
serviceCall: TSESTree.CallExpression,
|
|
133
|
+
) => {
|
|
134
|
+
try {
|
|
135
|
+
if (!isCalleeServiceWrapper(serviceCall)) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const enclosingScopeNode = getEnclosingScopeNode(serviceCall);
|
|
140
|
+
assert.ok(enclosingScopeNode, 'enclosingScopeNode is undefined');
|
|
141
|
+
const scope = scopeManager?.acquire(enclosingScopeNode);
|
|
142
|
+
assert.ok(scope, 'scope is undefined');
|
|
143
|
+
const urlArgument = serviceCall.arguments[0];
|
|
144
|
+
if (!isUrlArgumentValid(urlArgument, scope)) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
assert.ok(serviceCall.callee.type === AST_NODE_TYPES.MemberExpression);
|
|
149
|
+
assert.ok(serviceCall.callee.property.type === AST_NODE_TYPES.Identifier);
|
|
150
|
+
|
|
151
|
+
// method
|
|
152
|
+
const method = serviceCall.callee.property.name;
|
|
153
|
+
|
|
154
|
+
// options
|
|
155
|
+
const optionsArgument = ['get', 'head', 'del'].includes(method)
|
|
156
|
+
? serviceCall.arguments[1]
|
|
157
|
+
: serviceCall.arguments[2];
|
|
158
|
+
if (optionsArgument === undefined || optionsArgument.type !== AST_NODE_TYPES.ObjectExpression) {
|
|
159
|
+
context.report({
|
|
160
|
+
node: serviceCall,
|
|
161
|
+
messageId: 'invalidOptions',
|
|
162
|
+
});
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const resolveWithFullResponseProperty = optionsArgument.properties.find(
|
|
167
|
+
(property) =>
|
|
168
|
+
property.type === AST_NODE_TYPES.Property &&
|
|
169
|
+
property.key.type === AST_NODE_TYPES.Identifier &&
|
|
170
|
+
property.key.name === 'resolveWithFullResponse',
|
|
171
|
+
);
|
|
172
|
+
if (
|
|
173
|
+
resolveWithFullResponseProperty?.type !== AST_NODE_TYPES.Property ||
|
|
174
|
+
resolveWithFullResponseProperty.value.type !== AST_NODE_TYPES.Literal ||
|
|
175
|
+
resolveWithFullResponseProperty.value.value !== true
|
|
176
|
+
) {
|
|
177
|
+
context.report({
|
|
178
|
+
node: optionsArgument,
|
|
179
|
+
messageId: 'invalidOptions',
|
|
180
|
+
});
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
} catch (error) {
|
|
184
|
+
// eslint-disable-next-line no-console
|
|
185
|
+
console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
|
|
186
|
+
context.report({
|
|
187
|
+
node: serviceCall,
|
|
188
|
+
messageId: 'unknownError',
|
|
189
|
+
data: {
|
|
190
|
+
fileName: context.filename,
|
|
191
|
+
error: error instanceof Error ? error.toString() : JSON.stringify(error),
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
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;
|
package/dist-mjs/no-enum.mjs
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
// src/no-enum.ts
|
|
2
|
-
import { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
|
|
3
|
-
var ruleId = "no-enum";
|
|
4
|
-
var NO_ENUM = "NO_ENUM";
|
|
5
|
-
var createRule = ESLintUtils.RuleCreator((name) => name);
|
|
6
|
-
var rule = createRule({
|
|
7
|
-
name: ruleId,
|
|
8
|
-
meta: {
|
|
9
|
-
type: "problem",
|
|
10
|
-
docs: {
|
|
11
|
-
description: "Disallow the use of `enum` in TypeScript"
|
|
12
|
-
},
|
|
13
|
-
schema: [],
|
|
14
|
-
messages: {
|
|
15
|
-
[NO_ENUM]: "Avoid using `enum` in TypeScript."
|
|
16
|
-
}
|
|
17
|
-
},
|
|
18
|
-
defaultOptions: [],
|
|
19
|
-
create(context) {
|
|
20
|
-
return {
|
|
21
|
-
TSEnumDeclaration(node) {
|
|
22
|
-
context.report({
|
|
23
|
-
node,
|
|
24
|
-
messageId: NO_ENUM
|
|
25
|
-
});
|
|
26
|
-
},
|
|
27
|
-
Property(node) {
|
|
28
|
-
if (node.key.type === TSESTree.AST_NODE_TYPES.Identifier && node.key.name === "enum" && node.value.type === TSESTree.AST_NODE_TYPES.ArrayExpression) {
|
|
29
|
-
context.report({
|
|
30
|
-
node,
|
|
31
|
-
messageId: NO_ENUM
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
var no_enum_default = rule;
|
|
39
|
-
export {
|
|
40
|
-
no_enum_default as default,
|
|
41
|
-
ruleId
|
|
42
|
-
};
|
|
43
|
-
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vc3JjL25vLWVudW0udHMiXSwKICAibWFwcGluZ3MiOiAiO0FBRUEsU0FBUyxhQUFhLGdCQUFnQjtBQVEvQixJQUFNLFNBQVM7QUFDdEIsSUFBTSxVQUFVO0FBRWhCLElBQU0sYUFBYSxZQUFZLFlBQVksQ0FBQyxTQUFTLElBQUk7QUFFekQsSUFBTSxPQUFPLFdBQVc7QUFBQSxFQUN0QixNQUFNO0FBQUEsRUFDTixNQUFNO0FBQUEsSUFDSixNQUFNO0FBQUEsSUFDTixNQUFNO0FBQUEsTUFDSixhQUFhO0FBQUEsSUFDZjtBQUFBLElBQ0EsUUFBUSxDQUFDO0FBQUEsSUFDVCxVQUFVO0FBQUEsTUFDUixDQUFDLE9BQU8sR0FBRztBQUFBLElBQ2I7QUFBQSxFQUNGO0FBQUEsRUFDQSxnQkFBZ0IsQ0FBQztBQUFBLEVBQ2pCLE9BQU8sU0FBUztBQUNkLFdBQU87QUFBQSxNQUNMLGtCQUFrQixNQUFrQztBQUNsRCxnQkFBUSxPQUFPO0FBQUEsVUFDYjtBQUFBLFVBQ0EsV0FBVztBQUFBLFFBQ2IsQ0FBQztBQUFBLE1BQ0g7QUFBQSxNQUNBLFNBQVMsTUFBeUI7QUFDaEMsWUFDRSxLQUFLLElBQUksU0FBUyxTQUFTLGVBQWUsY0FDMUMsS0FBSyxJQUFJLFNBQVMsVUFDbEIsS0FBSyxNQUFNLFNBQVMsU0FBUyxlQUFlLGlCQUM1QztBQUNBLGtCQUFRLE9BQU87QUFBQSxZQUNiO0FBQUEsWUFDQSxXQUFXO0FBQUEsVUFDYixDQUFDO0FBQUEsUUFDSDtBQUFBLE1BQ0Y7QUFBQSxJQUNGO0FBQUEsRUFDRjtBQUNGLENBQUM7QUFFRCxJQUFPLGtCQUFROyIsCiAgIm5hbWVzIjogW10KfQo=
|
package/dist-types/no-enum.d.ts
DELETED
package/src/no-enum.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
// no-enum.ts
|
|
2
|
-
|
|
3
|
-
import { ESLintUtils, TSESTree } from '@typescript-eslint/utils';
|
|
4
|
-
|
|
5
|
-
/*
|
|
6
|
-
* Copyright (c) 2021-2024 Check Digit, LLC
|
|
7
|
-
*
|
|
8
|
-
* This code is licensed under the MIT license (see LICENSE.txt for details).
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
export const ruleId = 'no-enum';
|
|
12
|
-
const NO_ENUM = 'NO_ENUM';
|
|
13
|
-
|
|
14
|
-
const createRule = ESLintUtils.RuleCreator((name) => name);
|
|
15
|
-
|
|
16
|
-
const rule = createRule({
|
|
17
|
-
name: ruleId,
|
|
18
|
-
meta: {
|
|
19
|
-
type: 'problem',
|
|
20
|
-
docs: {
|
|
21
|
-
description: 'Disallow the use of `enum` in TypeScript',
|
|
22
|
-
},
|
|
23
|
-
schema: [],
|
|
24
|
-
messages: {
|
|
25
|
-
[NO_ENUM]: 'Avoid using `enum` in TypeScript.',
|
|
26
|
-
},
|
|
27
|
-
},
|
|
28
|
-
defaultOptions: [],
|
|
29
|
-
create(context) {
|
|
30
|
-
return {
|
|
31
|
-
TSEnumDeclaration(node: TSESTree.TSEnumDeclaration) {
|
|
32
|
-
context.report({
|
|
33
|
-
node,
|
|
34
|
-
messageId: NO_ENUM,
|
|
35
|
-
});
|
|
36
|
-
},
|
|
37
|
-
Property(node: TSESTree.Property) {
|
|
38
|
-
if (
|
|
39
|
-
node.key.type === TSESTree.AST_NODE_TYPES.Identifier &&
|
|
40
|
-
node.key.name === 'enum' &&
|
|
41
|
-
node.value.type === TSESTree.AST_NODE_TYPES.ArrayExpression
|
|
42
|
-
) {
|
|
43
|
-
context.report({
|
|
44
|
-
node,
|
|
45
|
-
messageId: NO_ENUM,
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
},
|
|
49
|
-
};
|
|
50
|
-
},
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
export default rule;
|