@checkdigit/eslint-plugin 7.6.0-PR.75-7ee9 → 7.6.0-PR.97-b19c

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.
Files changed (77) hide show
  1. package/dist-mjs/index.mjs +8 -156
  2. package/dist-mjs/no-status-code-assert.mjs +63 -0
  3. package/dist-mjs/require-resolve-full-response.mjs +5 -5
  4. package/dist-types/no-status-code-assert.d.ts +5 -0
  5. package/package.json +1 -1
  6. package/src/index.ts +4 -152
  7. package/src/no-status-code-assert.ts +85 -0
  8. package/src/require-resolve-full-response.ts +4 -4
  9. package/dist-mjs/agent/add-assert-import.mjs +0 -58
  10. package/dist-mjs/agent/add-base-path-const.mjs +0 -65
  11. package/dist-mjs/agent/add-base-path-import.mjs +0 -60
  12. package/dist-mjs/agent/add-url-domain.mjs +0 -61
  13. package/dist-mjs/agent/agent-test-wiring.mjs +0 -221
  14. package/dist-mjs/agent/fetch-response-body-json.mjs +0 -146
  15. package/dist-mjs/agent/fetch-response-header-getter.mjs +0 -117
  16. package/dist-mjs/agent/fetch-response-status.mjs +0 -76
  17. package/dist-mjs/agent/fetch-then.mjs +0 -273
  18. package/dist-mjs/agent/fetch.mjs +0 -48
  19. package/dist-mjs/agent/file.mjs +0 -43
  20. package/dist-mjs/agent/fix-function-call-arguments.mjs +0 -153
  21. package/dist-mjs/agent/no-fixture.mjs +0 -397
  22. package/dist-mjs/agent/no-mapped-response.mjs +0 -75
  23. package/dist-mjs/agent/no-service-wrapper.mjs +0 -185
  24. package/dist-mjs/agent/no-status-code.mjs +0 -59
  25. package/dist-mjs/agent/no-supertest.mjs +0 -346
  26. package/dist-mjs/agent/no-unused-function-argument.mjs +0 -79
  27. package/dist-mjs/agent/no-unused-imports.mjs +0 -81
  28. package/dist-mjs/agent/no-unused-service-variable.mjs +0 -74
  29. package/dist-mjs/agent/response-reference.mjs +0 -78
  30. package/dist-mjs/agent/supertest-then.mjs +0 -170
  31. package/dist-mjs/agent/url.mjs +0 -32
  32. package/dist-types/agent/add-assert-import.d.ts +0 -4
  33. package/dist-types/agent/add-base-path-const.d.ts +0 -4
  34. package/dist-types/agent/add-base-path-import.d.ts +0 -4
  35. package/dist-types/agent/add-url-domain.d.ts +0 -4
  36. package/dist-types/agent/agent-test-wiring.d.ts +0 -4
  37. package/dist-types/agent/fetch-response-body-json.d.ts +0 -4
  38. package/dist-types/agent/fetch-response-header-getter.d.ts +0 -4
  39. package/dist-types/agent/fetch-response-status.d.ts +0 -4
  40. package/dist-types/agent/fetch-then.d.ts +0 -4
  41. package/dist-types/agent/fetch.d.ts +0 -8
  42. package/dist-types/agent/file.d.ts +0 -7
  43. package/dist-types/agent/fix-function-call-arguments.d.ts +0 -9
  44. package/dist-types/agent/no-fixture.d.ts +0 -4
  45. package/dist-types/agent/no-mapped-response.d.ts +0 -4
  46. package/dist-types/agent/no-service-wrapper.d.ts +0 -4
  47. package/dist-types/agent/no-status-code.d.ts +0 -4
  48. package/dist-types/agent/no-supertest.d.ts +0 -4
  49. package/dist-types/agent/no-unused-function-argument.d.ts +0 -4
  50. package/dist-types/agent/no-unused-imports.d.ts +0 -4
  51. package/dist-types/agent/no-unused-service-variable.d.ts +0 -4
  52. package/dist-types/agent/response-reference.d.ts +0 -17
  53. package/dist-types/agent/supertest-then.d.ts +0 -4
  54. package/dist-types/agent/url.d.ts +0 -4
  55. package/src/agent/add-assert-import.ts +0 -74
  56. package/src/agent/add-base-path-const.ts +0 -81
  57. package/src/agent/add-base-path-import.ts +0 -69
  58. package/src/agent/add-url-domain.ts +0 -76
  59. package/src/agent/agent-test-wiring.ts +0 -273
  60. package/src/agent/fetch-response-body-json.ts +0 -194
  61. package/src/agent/fetch-response-header-getter.ts +0 -148
  62. package/src/agent/fetch-response-status.ts +0 -100
  63. package/src/agent/fetch-then.ts +0 -359
  64. package/src/agent/fetch.ts +0 -69
  65. package/src/agent/file.ts +0 -42
  66. package/src/agent/fix-function-call-arguments.ts +0 -200
  67. package/src/agent/no-fixture.ts +0 -581
  68. package/src/agent/no-mapped-response.ts +0 -84
  69. package/src/agent/no-service-wrapper.ts +0 -241
  70. package/src/agent/no-status-code.ts +0 -69
  71. package/src/agent/no-supertest.ts +0 -517
  72. package/src/agent/no-unused-function-argument.ts +0 -98
  73. package/src/agent/no-unused-imports.ts +0 -103
  74. package/src/agent/no-unused-service-variable.ts +0 -93
  75. package/src/agent/response-reference.ts +0 -153
  76. package/src/agent/supertest-then.ts +0 -230
  77. package/src/agent/url.ts +0 -32
