@checkdigit/eslint-plugin 7.5.0 → 7.6.0-PR.75-1b09

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 (69) hide show
  1. package/dist-mjs/agent/add-assert-import.mjs +58 -0
  2. package/dist-mjs/agent/add-base-path-const.mjs +65 -0
  3. package/dist-mjs/agent/add-base-path-import.mjs +60 -0
  4. package/dist-mjs/agent/add-url-domain.mjs +61 -0
  5. package/dist-mjs/agent/agent-test-wiring.mjs +221 -0
  6. package/dist-mjs/agent/fetch-response-body-json.mjs +146 -0
  7. package/dist-mjs/agent/fetch-response-header-getter.mjs +117 -0
  8. package/dist-mjs/agent/fetch-response-status.mjs +66 -0
  9. package/dist-mjs/agent/fetch-then.mjs +269 -0
  10. package/dist-mjs/agent/fetch.mjs +38 -0
  11. package/dist-mjs/agent/file.mjs +43 -0
  12. package/dist-mjs/agent/fix-function-call-arguments.mjs +153 -0
  13. package/dist-mjs/agent/no-fixture.mjs +361 -0
  14. package/dist-mjs/agent/no-mapped-response.mjs +75 -0
  15. package/dist-mjs/agent/no-service-wrapper.mjs +185 -0
  16. package/dist-mjs/agent/no-status-code.mjs +59 -0
  17. package/dist-mjs/agent/no-supertest.mjs +343 -0
  18. package/dist-mjs/agent/no-unused-function-argument.mjs +79 -0
  19. package/dist-mjs/agent/no-unused-imports.mjs +81 -0
  20. package/dist-mjs/agent/no-unused-service-variable.mjs +74 -0
  21. package/dist-mjs/agent/response-reference.mjs +70 -0
  22. package/dist-mjs/agent/url.mjs +32 -0
  23. package/dist-mjs/index.mjs +151 -4
  24. package/dist-types/agent/add-assert-import.d.ts +4 -0
  25. package/dist-types/agent/add-base-path-const.d.ts +4 -0
  26. package/dist-types/agent/add-base-path-import.d.ts +4 -0
  27. package/dist-types/agent/add-url-domain.d.ts +4 -0
  28. package/dist-types/agent/agent-test-wiring.d.ts +4 -0
  29. package/dist-types/agent/fetch-response-body-json.d.ts +4 -0
  30. package/dist-types/agent/fetch-response-header-getter.d.ts +4 -0
  31. package/dist-types/agent/fetch-response-status.d.ts +4 -0
  32. package/dist-types/agent/fetch-then.d.ts +4 -0
  33. package/dist-types/agent/fetch.d.ts +5 -0
  34. package/dist-types/agent/file.d.ts +7 -0
  35. package/dist-types/agent/fix-function-call-arguments.d.ts +9 -0
  36. package/dist-types/agent/no-fixture.d.ts +4 -0
  37. package/dist-types/agent/no-mapped-response.d.ts +4 -0
  38. package/dist-types/agent/no-service-wrapper.d.ts +4 -0
  39. package/dist-types/agent/no-status-code.d.ts +4 -0
  40. package/dist-types/agent/no-supertest.d.ts +4 -0
  41. package/dist-types/agent/no-unused-function-argument.d.ts +4 -0
  42. package/dist-types/agent/no-unused-imports.d.ts +4 -0
  43. package/dist-types/agent/no-unused-service-variable.d.ts +4 -0
  44. package/dist-types/agent/response-reference.d.ts +16 -0
  45. package/dist-types/agent/url.d.ts +4 -0
  46. package/package.json +1 -96
  47. package/src/agent/add-assert-import.ts +74 -0
  48. package/src/agent/add-base-path-const.ts +81 -0
  49. package/src/agent/add-base-path-import.ts +69 -0
  50. package/src/agent/add-url-domain.ts +76 -0
  51. package/src/agent/agent-test-wiring.ts +273 -0
  52. package/src/agent/fetch-response-body-json.ts +197 -0
  53. package/src/agent/fetch-response-header-getter.ts +148 -0
  54. package/src/agent/fetch-response-status.ts +87 -0
  55. package/src/agent/fetch-then.ts +357 -0
  56. package/src/agent/fetch.ts +57 -0
  57. package/src/agent/file.ts +42 -0
  58. package/src/agent/fix-function-call-arguments.ts +200 -0
  59. package/src/agent/no-fixture.ts +521 -0
  60. package/src/agent/no-mapped-response.ts +84 -0
  61. package/src/agent/no-service-wrapper.ts +241 -0
  62. package/src/agent/no-status-code.ts +72 -0
  63. package/src/agent/no-supertest.ts +497 -0
  64. package/src/agent/no-unused-function-argument.ts +98 -0
  65. package/src/agent/no-unused-imports.ts +103 -0
  66. package/src/agent/no-unused-service-variable.ts +93 -0
  67. package/src/agent/response-reference.ts +129 -0
  68. package/src/agent/url.ts +32 -0
  69. package/src/index.ts +147 -0
