@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,359 +0,0 @@
1
- // agent/fetch-then.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 { ScopeManager, Variable } from '@typescript-eslint/scope-manager';
12
- import { AST_NODE_TYPES, ESLintUtils, TSESTree } from '@typescript-eslint/utils';
13
- import type { SourceCode } from '@typescript-eslint/utils/ts-eslint';
14
-
15
- import { getEnclosingFunction, getEnclosingStatement, getParent, isUsedInArrayOrAsArgument } from '../library/ts-tree';
16
- import getDocumentationUrl from '../get-documentation-url';
17
- import { getIndentation } from '../library/format';
18
- import { isValidPropertyName } from '../library/variable';
19
- import { hasAssertions, isInvalidResponseHeadersAccess } from './fetch';
20
- import { replaceEndpointUrlPrefixWithBasePath } from './url';
21
-
22
- export const ruleId = 'fetch-then';
23
-
24
- interface FixtureCallInformation {
25
- fixtureNode: TSESTree.CallExpression;
26
- requestBody?: TSESTree.Expression;
27
- requestHeaders?: { name: TSESTree.Expression; value: TSESTree.Expression }[];
28
- assertions?: TSESTree.Expression[][];
29
- }
30
-
31
- // recursively analyze the fixture/supertest call chain to collect information of request/response
32
- function analyzeFixtureCall(call: TSESTree.CallExpression, results: FixtureCallInformation, sourceCode: SourceCode) {
33
- const parent = getParent(call);
34
- if (!parent) {
35
- return;
36
- }
37
-
38
- let nextCall;
39
- if (parent.type !== AST_NODE_TYPES.MemberExpression) {
40
- results.fixtureNode = call;
41
- return;
42
- }
43
-
44
- if (parent.property.type === AST_NODE_TYPES.Identifier) {
45
- if (parent.property.name === 'expect') {
46
- // supertest assertions
47
- const assertionCall = getParent(parent);
48
- assert.ok(assertionCall && assertionCall.type === AST_NODE_TYPES.CallExpression);
49
- results.assertions = [...(results.assertions ?? []), assertionCall.arguments as TSESTree.Expression[]];
50
- nextCall = assertionCall;
51
- } else if (parent.property.name === 'send') {
52
- // request body
53
- const sendRequestBodyCall = getParent(parent);
54
- assert.ok(sendRequestBodyCall && sendRequestBodyCall.type === AST_NODE_TYPES.CallExpression);
55
- results.requestBody = sendRequestBodyCall.arguments[0] as TSESTree.Expression;
56
- nextCall = sendRequestBodyCall;
57
- } else if (parent.property.name === 'set') {
58
- // request headers
59
- const setRequestHeaderCall = getParent(parent);
60
- assert.ok(setRequestHeaderCall && setRequestHeaderCall.type === AST_NODE_TYPES.CallExpression);
61
- const [name, value] = setRequestHeaderCall.arguments as [TSESTree.Expression, TSESTree.Expression];
62
- results.requestHeaders = [...(results.requestHeaders ?? []), { name, value }];
63
- nextCall = setRequestHeaderCall;
64
- }
65
- } else {
66
- throw new Error(`Unexpected TSESTree.Expression in fixture/supertest call ${sourceCode.getText(parent)}.`);
67
- }
68
- if (nextCall) {
69
- analyzeFixtureCall(nextCall, results, sourceCode);
70
- }
71
- }
72
-
73
- // eslint-disable-next-line sonarjs/cognitive-complexity
74
- function createResponseAssertions(
75
- fixtureCallInformation: FixtureCallInformation,
76
- sourceCode: SourceCode,
77
- responseVariableName: string,
78
- ) {
79
- let statusAssertion: string | undefined;
80
- const nonStatusAssertions: string[] = [];
81
- for (const expectArguments of fixtureCallInformation.assertions ?? []) {
82
- if (expectArguments.length === 1) {
83
- const [assertionArgument] = expectArguments;
84
- assert.ok(assertionArgument);
85
- if (
86
- (assertionArgument.type === AST_NODE_TYPES.MemberExpression &&
87
- assertionArgument.object.type === AST_NODE_TYPES.Identifier &&
88
- assertionArgument.object.name === 'StatusCodes') ||
89
- assertionArgument.type === AST_NODE_TYPES.Literal ||
90
- sourceCode.getText(assertionArgument).includes('StatusCodes.')
91
- ) {
92
- // status code assertion
93
- statusAssertion = `assert.equal(${responseVariableName}.status, ${sourceCode.getText(assertionArgument)})`;
94
- } else if (assertionArgument.type === AST_NODE_TYPES.ArrowFunctionExpression) {
95
- // callback assertion using arrow function
96
- let functionBody = sourceCode.getText(assertionArgument.body);
97
-
98
- const [originalResponseArgument] = assertionArgument.params;
99
- assert.ok(originalResponseArgument?.type === AST_NODE_TYPES.Identifier);
100
- const originalResponseArgumentName = originalResponseArgument.name;
101
- if (originalResponseArgumentName !== responseVariableName) {
102
- functionBody = functionBody.replace(
103
- new RegExp(`\\b${originalResponseArgumentName}\\b`, 'ug'),
104
- responseVariableName,
105
- );
106
- }
107
- nonStatusAssertions.push(`assert.doesNotThrow(()=>${functionBody})`);
108
- } else if (assertionArgument.type === AST_NODE_TYPES.Identifier) {
109
- // callback assertion using function reference
110
- nonStatusAssertions.push(
111
- `assert.doesNotThrow(()=>${sourceCode.getText(assertionArgument)}(${responseVariableName}))`,
112
- );
113
- } else if (
114
- assertionArgument.type === AST_NODE_TYPES.ObjectExpression ||
115
- assertionArgument.type === AST_NODE_TYPES.CallExpression
116
- ) {
117
- // body deep equal assertion
118
- nonStatusAssertions.push(
119
- `assert.deepEqual(await ${responseVariableName}.json(), ${sourceCode.getText(assertionArgument)})`,
120
- );
121
- } else {
122
- throw new Error(`Unexpected Supertest assertion argument: ".expect(${sourceCode.getText(assertionArgument)})`);
123
- }
124
- } else if (expectArguments.length === 2) {
125
- // header assertion
126
- const [headerName, headerValue] = expectArguments;
127
- assert.ok(headerName && headerValue);
128
- const headersReference = `${responseVariableName}.headers`;
129
- if (headerValue.type === AST_NODE_TYPES.Literal && headerValue.value instanceof RegExp) {
130
- nonStatusAssertions.push(
131
- `assert.ok(${headersReference}.get(${sourceCode.getText(headerName)}).match(${sourceCode.getText(headerValue)}))`,
132
- );
133
- } else {
134
- nonStatusAssertions.push(
135
- `assert.equal(${headersReference}.get(${sourceCode.getText(headerName)}), ${sourceCode.getText(headerValue)})`,
136
- );
137
- }
138
- }
139
- }
140
- return {
141
- statusAssertion,
142
- nonStatusAssertions,
143
- };
144
- }
145
-
146
- function getResponseHeadersAccesses(responseVariables: Variable[], scopeManager: ScopeManager, sourceCode: SourceCode) {
147
- const responseHeadersAccesses: TSESTree.MemberExpression[] = [];
148
- for (const responseVariable of responseVariables) {
149
- for (const responseReference of responseVariable.references) {
150
- const responseAccess = getParent(responseReference.identifier);
151
- if (!responseAccess || responseAccess.type !== AST_NODE_TYPES.MemberExpression) {
152
- continue;
153
- }
154
-
155
- const responseAccessParent = getParent(responseAccess);
156
- if (!responseAccessParent) {
157
- continue;
158
- }
159
-
160
- if (
161
- responseAccessParent.type === AST_NODE_TYPES.CallExpression &&
162
- responseAccessParent.arguments[0]?.type === AST_NODE_TYPES.ArrowFunctionExpression
163
- ) {
164
- // map-like operation against responses, e.g. responses.map((response) => response.headers.etag)
165
- responseHeadersAccesses.push(
166
- ...getResponseHeadersAccesses(
167
- scopeManager.getDeclaredVariables(responseAccessParent.arguments[0]),
168
- scopeManager,
169
- sourceCode,
170
- ),
171
- );
172
- continue;
173
- }
174
-
175
- if (
176
- responseAccess.computed &&
177
- responseAccess.property.type === AST_NODE_TYPES.Literal &&
178
- responseAccessParent.type === AST_NODE_TYPES.MemberExpression
179
- ) {
180
- // header access through indexed responses array, e.g. responses[0].headers, responses[1].get(...), etc.
181
- responseHeadersAccesses.push(responseAccessParent);
182
- } else {
183
- responseHeadersAccesses.push(responseAccess);
184
- }
185
- }
186
- }
187
- return responseHeadersAccesses;
188
- }
189
-
190
- const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
191
- const rule: ESLintUtils.RuleModule<'unknownError' | 'preferNativeFetch' | 'shouldUseHeaderGetter'> = createRule({
192
- name: ruleId,
193
- meta: {
194
- type: 'suggestion',
195
- docs: {
196
- description: 'Prefer native fetch API over customized fixture API.',
197
- url: getDocumentationUrl(ruleId),
198
- },
199
- messages: {
200
- preferNativeFetch: 'Prefer native fetch API over customized fixture API.',
201
- shouldUseHeaderGetter: 'Getter should be used to access response headers.',
202
- unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}.',
203
- },
204
- fixable: 'code',
205
- schema: [],
206
- },
207
- defaultOptions: [],
208
- create(context) {
209
- const sourceCode = context.sourceCode;
210
- const scopeManager = sourceCode.scopeManager;
211
- assert.ok(scopeManager);
212
-
213
- return {
214
- 'CallExpression[callee.object.object.name="fixture"][callee.object.property.name="api"]': (
215
- fixtureCall: TSESTree.CallExpression,
216
- // eslint-disable-next-line sonarjs/cognitive-complexity
217
- ) => {
218
- try {
219
- if (!hasAssertions(fixtureCall)) {
220
- // skip if there are no assertions, let "no-fixture" rule to handle the conversion
221
- return;
222
- }
223
-
224
- if (!(isUsedInArrayOrAsArgument(fixtureCall) || getEnclosingFunction(fixtureCall)?.async === false)) {
225
- return;
226
- }
227
-
228
- const fixtureFunction = fixtureCall.callee; // e.g. fixture.api.get
229
- assert.ok(fixtureFunction.type === AST_NODE_TYPES.MemberExpression);
230
- const indentation = getIndentation(fixtureCall, sourceCode);
231
-
232
- const [urlArgumentNode] = fixtureCall.arguments; // e.g. `/sample-service/v1/ping`
233
- assert.ok(urlArgumentNode !== undefined);
234
-
235
- const fixtureCallInformation = {} as FixtureCallInformation;
236
- analyzeFixtureCall(fixtureCall, fixtureCallInformation, sourceCode);
237
-
238
- // convert url from `/sample-service/v1/ping` to `${BASE_PATH}/ping`
239
- const originalUrlArgumentText = sourceCode.getText(urlArgumentNode);
240
- const fetchUrlArgumentText = replaceEndpointUrlPrefixWithBasePath(originalUrlArgumentText);
241
-
242
- // fetch request argument
243
- const methodNode = fixtureFunction.property; // get/put/etc.
244
- assert.ok(methodNode.type === AST_NODE_TYPES.Identifier);
245
- const fetchRequestArgumentLines = [
246
- '{',
247
- ` method: '${methodNode.name.toUpperCase()}',`,
248
- ...(fixtureCallInformation.requestBody
249
- ? [` body: JSON.stringify(${sourceCode.getText(fixtureCallInformation.requestBody)}),`]
250
- : []),
251
- ...(fixtureCallInformation.requestHeaders
252
- ? [
253
- ` headers: {`,
254
- ...fixtureCallInformation.requestHeaders.map(
255
- ({ name, value }) =>
256
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions, no-nested-ternary, sonarjs/no-nested-template-literals
257
- ` ${name.type === AST_NODE_TYPES.Literal ? (isValidPropertyName(name.value) ? name.value : `'${name.value}'`) : `[${sourceCode.getText(name)}]`}: ${sourceCode.getText(value)},`,
258
- ),
259
- ` },`,
260
- ]
261
- : []),
262
- '}',
263
- ].join(`\n${indentation}`);
264
-
265
- const responseVariableNameToUse = 'res';
266
- const { statusAssertion, nonStatusAssertions } = createResponseAssertions(
267
- fixtureCallInformation,
268
- sourceCode,
269
- responseVariableNameToUse,
270
- );
271
-
272
- // add variable declaration if needed
273
- const disableLintComment = '// eslint-disable-next-line @checkdigit/no-promise-instance-method';
274
- const fetchCallText = `fetch(${fetchUrlArgumentText}, ${fetchRequestArgumentLines})`;
275
- const appendingAssignmentAndAssertionText = [
276
- ...(statusAssertion !== undefined ? [statusAssertion] : []),
277
- ...nonStatusAssertions,
278
- ].join(`;\n${indentation}`);
279
- const replacementText = fixtureCallInformation.assertions
280
- ? [
281
- disableLintComment,
282
- `${fetchCallText}.then((${responseVariableNameToUse}) => {`,
283
- appendingAssignmentAndAssertionText === '' ? '' : ` ${appendingAssignmentAndAssertionText};`,
284
- ` return ${responseVariableNameToUse};`,
285
- `})`,
286
- ].join(`\n${indentation}`)
287
- : fetchCallText;
288
-
289
- context.report({
290
- node: fixtureCall,
291
- messageId: 'preferNativeFetch',
292
- fix(fixer) {
293
- return fixer.replaceText(fixtureCallInformation.fixtureNode, replacementText);
294
- },
295
- });
296
-
297
- const responsesVariable = getEnclosingStatement(fixtureCallInformation.fixtureNode);
298
- if (!responsesVariable) {
299
- return;
300
- }
301
-
302
- const responseVariableReferences = scopeManager.getDeclaredVariables(responsesVariable);
303
- const responseHeadersAccesses = getResponseHeadersAccesses(
304
- responseVariableReferences,
305
- scopeManager,
306
- sourceCode,
307
- );
308
- for (const responseHeadersAccess of responseHeadersAccesses) {
309
- if (isInvalidResponseHeadersAccess(responseHeadersAccess)) {
310
- const headerAccess = getParent(responseHeadersAccess);
311
- if (headerAccess?.type === AST_NODE_TYPES.MemberExpression) {
312
- const headerNameNode = headerAccess.property;
313
- const headerName = headerAccess.computed
314
- ? sourceCode.getText(headerNameNode)
315
- : `'${sourceCode.getText(headerNameNode)}'`;
316
- const headerAccessReplacementText = `${sourceCode.getText(headerAccess.object)}.get(${headerName})`;
317
-
318
- context.report({
319
- node: headerAccess,
320
- messageId: 'shouldUseHeaderGetter',
321
- fix(fixer) {
322
- return fixer.replaceText(headerAccess, headerAccessReplacementText);
323
- },
324
- });
325
- } else if (
326
- headerAccess?.type === AST_NODE_TYPES.CallExpression &&
327
- responseHeadersAccess.property.type === AST_NODE_TYPES.Identifier &&
328
- responseHeadersAccess.property.name === 'get'
329
- ) {
330
- const headerAccessReplacementText = `${sourceCode.getText(responseHeadersAccess.object)}.headers.get(${sourceCode.getText(headerAccess.arguments[0])})`;
331
-
332
- context.report({
333
- node: headerAccess,
334
- messageId: 'shouldUseHeaderGetter',
335
- fix(fixer) {
336
- return fixer.replaceText(headerAccess, headerAccessReplacementText);
337
- },
338
- });
339
- }
340
- }
341
- }
342
- } catch (error) {
343
- // eslint-disable-next-line no-console
344
- console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
345
- context.report({
346
- node: fixtureCall,
347
- messageId: 'unknownError',
348
- data: {
349
- fileName: context.filename,
350
- error: error instanceof Error ? error.toString() : JSON.stringify(error),
351
- },
352
- });
353
- }
354
- },
355
- };
356
- },
357
- });
358
-
359
- export default rule;
@@ -1,69 +0,0 @@
1
- // agent/fetch.ts
2
-
3
- import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
4
- import ts from 'typescript';
5
-
6
- import { getParent, isBlockStatement } from '../library/ts-tree';
7
-
8
- export function getResponseBodyRetrievalText(responseVariableName: string) {
9
- return `await ${responseVariableName}.json()`;
10
- }
11
-
12
- export function getResponseStatusRetrievalText(responseVariableName: string) {
13
- return `${responseVariableName}.status`;
14
- }
15
-
16
- export function getResponseHeadersRetrievalText(responseVariableName: string) {
17
- return `${responseVariableName}.headers`;
18
- }
19
-
20
- export function isInvalidResponseHeadersAccess(responseHeadersAccess: TSESTree.Node): boolean {
21
- const responseHeaderAccessParent = getParent(responseHeadersAccess);
22
- if (responseHeaderAccessParent?.type === AST_NODE_TYPES.VariableDeclarator) {
23
- return false;
24
- }
25
-
26
- if (
27
- responseHeaderAccessParent?.type === AST_NODE_TYPES.CallExpression &&
28
- responseHeaderAccessParent.callee.type === AST_NODE_TYPES.MemberExpression &&
29
- responseHeaderAccessParent.callee.property.type === AST_NODE_TYPES.Identifier &&
30
- responseHeaderAccessParent.callee.property.name === 'get'
31
- ) {
32
- return true;
33
- }
34
-
35
- return !(
36
- responseHeaderAccessParent?.type === AST_NODE_TYPES.MemberExpression &&
37
- responseHeaderAccessParent.property.type === AST_NODE_TYPES.Identifier &&
38
- responseHeaderAccessParent.property.name === 'get'
39
- );
40
- }
41
-
42
- export function hasAssertions(fixtureCall: TSESTree.Node): boolean {
43
- if (isBlockStatement(fixtureCall)) {
44
- return false;
45
- }
46
-
47
- const parent = getParent(fixtureCall);
48
- if (!parent) {
49
- return false;
50
- }
51
-
52
- if (
53
- parent.type === AST_NODE_TYPES.MemberExpression &&
54
- parent.property.type === AST_NODE_TYPES.Identifier &&
55
- parent.property.name === 'expect' &&
56
- getParent(parent)?.type === AST_NODE_TYPES.CallExpression
57
- ) {
58
- return true;
59
- }
60
-
61
- return hasAssertions(parent);
62
- }
63
-
64
- export function isFetchResponse(type: ts.Type): boolean {
65
- return (
66
- type.getProperties().some((symbol) => symbol.name === 'body') &&
67
- type.getProperties().some((symbol) => symbol.name === 'json')
68
- );
69
- }
package/src/agent/file.ts DELETED
@@ -1,42 +0,0 @@
1
- // agent/file.ts
2
-
3
- import fs from 'node:fs';
4
- import path from 'node:path';
5
-
6
- export function isApiIndexFile(filename: string): boolean {
7
- return /.*\/src\/api\/v\d+\/index.ts/u.test(filename);
8
- }
9
-
10
- export function getProjectRootFolder(indexFilename: string): string {
11
- return indexFilename.substring(0, indexFilename.lastIndexOf('/src/'));
12
- }
13
-
14
- export function getSwaggerPathByIndexFile(indexFilename: string): string {
15
- return indexFilename.replace(/index\.ts$/u, 'swagger.yml');
16
- }
17
-
18
- export function loadSwagger(filename: string): string {
19
- return fs.readFileSync(filename, 'utf8');
20
- }
21
-
22
- export function loadPackageJson(projectRoot: string): string {
23
- return fs.readFileSync(`${projectRoot}/package.json`, 'utf8');
24
- }
25
-
26
- export function getApiFolder(folder: string): string | undefined {
27
- if (/^\/?(?<absolutePath>(?:[^/]+\/)*)src\/api\/v\d+$/u.test(folder)) {
28
- return folder;
29
- }
30
- const upperFolder = folder.substring(0, folder.lastIndexOf('/'));
31
- return upperFolder.trim() === '' ? undefined : getApiFolder(upperFolder);
32
- }
33
-
34
- export function getApiIndexPathByFilename(filename: string): string | undefined {
35
- const apiFolder = getApiFolder(filename);
36
- if (apiFolder === undefined) {
37
- return undefined;
38
- }
39
-
40
- const relativePath = path.relative(path.dirname(filename), `${apiFolder}/index`);
41
- return relativePath.startsWith('../') ? relativePath : `./${relativePath}`;
42
- }
@@ -1,200 +0,0 @@
1
- // agent/fix-function-call-arguments.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
- import { ESLintUtils, TSESTree } from '@typescript-eslint/utils';
11
- import debug from 'debug';
12
- import getDocumentationUrl from '../get-documentation-url';
13
-
14
- export const ruleId = 'fix-function-call-arguments';
15
-
16
- export interface FixFunctionCallArgumentsRuleOptions {
17
- typesToCheck: string[];
18
- }
19
- const DEFAULT_OPTIONS = {
20
- typesToCheck: [
21
- 'Configuration<ResolvedServices>',
22
- 'Fixture<ResolvedServices>',
23
- 'InboundContext',
24
- '{ get: () => string; }',
25
- 'Api',
26
- ],
27
- };
28
-
29
- const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
30
- const log = debug('eslint-plugin:fix-function-call-arguments');
31
-
32
- const rule: ESLintUtils.RuleModule<
33
- 'removeIncompatibleFunctionArguments' | 'unknownError',
34
- [FixFunctionCallArgumentsRuleOptions]
35
- > = createRule({
36
- name: ruleId,
37
- meta: {
38
- type: 'suggestion',
39
- docs: {
40
- description: 'Remove incompatible function arguments.',
41
- },
42
- messages: {
43
- removeIncompatibleFunctionArguments: 'Removing incompatible function arguments.',
44
- unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}.',
45
- },
46
- fixable: 'code',
47
- schema: [
48
- {
49
- type: 'object',
50
- properties: {
51
- typesToCheck: {
52
- description: 'Text representation of the types of which the function call parameters will be examine',
53
- type: 'array',
54
- items: {
55
- type: 'string',
56
- },
57
- },
58
- },
59
- additionalProperties: false,
60
- },
61
- ],
62
- },
63
- defaultOptions: [DEFAULT_OPTIONS],
64
- create(context) {
65
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
66
- const { typesToCheck } = context.options[0] ?? DEFAULT_OPTIONS;
67
- const parserServices = ESLintUtils.getParserServices(context);
68
- const typeChecker = parserServices.program.getTypeChecker();
69
- const sourceCode = context.sourceCode;
70
-
71
- return {
72
- CallExpression(callExpression) {
73
- // ignore calls like `foo.bar()` which are likely to be 3rd party module calls
74
- // we only focus on calls against local functions or functions imported from the same module
75
- // if (callExpression.callee.type === TSESTree.AST_NODE_TYPES.MemberExpression) {
76
- // return;
77
- // }
78
-
79
- log('===== file name:', context.filename);
80
- log('callExpression:', sourceCode.getText(callExpression));
81
-
82
- try {
83
- const actualParameters = callExpression.arguments;
84
- if (
85
- !actualParameters.some((actualParameter) => {
86
- const actualType = typeChecker.getTypeAtLocation(
87
- parserServices.esTreeNodeToTSNodeMap.get(actualParameter),
88
- );
89
- const actualTypeString = typeChecker.typeToString(actualType);
90
- return typesToCheck.includes(actualTypeString) || actualTypeString.endsWith('RequestType');
91
- })
92
- ) {
93
- return;
94
- }
95
-
96
- const calleeTsNode = parserServices.esTreeNodeToTSNodeMap.get(callExpression.callee);
97
- const calleeType = typeChecker.getTypeAtLocation(calleeTsNode);
98
-
99
- const signatures = calleeType.getCallSignatures();
100
- if (signatures.length > 1) {
101
- // ignore complex signatures with overloads
102
- return;
103
- }
104
-
105
- const signature = signatures[0];
106
- assert(signature);
107
- // if (
108
- // signature === undefined ||
109
- // (signature.typeParameters !== undefined && signature.typeParameters.length > 0)
110
- // ) {
111
- // // ignore complex signatures with type parameters
112
- // return;
113
- // }
114
-
115
- log('signature:', signature.getDeclaration().getText());
116
- const expectedParameters = signature.getParameters();
117
- log(
118
- 'expected parameters:',
119
- expectedParameters.map((expectedParameter) =>
120
- typeChecker.typeToString(typeChecker.getTypeOfSymbol(expectedParameter)),
121
- ),
122
- );
123
- const expectedParametersCount = expectedParameters.length;
124
- const actualParametersCount = actualParameters.length;
125
- if (actualParametersCount === 0) {
126
- return;
127
- }
128
-
129
- const parametersToKeep: TSESTree.CallExpressionArgument[] = [];
130
- let expectedParameterIndex = 0;
131
- for (const [actualParameterIndex, actualParameter] of actualParameters.entries()) {
132
- if (expectedParameterIndex >= expectedParametersCount) {
133
- break;
134
- }
135
-
136
- const expectedParameter = expectedParameters[expectedParameterIndex];
137
- assert.ok(expectedParameter, 'Expected parameter not found.');
138
-
139
- const expectedType = typeChecker.getTypeOfSymbol(expectedParameter);
140
- const actualType = typeChecker.getTypeAtLocation(parserServices.esTreeNodeToTSNodeMap.get(actualParameter));
141
- const actualTypeString = typeChecker.typeToString(actualType);
142
- log(
143
- 'expected type: #',
144
- expectedParameterIndex,
145
- expectedParameter.escapedName,
146
- typeChecker.typeToString(expectedType),
147
- );
148
- log('actual type: #', actualParameterIndex, sourceCode.getText(actualParameter), actualTypeString);
149
-
150
- if (
151
- (typesToCheck.includes(actualTypeString) || actualTypeString.endsWith('RequestType')) &&
152
- !typeChecker.isTypeAssignableTo(actualType, expectedType)
153
- ) {
154
- log('removing un-matched parameter', sourceCode.getText(actualParameter));
155
- continue;
156
- }
157
- parametersToKeep.push(actualParameter);
158
- expectedParameterIndex++;
159
- }
160
-
161
- if (parametersToKeep.length === actualParametersCount) {
162
- return;
163
- }
164
-
165
- const firstParameter = actualParameters[0];
166
- const lastParameter = actualParameters.at(-1);
167
- assert.ok(firstParameter !== undefined && lastParameter !== undefined);
168
- const tokenAfterParameters = sourceCode.getTokenAfter(lastParameter);
169
-
170
- context.report({
171
- node: callExpression,
172
- messageId: 'removeIncompatibleFunctionArguments',
173
- fix(fixer) {
174
- return fixer.replaceTextRange(
175
- [
176
- firstParameter.range[0],
177
- tokenAfterParameters?.value === ',' ? tokenAfterParameters.range[1] : lastParameter.range[1],
178
- ],
179
- parametersToKeep.map((arg) => sourceCode.getText(arg)).join(', '),
180
- );
181
- },
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: callExpression,
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;