@@ -1,241 +0,0 @@
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 parserServices = ESLintUtils.getParserServices(context);
45
- const typeChecker = parserServices.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 = parserServices.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;
@@ -1,69 +0,0 @@
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
- import { isFetchResponse } from './fetch';
13
-
14
- export const ruleId = 'no-status-code';
15
-
16
- const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
17
-
18
- const rule: ESLintUtils.RuleModule<'unknownError' | 'replaceStatusCode'> = createRule({
19
- name: ruleId,
20
- meta: {
21
- type: 'suggestion',
22
- docs: {
23
- description: 'Access the status code property of the fetch Response using "status" instead of "statusCode".',
24
- },
25
- messages: {
26
- replaceStatusCode: 'Replace "statusCode" with "status".',
27
- unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}.',
28
- },
29
- fixable: 'code',
30
- schema: [],
31
- },
32
- defaultOptions: [],
33
- create(context) {
34
- const parserServices = ESLintUtils.getParserServices(context);
35
- const typeChecker = parserServices.program.getTypeChecker();
36
-
37
- return {
38
- 'MemberExpression[property.name="statusCode"]': (responseStatusCode: TSESTree.MemberExpression) => {
39
- try {
40
- const responseNode = parserServices.esTreeNodeToTSNodeMap.get(responseStatusCode.object);
41
- const responseType = typeChecker.getTypeAtLocation(responseNode);
42
-
43
- if (isFetchResponse(responseType)) {
44
- context.report({
45
- messageId: 'replaceStatusCode',
46
- node: responseStatusCode.property,
47
- fix(fixer) {
48
- return fixer.replaceText(responseStatusCode.property, 'status');
49
- },
50
- });
51
- }
52
- } catch (error) {
53
- // eslint-disable-next-line no-console
54
- console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
55
- context.report({
56
- node: responseStatusCode,
57
- messageId: 'unknownError',
58
- data: {
59
- fileName: context.filename,
60
- error: error instanceof Error ? error.toString() : JSON.stringify(error),
61
- },
62
- });
63
- }
64
- },
65
- };
66
- },
67
- });
68
-
69
- export default rule;