@@ -0,0 +1,273 @@
1
+ // agent/agent-test-wiring.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 { AST_TOKEN_TYPES, ESLintUtils, TSESTree } from '@typescript-eslint/utils';
12
+ import type { RuleFix, RuleFixer } from '@typescript-eslint/utils/ts-eslint';
13
+ import debug from 'debug';
14
+
15
+ import getDocumentationUrl from '../get-documentation-url';
16
+
17
+ export const ruleId = 'agent-test-wiring';
18
+ const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
19
+ const log = debug('eslint-plugin:agent:agent-test-wiring');
20
+
21
+ const STATEMENT_FIXTURE_RESET = 'fixture.reset()';
22
+ const STATEMENT_FIXTURE_RESET_AWAITED = `await ${STATEMENT_FIXTURE_RESET};`;
23
+ const STATEMENT_AGENT_DECLARATION = 'let agent: Agent;';
24
+ const STATEMENT_AGENT_CREATION = 'agent = await createAgent();';
25
+ const STATEMENT_AGENT_REGISTER = 'agent.register(await fixturePlugin(fixture));';
26
+ const STATEMENT_AGENT_ENABLE = 'agent.enable();';
27
+ const STATEMENT_AGENT_DISPOSE = 'await agent[Symbol.asyncDispose]();';
28
+
29
+ const rule: ESLintUtils.RuleModule<'updateTestWiring' | 'unknownError'> = createRule({
30
+ name: ruleId,
31
+ meta: {
32
+ type: 'suggestion',
33
+ docs: {
34
+ description: 'Update test wiring.',
35
+ },
36
+ messages: {
37
+ updateTestWiring: 'Updating test wiring.',
38
+ unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}.',
39
+ },
40
+ fixable: 'code',
41
+ schema: [],
42
+ },
43
+ defaultOptions: [],
44
+ // eslint-disable-next-line max-lines-per-function
45
+ create(context) {
46
+ log('Processing file:', context.filename);
47
+ const sourceCode = context.sourceCode;
48
+ const importDeclarations = new Map<string, TSESTree.ImportDeclaration>();
49
+ let isFixtureUsed = false;
50
+ let beforeAll: TSESTree.CallExpression | undefined;
51
+ let beforeEach: TSESTree.CallExpression | undefined;
52
+ let afterAll: TSESTree.CallExpression | undefined;
53
+
54
+ return {
55
+ ImportDeclaration(importDeclaration) {
56
+ const moduleName = importDeclaration.source.value;
57
+ importDeclarations.set(moduleName, importDeclaration);
58
+ if (
59
+ moduleName === '@checkdigit/fixture' &&
60
+ importDeclaration.specifiers.some(
61
+ (specifier) =>
62
+ specifier.type === TSESTree.AST_NODE_TYPES.ImportSpecifier &&
63
+ specifier.imported.type === TSESTree.AST_NODE_TYPES.Identifier &&
64
+ specifier.imported.name === 'createFixture',
65
+ )
66
+ ) {
67
+ isFixtureUsed = true;
68
+ }
69
+ },
70
+ 'CallExpression[callee.name="beforeAll"]': (callExpression: TSESTree.CallExpression) => {
71
+ beforeAll = callExpression;
72
+ },
73
+ 'CallExpression[callee.name="beforeEach"]': (callExpression: TSESTree.CallExpression) => {
74
+ beforeEach = callExpression;
75
+ },
76
+ 'CallExpression[callee.name="afterAll"]': (callExpression: TSESTree.CallExpression) => {
77
+ afterAll = callExpression;
78
+ },
79
+ // eslint-disable-next-line sonarjs/cognitive-complexity
80
+ 'Program:exit'(program) {
81
+ if (!isFixtureUsed || (beforeAll === undefined && beforeEach === undefined)) {
82
+ // only update test wiring if fixture is used
83
+ return;
84
+ }
85
+
86
+ try {
87
+ let jestImportFixer: ((fixer: RuleFixer) => RuleFix) | undefined;
88
+ let agentImportFixer: ((fixer: RuleFixer) => RuleFix) | undefined;
89
+ let fixturePluginImportFixer: ((fixer: RuleFixer) => RuleFix) | undefined;
90
+ let agentDeclarationFixer: ((fixer: RuleFixer) => RuleFix) | undefined;
91
+ let beforeAllOrEachFixer: ((fixer: RuleFixer) => RuleFix) | undefined;
92
+ let afterAllFixer: ((fixer: RuleFixer) => RuleFix) | undefined;
93
+
94
+ const lastImportDeclaration = [...importDeclarations.values()].at(-1);
95
+ assert.ok(lastImportDeclaration);
96
+
97
+ // make sure that afterAll is imported from jest
98
+ const jestImportDeclaration = importDeclarations.get('@jest/globals');
99
+ assert.ok(jestImportDeclaration);
100
+ const importsToAdd = ['afterAll', 'beforeAll'].filter(
101
+ (jestHook) =>
102
+ !jestImportDeclaration.specifiers.some(
103
+ (specifier) =>
104
+ specifier.type === TSESTree.AST_NODE_TYPES.ImportSpecifier &&
105
+ specifier.imported.type === TSESTree.AST_NODE_TYPES.Identifier &&
106
+ specifier.imported.name === jestHook,
107
+ ),
108
+ );
109
+ if (importsToAdd.length > 0) {
110
+ const firstImportSpecifier = jestImportDeclaration.specifiers[0];
111
+ assert.ok(firstImportSpecifier);
112
+ jestImportFixer = (fixer: RuleFixer) =>
113
+ fixer.insertTextBefore(firstImportSpecifier, `${importsToAdd.join(', ')}, `);
114
+ }
115
+
116
+ // make sure that agent is imported
117
+ const agentImportDeclaration = importDeclarations.get('@checkdigit/agent');
118
+ if (!agentImportDeclaration) {
119
+ agentImportFixer = (fixer: RuleFixer) =>
120
+ fixer.insertTextAfter(
121
+ lastImportDeclaration,
122
+ `\nimport createAgent, { type Agent } from '@checkdigit/agent';`,
123
+ );
124
+ }
125
+
126
+ // make sure that fixture plugin is imported
127
+ const pathLets = context.filename.split('/');
128
+ const currentFileIndex = pathLets.length - 1;
129
+ const pluginFolderIndex = pathLets.lastIndexOf('src') + 1;
130
+ // it should be safe to assume that the test code is always at least one level deeper than the plugin folder
131
+ const fixturePluginImportPath = `${'../'.repeat(currentFileIndex - pluginFolderIndex)}plugin/fixture.test`;
132
+ if (!importDeclarations.get(fixturePluginImportPath)) {
133
+ fixturePluginImportFixer = (fixer: RuleFixer) =>
134
+ fixer.insertTextAfter(lastImportDeclaration, `\nimport fixturePlugin from '${fixturePluginImportPath}';`);
135
+ }
136
+
137
+ // inject agent declaration and initialization
138
+ if (beforeAll === undefined) {
139
+ // create `beforeAll` block if it doesn't exist
140
+ beforeAllOrEachFixer = (fixer: RuleFixer) =>
141
+ fixer.insertTextBefore(
142
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
143
+ beforeEach!,
144
+ [
145
+ STATEMENT_AGENT_DECLARATION,
146
+ `beforeAll(async () => {`,
147
+ [STATEMENT_AGENT_CREATION, STATEMENT_AGENT_REGISTER, STATEMENT_AGENT_ENABLE].join('\n'),
148
+ `});\n`,
149
+ ].join('\n'),
150
+ );
151
+ } else {
152
+ const beforeAllArgument = beforeAll.arguments[0];
153
+ assert.ok(beforeAllArgument !== undefined);
154
+ if (!sourceCode.getText(beforeAllArgument).includes(STATEMENT_AGENT_CREATION)) {
155
+ if (
156
+ beforeAllArgument.type === TSESTree.AST_NODE_TYPES.ArrowFunctionExpression &&
157
+ beforeAllArgument.body.type === TSESTree.AST_NODE_TYPES.BlockStatement
158
+ ) {
159
+ const fixtureResetStatement = beforeAllArgument.body.body.find(
160
+ (statement) => sourceCode.getText(statement) === STATEMENT_FIXTURE_RESET_AWAITED,
161
+ );
162
+ assert.ok(fixtureResetStatement !== undefined);
163
+ beforeAllOrEachFixer = (fixer: RuleFixer) =>
164
+ fixer.replaceText(
165
+ fixtureResetStatement,
166
+ [
167
+ STATEMENT_AGENT_CREATION,
168
+ STATEMENT_AGENT_REGISTER,
169
+ STATEMENT_AGENT_ENABLE,
170
+ STATEMENT_FIXTURE_RESET_AWAITED,
171
+ ].join('\n'),
172
+ );
173
+ } else {
174
+ beforeAllOrEachFixer = (fixer: RuleFixer) =>
175
+ fixer.replaceText(
176
+ beforeAllArgument,
177
+ [
178
+ `async () => {`,
179
+ STATEMENT_AGENT_CREATION,
180
+ STATEMENT_AGENT_REGISTER,
181
+ STATEMENT_AGENT_ENABLE,
182
+ STATEMENT_FIXTURE_RESET_AWAITED,
183
+ `}`,
184
+ ].join('\n'),
185
+ );
186
+ }
187
+ agentDeclarationFixer = (fixer: RuleFixer) =>
188
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
189
+ fixer.insertTextBefore(beforeAll!, `${STATEMENT_AGENT_DECLARATION}\n`);
190
+ }
191
+ }
192
+
193
+ // inject agent disposal to `afterAll` block
194
+ if (afterAll !== undefined) {
195
+ const afterAllArrowFunctionExpression = afterAll.arguments[0];
196
+ assert.ok(
197
+ afterAllArrowFunctionExpression !== undefined &&
198
+ afterAllArrowFunctionExpression.type === TSESTree.AST_NODE_TYPES.ArrowFunctionExpression,
199
+ );
200
+ const arrowFunctionBody = afterAllArrowFunctionExpression.body;
201
+ assert.ok(arrowFunctionBody.type === TSESTree.AST_NODE_TYPES.BlockStatement);
202
+
203
+ const afterAllBodyText = sourceCode.getText(arrowFunctionBody);
204
+ if (!afterAllBodyText.includes(STATEMENT_AGENT_DISPOSE)) {
205
+ const lastStatement = arrowFunctionBody.body.at(-1);
206
+ assert.ok(lastStatement);
207
+ afterAllFixer = (fixer: RuleFixer) => fixer.insertTextAfter(lastStatement, STATEMENT_AGENT_DISPOSE);
208
+ }
209
+ } else {
210
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
211
+ const nextToken = sourceCode.getTokenAfter(beforeAll ?? beforeEach!);
212
+ afterAllFixer = (fixer: RuleFixer) =>
213
+ fixer.insertTextAfter(
214
+ nextToken !== null && nextToken.type === AST_TOKEN_TYPES.Punctuator
215
+ ? nextToken
216
+ : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
217
+ beforeAll!,
218
+ ['', `afterAll(async () => {`, STATEMENT_AGENT_DISPOSE, `});`].join('\n'),
219
+ );
220
+ }
221
+
222
+ if (
223
+ jestImportFixer !== undefined ||
224
+ agentImportFixer !== undefined ||
225
+ fixturePluginImportFixer !== undefined ||
226
+ agentDeclarationFixer !== undefined ||
227
+ beforeAllOrEachFixer !== undefined ||
228
+ afterAllFixer !== undefined
229
+ ) {
230
+ context.report({
231
+ messageId: 'updateTestWiring',
232
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
233
+ node: beforeAll ?? beforeEach!,
234
+ *fix(fixer) {
235
+ if (jestImportFixer !== undefined) {
236
+ yield jestImportFixer(fixer);
237
+ }
238
+ if (agentImportFixer !== undefined) {
239
+ yield agentImportFixer(fixer);
240
+ }
241
+ if (fixturePluginImportFixer !== undefined) {
242
+ yield fixturePluginImportFixer(fixer);
243
+ }
244
+ if (agentDeclarationFixer !== undefined) {
245
+ yield agentDeclarationFixer(fixer);
246
+ }
247
+ if (beforeAllOrEachFixer !== undefined) {
248
+ yield beforeAllOrEachFixer(fixer);
249
+ }
250
+ if (afterAllFixer !== undefined) {
251
+ yield afterAllFixer(fixer);
252
+ }
253
+ },
254
+ });
255
+ }
256
+ } catch (error) {
257
+ // eslint-disable-next-line no-console
258
+ console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
259
+ context.report({
260
+ node: program,
261
+ messageId: 'unknownError',
262
+ data: {
263
+ fileName: context.filename,
264
+ error: error instanceof Error ? error.toString() : JSON.stringify(error),
265
+ },
266
+ });
267
+ }
268
+ },
269
+ };
270
+ },
271
+ });
272
+
273
+ export default rule;
@@ -0,0 +1,197 @@
1
+ // agent/fetch-response-body-json.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 { AST_NODE_TYPES, ESLintUtils, TSESTree } from '@typescript-eslint/utils';
12
+
13
+ import getDocumentationUrl from '../get-documentation-url';
14
+ import { getAncestor } from '../library/ts-tree';
15
+
16
+ export const ruleId = 'fetch-response-body-json';
17
+
18
+ const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
19
+
20
+ interface Change {
21
+ enclosingFunction: TSESTree.FunctionExpression | TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration;
22
+ enclosingStatement: TSESTree.VariableDeclaration | TSESTree.ExpressionStatement | TSESTree.ReturnStatement;
23
+ enclosingStatementIndex: number;
24
+ responseBodyNode: TSESTree.MemberExpression;
25
+ responseVariableName: string;
26
+ responseBodyVariableName: string;
27
+ isResponseBodyVariableDeclared: boolean;
28
+ // replacementText: string;
29
+ }
30
+
31
+ const rule: ESLintUtils.RuleModule<'unknownError' | 'replaceBodyWithJson' | 'refactorNeeded'> = createRule({
32
+ name: ruleId,
33
+ meta: {
34
+ type: 'suggestion',
35
+ docs: {
36
+ description: 'Replace "response.body" with "await response.json()".',
37
+ },
38
+ messages: {
39
+ refactorNeeded:
40
+ 'Please extract the fetch call and check its reponse status code before accessing its response body.',
41
+ replaceBodyWithJson: 'Replace "response.body" with "await response.json()".',
42
+ unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}.',
43
+ },
44
+ fixable: 'code',
45
+ schema: [],
46
+ },
47
+ defaultOptions: [],
48
+ create(context) {
49
+ const parserServices = ESLintUtils.getParserServices(context);
50
+ const typeChecker = parserServices.program.getTypeChecker();
51
+ const allChanges = new Map<TSESTree.Node, Map<string, Change[]>>();
52
+
53
+ return {
54
+ 'MemberExpression[property.name="body"]': (responseBodyNode: TSESTree.MemberExpression) => {
55
+ try {
56
+ const responseNode = parserServices.esTreeNodeToTSNodeMap.get(responseBodyNode.object);
57
+ const responseType = typeChecker.getTypeAtLocation(responseNode);
58
+
59
+ const shouldReplace =
60
+ responseType.getProperties().some((symbol) => symbol.name === 'body') &&
61
+ responseType.getProperties().some((symbol) => symbol.name === 'json');
62
+
63
+ if (shouldReplace) {
64
+ if (responseBodyNode.object.type !== AST_NODE_TYPES.Identifier) {
65
+ context.report({
66
+ node: responseBodyNode,
67
+ messageId: 'refactorNeeded',
68
+ });
69
+ return;
70
+ }
71
+
72
+ const enclosingFunction = getAncestor(
73
+ responseBodyNode,
74
+ (node: TSESTree.Node) =>
75
+ node.type === AST_NODE_TYPES.ArrowFunctionExpression ||
76
+ node.type === AST_NODE_TYPES.FunctionExpression ||
77
+ node.type === AST_NODE_TYPES.FunctionDeclaration,
78
+ ) as TSESTree.FunctionExpression | TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration;
79
+ const enclosingStatement = getAncestor(
80
+ responseBodyNode,
81
+ (node: TSESTree.Node) =>
82
+ (node.type === AST_NODE_TYPES.VariableDeclaration ||
83
+ node.type === AST_NODE_TYPES.ExpressionStatement ||
84
+ node.type === AST_NODE_TYPES.ReturnStatement) &&
85
+ node.parent.type === AST_NODE_TYPES.BlockStatement,
86
+ ) as TSESTree.VariableDeclaration | TSESTree.ExpressionStatement | TSESTree.ReturnStatement;
87
+ const enclosingStatementIndex = (enclosingFunction.body as TSESTree.BlockStatement).body.indexOf(
88
+ enclosingStatement,
89
+ );
90
+ const responseVariableName = responseBodyNode.object.name;
91
+ const isResponseBodyVariableDeclared =
92
+ enclosingStatement.type === AST_NODE_TYPES.VariableDeclaration &&
93
+ enclosingStatement.declarations.some(
94
+ (declaration) =>
95
+ declaration.init === responseBodyNode ||
96
+ (declaration.init?.type === AST_NODE_TYPES.TSAsExpression &&
97
+ declaration.init.expression === responseBodyNode),
98
+ );
99
+ const responseBodyVariableName = isResponseBodyVariableDeclared
100
+ ? (enclosingStatement.declarations.find(
101
+ (declaration) =>
102
+ declaration.init === responseBodyNode ||
103
+ (declaration.init?.type === AST_NODE_TYPES.TSAsExpression &&
104
+ declaration.init.expression === responseBodyNode),
105
+ )?.id as unknown as string)
106
+ : `${responseBodyNode.object.name}Body`;
107
+
108
+ const change: Change = {
109
+ enclosingFunction,
110
+ enclosingStatement,
111
+ enclosingStatementIndex,
112
+ responseVariableName,
113
+ responseBodyNode,
114
+ responseBodyVariableName,
115
+ isResponseBodyVariableDeclared,
116
+ };
117
+
118
+ const changesByFunction = allChanges.get(enclosingFunction) ?? new Map<string, Change[]>();
119
+ const changesByResponse = changesByFunction.get(responseVariableName) ?? [];
120
+ changesByResponse.push(change);
121
+ changesByFunction.set(responseVariableName, changesByResponse);
122
+ allChanges.set(enclosingFunction, changesByFunction);
123
+ }
124
+ } catch (error) {
125
+ // eslint-disable-next-line no-console
126
+ console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
127
+ context.report({
128
+ node: responseBodyNode,
129
+ messageId: 'unknownError',
130
+ data: {
131
+ fileName: context.filename,
132
+ error: error instanceof Error ? error.toString() : JSON.stringify(error),
133
+ },
134
+ });
135
+ }
136
+ },
137
+
138
+ 'Program:exit': () => {
139
+ if (allChanges.size === 0) {
140
+ return;
141
+ }
142
+
143
+ const fixes: { node: TSESTree.Node | TSESTree.Token; text: string; insert: boolean }[] = [];
144
+ for (const changesByFunction of allChanges.values()) {
145
+ for (const changesByResponse of changesByFunction.values()) {
146
+ const orderedChanges = changesByResponse.sort(
147
+ (changeA, changeB) => changeA.enclosingStatementIndex - changeB.enclosingStatementIndex,
148
+ );
149
+ const firstChange = orderedChanges[0];
150
+ assert(firstChange);
151
+
152
+ const {
153
+ responseBodyNode,
154
+ responseVariableName,
155
+ responseBodyVariableName,
156
+ isResponseBodyVariableDeclared,
157
+ enclosingStatement,
158
+ } = firstChange;
159
+
160
+ let remainingChanges;
161
+ if (!isResponseBodyVariableDeclared) {
162
+ fixes.push({
163
+ node: context.sourceCode.getTokenBefore(enclosingStatement) as TSESTree.Token,
164
+ text: `\nconst ${responseBodyVariableName} = await ${responseVariableName}.json();`,
165
+ insert: true,
166
+ });
167
+ remainingChanges = orderedChanges;
168
+ } else {
169
+ fixes.push({
170
+ node: responseBodyNode,
171
+ text: `await ${responseVariableName}.json()`,
172
+ insert: false,
173
+ });
174
+ remainingChanges = orderedChanges.slice(1);
175
+ }
176
+
177
+ for (const change of remainingChanges) {
178
+ fixes.push({ node: change.responseBodyNode, text: responseBodyVariableName, insert: false });
179
+ }
180
+ }
181
+ }
182
+
183
+ for (const fix of fixes.reverse()) {
184
+ context.report({
185
+ node: fix.node,
186
+ messageId: 'replaceBodyWithJson',
187
+ fix(fixer) {
188
+ return fix.insert ? fixer.insertTextAfter(fix.node, fix.text) : fixer.replaceText(fix.node, fix.text);
189
+ },
190
+ });
191
+ }
192
+ },
193
+ };
194
+ },
195
+ });
196
+
197
+ export default rule;
@@ -0,0 +1,148 @@
1
+ // agent/fetch-response-header-getter-ts.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
+
11
+ import getDocumentationUrl from '../get-documentation-url';
12
+
13
+ export const ruleId = 'fetch-response-header-getter-ts';
14
+ const HEADER_BUILTIN_FUNCTIONS = Object.keys(Headers.prototype);
15
+
16
+ const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
17
+
18
+ const rule: ESLintUtils.RuleModule<'unknownError' | 'useGetter'> = createRule({
19
+ name: ruleId,
20
+ meta: {
21
+ type: 'suggestion',
22
+ docs: {
23
+ description: 'Use "get()" method to get header value from the headers object of the fetch response.',
24
+ },
25
+ messages: {
26
+ useGetter: 'Use "get()" method to get header value from the headers object of the fetch response.',
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
+ const sourceCode = context.sourceCode;
37
+
38
+ return {
39
+ MemberExpression: (responseHeadersAccess: TSESTree.MemberExpression) => {
40
+ try {
41
+ if (
42
+ responseHeadersAccess.property.type === AST_NODE_TYPES.Identifier &&
43
+ HEADER_BUILTIN_FUNCTIONS.includes(responseHeadersAccess.property.name)
44
+ ) {
45
+ // skip Headers's built-in function calls
46
+ return;
47
+ }
48
+
49
+ const responseHeadersTsNode = parserServices.esTreeNodeToTSNodeMap.get(responseHeadersAccess.object);
50
+ let responseHeadersType = typeChecker.getTypeAtLocation(responseHeadersTsNode);
51
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
52
+ responseHeadersType = responseHeadersType.isUnion() ? responseHeadersType.types[0]! : responseHeadersType;
53
+ const responseHeadersTypeName = // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
54
+ (responseHeadersType.symbol ?? responseHeadersType.aliasSymbol)?.escapedName;
55
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
56
+ if (responseHeadersTypeName !== 'Headers' && responseHeadersTypeName !== 'HeaderGetter') {
57
+ return;
58
+ }
59
+
60
+ let replacementText: string;
61
+ if (!responseHeadersAccess.computed) {
62
+ // e.g. headers.etag
63
+ replacementText = `${sourceCode.getText(responseHeadersAccess.object)}.get('${sourceCode.getText(responseHeadersAccess.property)}')`;
64
+ } else if (
65
+ responseHeadersAccess.property.type === AST_NODE_TYPES.Identifier ||
66
+ responseHeadersAccess.property.type === AST_NODE_TYPES.Literal ||
67
+ responseHeadersAccess.property.type === AST_NODE_TYPES.TemplateLiteral
68
+ ) {
69
+ replacementText = `${sourceCode.getText(responseHeadersAccess.object)}.get(${sourceCode.getText(responseHeadersAccess.property)})`;
70
+ } else {
71
+ throw new Error(`Unexpected property type: ${responseHeadersAccess.property.type}`);
72
+ }
73
+
74
+ context.report({
75
+ messageId: 'useGetter',
76
+ node: responseHeadersAccess.property,
77
+ fix(fixer) {
78
+ return fixer.replaceText(responseHeadersAccess, replacementText);
79
+ },
80
+ });
81
+ } catch (error) {
82
+ // eslint-disable-next-line no-console
83
+ console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
84
+ context.report({
85
+ node: responseHeadersAccess,
86
+ messageId: 'unknownError',
87
+ data: {
88
+ fileName: context.filename,
89
+ error: error instanceof Error ? error.toString() : JSON.stringify(error),
90
+ },
91
+ });
92
+ }
93
+ },
94
+
95
+ // convert response.get() to response.headers.get()
96
+ 'CallExpression[callee.property.name="get"]': (responseHeadersAccess: TSESTree.CallExpression) => {
97
+ try {
98
+ if (responseHeadersAccess.callee.type !== AST_NODE_TYPES.MemberExpression) {
99
+ return;
100
+ }
101
+
102
+ // skip request-like calls
103
+ if (
104
+ responseHeadersAccess.callee.object.type !== AST_NODE_TYPES.Identifier ||
105
+ responseHeadersAccess.callee.object.name === 'request'
106
+ ) {
107
+ return;
108
+ }
109
+ const responseNode = responseHeadersAccess.callee.object;
110
+ const responseHeadersTsNode = parserServices.esTreeNodeToTSNodeMap.get(responseNode);
111
+ const responseType = typeChecker.getTypeAtLocation(responseHeadersTsNode);
112
+ const typeName = typeChecker.typeToString(responseType);
113
+ if (typeName === 'InboundContext' || typeName.endsWith('RequestType')) {
114
+ return;
115
+ }
116
+
117
+ // make sure the response type has "headers" property
118
+ const hasHeadersProperty = responseType.getProperties().some((symbol) => symbol.name === 'headers');
119
+ if (!hasHeadersProperty) {
120
+ return;
121
+ }
122
+
123
+ const replacementText = `${sourceCode.getText(responseNode)}.headers`;
124
+ context.report({
125
+ messageId: 'useGetter',
126
+ node: responseHeadersAccess,
127
+ fix(fixer) {
128
+ return fixer.replaceText(responseNode, replacementText);
129
+ },
130
+ });
131
+ } catch (error) {
132
+ // eslint-disable-next-line no-console
133
+ console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
134
+ context.report({
135
+ node: responseHeadersAccess,
136
+ messageId: 'unknownError',
137
+ data: {
138
+ fileName: context.filename,
139
+ error: error instanceof Error ? error.toString() : JSON.stringify(error),
140
+ },
141
+ });
142
+ }
143
+ },
144
+ };
145
+ },
146
+ });
147
+
148
+ export default rule;