@checkdigit/eslint-plugin 6.6.0-PR.79-7a20 → 6.7.0-PR.79-33df
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 +880 -656
- package/dist-cjs/metafile.json +90 -32
- package/dist-mjs/index.mjs +8 -3
- 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/no-enum.mjs +11 -2
- package/dist-mjs/require-resolve-full-response.mjs +159 -0
- package/dist-types/index.d.ts +2 -0
- 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/package.json +1 -1
- package/src/index.ts +5 -0
- 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/no-enum.ts +18 -3
- package/src/require-resolve-full-response.ts +200 -0
|
@@ -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;
|