@checkdigit/eslint-plugin 6.6.0-PR.75-0dbb → 6.6.0-PR.75-d33e
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 +9961 -395
- package/dist-cjs/metafile.json +4722 -12
- package/dist-mjs/ast/format.mjs +2 -1
- package/dist-mjs/ast/ts-tree.mjs +65 -0
- package/dist-mjs/fixture/no-service-wrapper.mjs +181 -0
- package/dist-mjs/fixture/url.mjs +11 -3
- package/dist-mjs/index.mjs +6 -3
- package/dist-types/ast/format.d.ts +2 -1
- package/dist-types/ast/ts-tree.d.ts +8 -0
- package/dist-types/fixture/no-service-wrapper.d.ts +4 -0
- package/dist-types/fixture/url.d.ts +3 -0
- package/dist-types/index.d.ts +2 -0
- package/package.json +1 -1
- package/src/ast/format.ts +2 -1
- package/src/ast/ts-tree.ts +90 -0
- package/src/fixture/no-service-wrapper.ts +244 -0
- package/src/fixture/url.ts +9 -1
- package/src/index.ts +3 -0
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
// fixture/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 { AST_NODE_TYPES, ESLintUtils, TSESTree } from '@typescript-eslint/utils';
|
|
10
|
+
import { DefinitionType, type Scope } from '@typescript-eslint/scope-manager';
|
|
11
|
+
import { PLAIN_URL_REGEXP, TOKENIZED_URL_REGEXP, replaceEndpointUrlPrefixWithDomain } from './url';
|
|
12
|
+
import { strict as assert } from 'node:assert';
|
|
13
|
+
import getDocumentationUrl from '../get-documentation-url';
|
|
14
|
+
import { getEnclosingScopeNode } from '../ast/ts-tree';
|
|
15
|
+
import { getIndentation } from '../ast/format';
|
|
16
|
+
|
|
17
|
+
export const ruleId = 'no-service-wrapper';
|
|
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
|
+
preferNativeFetch: 'Prefer native fetch over customized service wrapper.',
|
|
30
|
+
invalidOptions:
|
|
31
|
+
'"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.',
|
|
32
|
+
unknownError:
|
|
33
|
+
'Unknown error occurred in file "{{fileName}}": {{ error }}. Please manually convert the usage of customized service wrapper call to native fetch.',
|
|
34
|
+
},
|
|
35
|
+
fixable: 'code',
|
|
36
|
+
schema: [],
|
|
37
|
+
},
|
|
38
|
+
defaultOptions: [],
|
|
39
|
+
// eslint-disable-next-line max-lines-per-function
|
|
40
|
+
create(context) {
|
|
41
|
+
const sourceCode = context.sourceCode;
|
|
42
|
+
const scopeManager = sourceCode.scopeManager;
|
|
43
|
+
const parserService = ESLintUtils.getParserServices(context);
|
|
44
|
+
const typeChecker = parserService.program.getTypeChecker();
|
|
45
|
+
|
|
46
|
+
// function reportUnknownError(node: TSESTree.Node, error: string) {
|
|
47
|
+
// context.report({
|
|
48
|
+
// node,
|
|
49
|
+
// messageId: 'unknownError',
|
|
50
|
+
// data: { error, fileName: context.filename },
|
|
51
|
+
// });
|
|
52
|
+
// }
|
|
53
|
+
|
|
54
|
+
function isUrlArgumentValid(urlArgument: TSESTree.Node | undefined, scope: Scope) {
|
|
55
|
+
if (
|
|
56
|
+
(urlArgument?.type === AST_NODE_TYPES.Literal && typeof urlArgument.value === 'string') ||
|
|
57
|
+
urlArgument?.type === AST_NODE_TYPES.TemplateLiteral
|
|
58
|
+
) {
|
|
59
|
+
const urlText = sourceCode.getText(urlArgument);
|
|
60
|
+
return PLAIN_URL_REGEXP.test(urlText) || TOKENIZED_URL_REGEXP.test(urlText);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (urlArgument?.type === AST_NODE_TYPES.Identifier) {
|
|
64
|
+
const foundVariable = scope.variables.find((variable) => variable.name === urlArgument.name);
|
|
65
|
+
assert.ok(foundVariable, `Variable "${urlArgument.name}" not found in scope`);
|
|
66
|
+
const variableDefinition = foundVariable.defs.find((def) => def.type === DefinitionType.Variable);
|
|
67
|
+
assert.ok(variableDefinition, `Variable "${urlArgument.name}" not defined in scope`);
|
|
68
|
+
const variableDefinitionNode = variableDefinition.node;
|
|
69
|
+
assert.ok(variableDefinitionNode.type === AST_NODE_TYPES.VariableDeclarator);
|
|
70
|
+
assert.ok(variableDefinitionNode.init, 'Variable definition node has no init property');
|
|
71
|
+
return isUrlArgumentValid(variableDefinitionNode.init, scope);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function getType(identifier: TSESTree.Identifier) {
|
|
78
|
+
const variable = parserService.esTreeNodeToTSNodeMap.get(identifier);
|
|
79
|
+
const variableType = typeChecker.getTypeAtLocation(variable);
|
|
80
|
+
return typeChecker.typeToString(variableType);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function isServiceLikeName(name: string) {
|
|
84
|
+
return /.*[Ss]ervice$/u.test(name);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function isCalleeServiceWrapper(serviceCall: TSESTree.CallExpression) {
|
|
88
|
+
const callee = serviceCall.callee;
|
|
89
|
+
if (callee.type !== AST_NODE_TYPES.MemberExpression) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const endpoint = callee.object;
|
|
94
|
+
if (endpoint.type === AST_NODE_TYPES.Identifier) {
|
|
95
|
+
return getType(endpoint) === 'Endpoint' || isServiceLikeName(endpoint.name);
|
|
96
|
+
}
|
|
97
|
+
if (endpoint.type !== AST_NODE_TYPES.CallExpression) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const [contextArgument] = endpoint.arguments;
|
|
102
|
+
if (contextArgument?.type !== AST_NODE_TYPES.Identifier) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
if (contextArgument.name !== 'EMPTY_CONTEXT' && getType(contextArgument) !== 'InboundContext') {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
const service = endpoint.callee;
|
|
109
|
+
if (service.type === AST_NODE_TYPES.Identifier) {
|
|
110
|
+
return getType(service) === 'ResolvedService';
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (service.type !== AST_NODE_TYPES.MemberExpression) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
const services = service.object;
|
|
117
|
+
if (services.type === AST_NODE_TYPES.Identifier) {
|
|
118
|
+
return getType(services) === 'ResolvedServices';
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (services.type !== AST_NODE_TYPES.MemberExpression) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
const configuration = services.object;
|
|
125
|
+
if (configuration.type === AST_NODE_TYPES.Identifier) {
|
|
126
|
+
return ['Configuration', 'Configuration<ResolvedServices>'].includes(getType(configuration));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// following applies only to test code (fixture)
|
|
130
|
+
if (configuration.type !== AST_NODE_TYPES.MemberExpression) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
const fixture = configuration.object;
|
|
134
|
+
if (fixture.type === AST_NODE_TYPES.Identifier) {
|
|
135
|
+
return fixture.name === 'fixture' || getType(fixture) === 'Fixture';
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
'CallExpression[callee.property.name=/^(head|get|put|post|del|patch)$/]': (
|
|
143
|
+
serviceCall: TSESTree.CallExpression,
|
|
144
|
+
) => {
|
|
145
|
+
try {
|
|
146
|
+
const enclosingScopeNode = getEnclosingScopeNode(serviceCall);
|
|
147
|
+
assert.ok(enclosingScopeNode, 'enclosingScopeNode is undefined');
|
|
148
|
+
const scope = scopeManager?.acquire(enclosingScopeNode);
|
|
149
|
+
assert.ok(scope, 'scope is undefined');
|
|
150
|
+
|
|
151
|
+
const urlArgument = serviceCall.arguments[0];
|
|
152
|
+
|
|
153
|
+
if (!isUrlArgumentValid(urlArgument, scope)) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (!isCalleeServiceWrapper(serviceCall)) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
assert.ok(serviceCall.callee.type === AST_NODE_TYPES.MemberExpression);
|
|
162
|
+
assert.ok(serviceCall.callee.property.type === AST_NODE_TYPES.Identifier);
|
|
163
|
+
|
|
164
|
+
// method
|
|
165
|
+
const method = serviceCall.callee.property.name;
|
|
166
|
+
|
|
167
|
+
// body
|
|
168
|
+
const requestBodyProperty = ['put', 'post', 'options'].includes(method)
|
|
169
|
+
? serviceCall.arguments[1]
|
|
170
|
+
: undefined;
|
|
171
|
+
|
|
172
|
+
// options
|
|
173
|
+
const optionsArgument = ['get', 'head', 'del'].includes(method)
|
|
174
|
+
? serviceCall.arguments[1]
|
|
175
|
+
: serviceCall.arguments[2];
|
|
176
|
+
if (optionsArgument === undefined || optionsArgument.type !== AST_NODE_TYPES.ObjectExpression) {
|
|
177
|
+
context.report({
|
|
178
|
+
node: serviceCall,
|
|
179
|
+
messageId: 'invalidOptions',
|
|
180
|
+
});
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
const resolveWithFullResponseProperty = optionsArgument.properties.find(
|
|
184
|
+
(property) =>
|
|
185
|
+
property.type === AST_NODE_TYPES.Property &&
|
|
186
|
+
property.key.type === AST_NODE_TYPES.Identifier &&
|
|
187
|
+
property.key.name === 'resolveWithFullResponse',
|
|
188
|
+
);
|
|
189
|
+
if (
|
|
190
|
+
resolveWithFullResponseProperty?.type !== AST_NODE_TYPES.Property ||
|
|
191
|
+
resolveWithFullResponseProperty.value.type !== AST_NODE_TYPES.Literal ||
|
|
192
|
+
resolveWithFullResponseProperty.value.value !== true
|
|
193
|
+
) {
|
|
194
|
+
context.report({
|
|
195
|
+
node: optionsArgument,
|
|
196
|
+
messageId: 'invalidOptions',
|
|
197
|
+
});
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// headers
|
|
202
|
+
const requestHeadersProperty = optionsArgument.properties.find(
|
|
203
|
+
(property) =>
|
|
204
|
+
property.type === AST_NODE_TYPES.Property &&
|
|
205
|
+
property.key.type === AST_NODE_TYPES.Identifier &&
|
|
206
|
+
property.key.name === 'headers',
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
context.report({
|
|
210
|
+
messageId: 'preferNativeFetch',
|
|
211
|
+
node: serviceCall,
|
|
212
|
+
fix(fixer) {
|
|
213
|
+
const url = sourceCode.getText(urlArgument);
|
|
214
|
+
const replacedUrl = replaceEndpointUrlPrefixWithDomain(url);
|
|
215
|
+
const indentation = getIndentation(serviceCall, sourceCode);
|
|
216
|
+
|
|
217
|
+
const fetchText = [
|
|
218
|
+
`fetch(${replacedUrl}, {`,
|
|
219
|
+
` method: '${method.toUpperCase()}',`,
|
|
220
|
+
...(requestHeadersProperty ? [` ${sourceCode.getText(requestHeadersProperty)},`] : []),
|
|
221
|
+
...(requestBodyProperty ? [` body: JSON.stringify(${sourceCode.getText(requestBodyProperty)}),`] : []),
|
|
222
|
+
'})',
|
|
223
|
+
].join(`\n${indentation}`);
|
|
224
|
+
return fixer.replaceText(serviceCall, fetchText);
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
} catch (error) {
|
|
228
|
+
// eslint-disable-next-line no-console
|
|
229
|
+
console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
|
|
230
|
+
context.report({
|
|
231
|
+
node: serviceCall,
|
|
232
|
+
messageId: 'unknownError',
|
|
233
|
+
data: {
|
|
234
|
+
fileName: context.filename,
|
|
235
|
+
error: error instanceof Error ? error.toString() : JSON.stringify(error),
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
},
|
|
240
|
+
};
|
|
241
|
+
},
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
export default rule;
|
package/src/fixture/url.ts
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
// fixture/url.ts
|
|
2
2
|
|
|
3
|
+
export const PLAIN_URL_REGEXP = /^[`']\/\w+(?<serviceNamePart>-\w+)*\/v\d+\/.+[`']$/u;
|
|
4
|
+
export const TOKENIZED_URL_REGEXP = /^`\$\{(?<serviceNamePart>\w+_)*BASE_PATH\}\/.+`$/u;
|
|
5
|
+
|
|
3
6
|
export function replaceEndpointUrlPrefixWithBasePath(url: string) {
|
|
4
7
|
// eslint-disable-next-line no-template-curly-in-string
|
|
5
|
-
return url.replace(
|
|
8
|
+
return url.replace(/^`\/\w+(?<parts>-\w+)*\/v\d+\//u, '`${BASE_PATH}/');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function replaceEndpointUrlPrefixWithDomain(url: string) {
|
|
12
|
+
// eslint-disable-next-line no-template-curly-in-string
|
|
13
|
+
return url.replace(/\/(?<servicename>\w+(?<parts>-\w+)*)(?<path>\/v\d+\/.*$)/u, 'https://$1.checkdigit/$1$3');
|
|
6
14
|
}
|
package/src/index.ts
CHANGED
|
@@ -11,6 +11,7 @@ import fetchThen, { ruleId as fetchThenRuleId } from './fixture/fetch-then';
|
|
|
11
11
|
import invalidJsonStringify, { ruleId as invalidJsonStringifyRuleId } from './invalid-json-stringify';
|
|
12
12
|
import noFixture, { ruleId as noFixtureRuleId } from './fixture/no-fixture';
|
|
13
13
|
import noPromiseInstanceMethod, { ruleId as noPromiseInstanceMethodRuleId } from './no-promise-instance-method';
|
|
14
|
+
import noServiceWrapper, { ruleId as noServiceWrapperRuleId } from './fixture/no-service-wrapper';
|
|
14
15
|
import filePathComment from './file-path-comment';
|
|
15
16
|
import noCardNumbers from './no-card-numbers';
|
|
16
17
|
import noTestImport from './no-test-import';
|
|
@@ -37,6 +38,7 @@ export default {
|
|
|
37
38
|
[noFixtureRuleId]: noFixture,
|
|
38
39
|
[fetchHeaderGetterRuleId]: fetchHeaderGetter,
|
|
39
40
|
[fetchThenRuleId]: fetchThen,
|
|
41
|
+
[noServiceWrapperRuleId]: noServiceWrapper,
|
|
40
42
|
},
|
|
41
43
|
configs: {
|
|
42
44
|
all: {
|
|
@@ -55,6 +57,7 @@ export default {
|
|
|
55
57
|
[`@checkdigit/${noFixtureRuleId}`]: 'error',
|
|
56
58
|
[`@checkdigit/${fetchHeaderGetterRuleId}`]: 'error',
|
|
57
59
|
[`@checkdigit/${fetchThenRuleId}`]: 'error',
|
|
60
|
+
[`@checkdigit/${noServiceWrapperRuleId}`]: 'error',
|
|
58
61
|
},
|
|
59
62
|
},
|
|
60
63
|
recommended: {
|