@checkdigit/eslint-plugin 7.5.0 → 7.6.0-PR.75-1b09
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-mjs/agent/add-assert-import.mjs +58 -0
- package/dist-mjs/agent/add-base-path-const.mjs +65 -0
- package/dist-mjs/agent/add-base-path-import.mjs +60 -0
- package/dist-mjs/agent/add-url-domain.mjs +61 -0
- package/dist-mjs/agent/agent-test-wiring.mjs +221 -0
- package/dist-mjs/agent/fetch-response-body-json.mjs +146 -0
- package/dist-mjs/agent/fetch-response-header-getter.mjs +117 -0
- package/dist-mjs/agent/fetch-response-status.mjs +66 -0
- package/dist-mjs/agent/fetch-then.mjs +269 -0
- package/dist-mjs/agent/fetch.mjs +38 -0
- package/dist-mjs/agent/file.mjs +43 -0
- package/dist-mjs/agent/fix-function-call-arguments.mjs +153 -0
- package/dist-mjs/agent/no-fixture.mjs +361 -0
- package/dist-mjs/agent/no-mapped-response.mjs +75 -0
- package/dist-mjs/agent/no-service-wrapper.mjs +185 -0
- package/dist-mjs/agent/no-status-code.mjs +59 -0
- package/dist-mjs/agent/no-supertest.mjs +343 -0
- package/dist-mjs/agent/no-unused-function-argument.mjs +79 -0
- package/dist-mjs/agent/no-unused-imports.mjs +81 -0
- package/dist-mjs/agent/no-unused-service-variable.mjs +74 -0
- package/dist-mjs/agent/response-reference.mjs +70 -0
- package/dist-mjs/agent/url.mjs +32 -0
- package/dist-mjs/index.mjs +151 -4
- package/dist-types/agent/add-assert-import.d.ts +4 -0
- package/dist-types/agent/add-base-path-const.d.ts +4 -0
- package/dist-types/agent/add-base-path-import.d.ts +4 -0
- package/dist-types/agent/add-url-domain.d.ts +4 -0
- package/dist-types/agent/agent-test-wiring.d.ts +4 -0
- package/dist-types/agent/fetch-response-body-json.d.ts +4 -0
- package/dist-types/agent/fetch-response-header-getter.d.ts +4 -0
- package/dist-types/agent/fetch-response-status.d.ts +4 -0
- package/dist-types/agent/fetch-then.d.ts +4 -0
- package/dist-types/agent/fetch.d.ts +5 -0
- package/dist-types/agent/file.d.ts +7 -0
- package/dist-types/agent/fix-function-call-arguments.d.ts +9 -0
- package/dist-types/agent/no-fixture.d.ts +4 -0
- package/dist-types/agent/no-mapped-response.d.ts +4 -0
- package/dist-types/agent/no-service-wrapper.d.ts +4 -0
- package/dist-types/agent/no-status-code.d.ts +4 -0
- package/dist-types/agent/no-supertest.d.ts +4 -0
- package/dist-types/agent/no-unused-function-argument.d.ts +4 -0
- package/dist-types/agent/no-unused-imports.d.ts +4 -0
- package/dist-types/agent/no-unused-service-variable.d.ts +4 -0
- package/dist-types/agent/response-reference.d.ts +16 -0
- package/dist-types/agent/url.d.ts +4 -0
- package/package.json +1 -96
- package/src/agent/add-assert-import.ts +74 -0
- package/src/agent/add-base-path-const.ts +81 -0
- package/src/agent/add-base-path-import.ts +69 -0
- package/src/agent/add-url-domain.ts +76 -0
- package/src/agent/agent-test-wiring.ts +273 -0
- package/src/agent/fetch-response-body-json.ts +197 -0
- package/src/agent/fetch-response-header-getter.ts +148 -0
- package/src/agent/fetch-response-status.ts +87 -0
- package/src/agent/fetch-then.ts +357 -0
- package/src/agent/fetch.ts +57 -0
- package/src/agent/file.ts +42 -0
- package/src/agent/fix-function-call-arguments.ts +200 -0
- package/src/agent/no-fixture.ts +521 -0
- package/src/agent/no-mapped-response.ts +84 -0
- package/src/agent/no-service-wrapper.ts +241 -0
- package/src/agent/no-status-code.ts +72 -0
- package/src/agent/no-supertest.ts +497 -0
- package/src/agent/no-unused-function-argument.ts +98 -0
- package/src/agent/no-unused-imports.ts +103 -0
- package/src/agent/no-unused-service-variable.ts +93 -0
- package/src/agent/response-reference.ts +129 -0
- package/src/agent/url.ts +32 -0
- package/src/index.ts +147 -0
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
// agent/no-service-wrapper.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 { strict as assert } from 'node:assert';
|
|
10
|
+
|
|
11
|
+
import { DefinitionType, type Scope } from '@typescript-eslint/scope-manager';
|
|
12
|
+
import { AST_NODE_TYPES, ESLintUtils, TSESTree } from '@typescript-eslint/utils';
|
|
13
|
+
|
|
14
|
+
import getDocumentationUrl from '../get-documentation-url';
|
|
15
|
+
import { getEnclosingScopeNode } from '../library/ts-tree';
|
|
16
|
+
import { getIndentation } from '../library/format';
|
|
17
|
+
import { isServiceApiCallUrl, replaceEndpointUrlPrefixWithDomain } from './url';
|
|
18
|
+
|
|
19
|
+
export const ruleId = 'no-service-wrapper';
|
|
20
|
+
|
|
21
|
+
const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
|
|
22
|
+
|
|
23
|
+
const rule: ESLintUtils.RuleModule<'unknownError' | 'preferNativeFetch' | 'invalidOptions'> = createRule({
|
|
24
|
+
name: ruleId,
|
|
25
|
+
meta: {
|
|
26
|
+
type: 'suggestion',
|
|
27
|
+
docs: {
|
|
28
|
+
description: 'Prefer native fetch over customized service wrapper.',
|
|
29
|
+
},
|
|
30
|
+
messages: {
|
|
31
|
+
preferNativeFetch: 'Prefer native fetch over customized service wrapper.',
|
|
32
|
+
invalidOptions:
|
|
33
|
+
'"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. Please manually convert the usage of customized service wrapper call to native fetch.',
|
|
34
|
+
unknownError:
|
|
35
|
+
'Unknown error occurred in file "{{fileName}}": {{ error }}. Please manually convert the usage of customized service wrapper call to native fetch.',
|
|
36
|
+
},
|
|
37
|
+
fixable: 'code',
|
|
38
|
+
schema: [],
|
|
39
|
+
},
|
|
40
|
+
defaultOptions: [],
|
|
41
|
+
create(context) {
|
|
42
|
+
const sourceCode = context.sourceCode;
|
|
43
|
+
const scopeManager = sourceCode.scopeManager;
|
|
44
|
+
const parserService = ESLintUtils.getParserServices(context);
|
|
45
|
+
const typeChecker = parserService.program.getTypeChecker();
|
|
46
|
+
|
|
47
|
+
function isUrlArgumentValid(urlArgument: TSESTree.Node | undefined, scope: Scope) {
|
|
48
|
+
if (
|
|
49
|
+
(urlArgument?.type === AST_NODE_TYPES.Literal && typeof urlArgument.value === 'string') ||
|
|
50
|
+
urlArgument?.type === AST_NODE_TYPES.TemplateLiteral
|
|
51
|
+
) {
|
|
52
|
+
const urlText = sourceCode.getText(urlArgument);
|
|
53
|
+
return isServiceApiCallUrl(urlText);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (urlArgument?.type === AST_NODE_TYPES.Identifier) {
|
|
57
|
+
const foundVariable = scope.variables.find((variable) => variable.name === urlArgument.name);
|
|
58
|
+
if (foundVariable) {
|
|
59
|
+
const variableDefinition = foundVariable.defs.find((def) => def.type === DefinitionType.Variable);
|
|
60
|
+
if (variableDefinition !== undefined) {
|
|
61
|
+
const variableDefinitionNode = variableDefinition.node;
|
|
62
|
+
assert.ok(variableDefinitionNode.init, 'Variable definition node has no init property');
|
|
63
|
+
return isUrlArgumentValid(variableDefinitionNode.init, scope);
|
|
64
|
+
}
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function getType(identifier: TSESTree.Identifier) {
|
|
73
|
+
const variable = parserService.esTreeNodeToTSNodeMap.get(identifier);
|
|
74
|
+
const variableType = typeChecker.getTypeAtLocation(variable);
|
|
75
|
+
return typeChecker.typeToString(variableType);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function isServiceLikeName(name: string) {
|
|
79
|
+
return /.*[Ss]ervice$/u.test(name);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function isCalleeServiceWrapper(serviceCall: TSESTree.CallExpression) {
|
|
83
|
+
const callee = serviceCall.callee;
|
|
84
|
+
if (callee.type !== AST_NODE_TYPES.MemberExpression) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const endpoint = callee.object;
|
|
89
|
+
if (endpoint.type === AST_NODE_TYPES.Identifier) {
|
|
90
|
+
return getType(endpoint) === 'Endpoint' || isServiceLikeName(endpoint.name);
|
|
91
|
+
}
|
|
92
|
+
if (endpoint.type !== AST_NODE_TYPES.CallExpression) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const [contextArgument] = endpoint.arguments;
|
|
97
|
+
if (contextArgument?.type !== AST_NODE_TYPES.Identifier) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
if (contextArgument.name !== 'EMPTY_CONTEXT' && getType(contextArgument) !== 'InboundContext') {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
const service = endpoint.callee;
|
|
104
|
+
if (service.type === AST_NODE_TYPES.Identifier) {
|
|
105
|
+
return getType(service) === 'ResolvedService';
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (service.type !== AST_NODE_TYPES.MemberExpression) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
const services = service.object;
|
|
112
|
+
if (services.type === AST_NODE_TYPES.Identifier) {
|
|
113
|
+
return getType(services) === 'ResolvedServices';
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (services.type !== AST_NODE_TYPES.MemberExpression) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
const configuration = services.object;
|
|
120
|
+
if (configuration.type === AST_NODE_TYPES.Identifier) {
|
|
121
|
+
return ['Configuration', 'Configuration<ResolvedServices>'].includes(getType(configuration));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// following applies only to test code (fixture)
|
|
125
|
+
if (configuration.type !== AST_NODE_TYPES.MemberExpression) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
const fixture = configuration.object;
|
|
129
|
+
if (fixture.type === AST_NODE_TYPES.Identifier) {
|
|
130
|
+
return fixture.name === 'fixture' || getType(fixture) === 'Fixture';
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
'CallExpression[callee.property.name=/^(head|get|put|post|del|patch)$/]': (
|
|
138
|
+
serviceCall: TSESTree.CallExpression,
|
|
139
|
+
) => {
|
|
140
|
+
try {
|
|
141
|
+
if (!isCalleeServiceWrapper(serviceCall)) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const enclosingScopeNode = getEnclosingScopeNode(serviceCall);
|
|
146
|
+
assert.ok(enclosingScopeNode, 'enclosingScopeNode is undefined');
|
|
147
|
+
const scope = scopeManager?.acquire(enclosingScopeNode);
|
|
148
|
+
assert.ok(scope, 'scope is undefined');
|
|
149
|
+
const urlArgument = serviceCall.arguments[0];
|
|
150
|
+
if (!isUrlArgumentValid(urlArgument, scope)) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
assert.ok(serviceCall.callee.type === AST_NODE_TYPES.MemberExpression);
|
|
155
|
+
assert.ok(serviceCall.callee.property.type === AST_NODE_TYPES.Identifier);
|
|
156
|
+
|
|
157
|
+
// method
|
|
158
|
+
const method = serviceCall.callee.property.name;
|
|
159
|
+
|
|
160
|
+
// body
|
|
161
|
+
let requestBodyProperty = ['put', 'post', 'options'].includes(method) ? serviceCall.arguments[1] : undefined;
|
|
162
|
+
if (
|
|
163
|
+
requestBodyProperty !== undefined &&
|
|
164
|
+
requestBodyProperty.type === AST_NODE_TYPES.Identifier &&
|
|
165
|
+
requestBodyProperty.name === 'undefined'
|
|
166
|
+
) {
|
|
167
|
+
requestBodyProperty = undefined;
|
|
168
|
+
}
|
|
169
|
+
// options
|
|
170
|
+
const optionsArgument = ['get', 'head', 'del'].includes(method)
|
|
171
|
+
? serviceCall.arguments[1]
|
|
172
|
+
: serviceCall.arguments[2];
|
|
173
|
+
if (optionsArgument === undefined || optionsArgument.type !== AST_NODE_TYPES.ObjectExpression) {
|
|
174
|
+
context.report({
|
|
175
|
+
node: serviceCall,
|
|
176
|
+
messageId: 'invalidOptions',
|
|
177
|
+
});
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const resolveWithFullResponseProperty = optionsArgument.properties.find(
|
|
181
|
+
(property) =>
|
|
182
|
+
property.type === AST_NODE_TYPES.Property &&
|
|
183
|
+
property.key.type === AST_NODE_TYPES.Identifier &&
|
|
184
|
+
property.key.name === 'resolveWithFullResponse',
|
|
185
|
+
);
|
|
186
|
+
if (
|
|
187
|
+
resolveWithFullResponseProperty?.type !== AST_NODE_TYPES.Property ||
|
|
188
|
+
resolveWithFullResponseProperty.value.type !== AST_NODE_TYPES.Literal ||
|
|
189
|
+
resolveWithFullResponseProperty.value.value !== true
|
|
190
|
+
) {
|
|
191
|
+
context.report({
|
|
192
|
+
node: optionsArgument,
|
|
193
|
+
messageId: 'invalidOptions',
|
|
194
|
+
});
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// headers
|
|
199
|
+
const requestHeadersProperty = optionsArgument.properties.find(
|
|
200
|
+
(property) =>
|
|
201
|
+
property.type === AST_NODE_TYPES.Property &&
|
|
202
|
+
property.key.type === AST_NODE_TYPES.Identifier &&
|
|
203
|
+
property.key.name === 'headers',
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
context.report({
|
|
207
|
+
messageId: 'preferNativeFetch',
|
|
208
|
+
node: serviceCall,
|
|
209
|
+
fix(fixer) {
|
|
210
|
+
const url = sourceCode.getText(urlArgument);
|
|
211
|
+
const replacedUrl = replaceEndpointUrlPrefixWithDomain(url);
|
|
212
|
+
const indentation = getIndentation(serviceCall, sourceCode);
|
|
213
|
+
|
|
214
|
+
const fetchText = [
|
|
215
|
+
`fetch(${replacedUrl}, {`,
|
|
216
|
+
` method: '${method.toLowerCase() === 'del' ? 'DELETE' : method.toUpperCase()}',`,
|
|
217
|
+
...(requestHeadersProperty ? [` ${sourceCode.getText(requestHeadersProperty)},`] : []),
|
|
218
|
+
...(requestBodyProperty ? [` body: JSON.stringify(${sourceCode.getText(requestBodyProperty)}),`] : []),
|
|
219
|
+
'})',
|
|
220
|
+
].join(`\n${indentation}`);
|
|
221
|
+
return fixer.replaceText(serviceCall, fetchText);
|
|
222
|
+
},
|
|
223
|
+
});
|
|
224
|
+
} catch (error) {
|
|
225
|
+
// eslint-disable-next-line no-console
|
|
226
|
+
console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
|
|
227
|
+
context.report({
|
|
228
|
+
node: serviceCall,
|
|
229
|
+
messageId: 'unknownError',
|
|
230
|
+
data: {
|
|
231
|
+
fileName: context.filename,
|
|
232
|
+
error: error instanceof Error ? error.toString() : JSON.stringify(error),
|
|
233
|
+
},
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
};
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
export default rule;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// agent/no-status-code.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
|
+
|
|
11
|
+
import getDocumentationUrl from '../get-documentation-url';
|
|
12
|
+
|
|
13
|
+
export const ruleId = 'no-status-code';
|
|
14
|
+
|
|
15
|
+
const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
|
|
16
|
+
|
|
17
|
+
const rule: ESLintUtils.RuleModule<'unknownError' | 'replaceStatusCode'> = createRule({
|
|
18
|
+
name: ruleId,
|
|
19
|
+
meta: {
|
|
20
|
+
type: 'suggestion',
|
|
21
|
+
docs: {
|
|
22
|
+
description: 'Access the status code property of the fetch Response using "status" instead of "statusCode".',
|
|
23
|
+
},
|
|
24
|
+
messages: {
|
|
25
|
+
replaceStatusCode: 'Replace "statusCode" with "status".',
|
|
26
|
+
unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}.',
|
|
27
|
+
},
|
|
28
|
+
fixable: 'code',
|
|
29
|
+
schema: [],
|
|
30
|
+
},
|
|
31
|
+
defaultOptions: [],
|
|
32
|
+
create(context) {
|
|
33
|
+
const parserServices = ESLintUtils.getParserServices(context);
|
|
34
|
+
const typeChecker = parserServices.program.getTypeChecker();
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
'MemberExpression[property.name="statusCode"]': (responseStatusCode: TSESTree.MemberExpression) => {
|
|
38
|
+
try {
|
|
39
|
+
const responseNode = parserServices.esTreeNodeToTSNodeMap.get(responseStatusCode.object);
|
|
40
|
+
const responseType = typeChecker.getTypeAtLocation(responseNode);
|
|
41
|
+
|
|
42
|
+
const shouldReplace =
|
|
43
|
+
responseType.getProperties().some((symbol) => symbol.name === 'status') &&
|
|
44
|
+
!responseType.getProperties().some((symbol) => symbol.name === 'statusCode');
|
|
45
|
+
|
|
46
|
+
if (shouldReplace) {
|
|
47
|
+
context.report({
|
|
48
|
+
messageId: 'replaceStatusCode',
|
|
49
|
+
node: responseStatusCode.property,
|
|
50
|
+
fix(fixer) {
|
|
51
|
+
return fixer.replaceText(responseStatusCode.property, 'status');
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
} catch (error) {
|
|
56
|
+
// eslint-disable-next-line no-console
|
|
57
|
+
console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
|
|
58
|
+
context.report({
|
|
59
|
+
node: responseStatusCode,
|
|
60
|
+
messageId: 'unknownError',
|
|
61
|
+
data: {
|
|
62
|
+
fileName: context.filename,
|
|
63
|
+
error: error instanceof Error ? error.toString() : JSON.stringify(error),
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
export default rule;
|