@checkdigit/eslint-plugin 7.2.0 → 7.3.0-PR.75-aa6d

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