@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,4 @@
1
+ import { ESLintUtils } from '@typescript-eslint/utils';
2
+ export declare const ruleId = "no-unused-service-variable";
3
+ declare const rule: ESLintUtils.RuleModule<'unknownError' | 'removeUnusedServiceVariables'>;
4
+ export default rule;
@@ -0,0 +1,16 @@
1
+ import type { MemberExpression, VariableDeclaration } from 'estree';
2
+ import { type Scope } from 'eslint';
3
+ /**
4
+ * analyze response related variables and their references
5
+ * the implementation is for fixture API, but it can be used for fetch API as well since the tree structure is similar
6
+ * @param variableDeclaration - variable declaration node
7
+ */
8
+ export declare function analyzeResponseReferences(variableDeclaration: VariableDeclaration | undefined, scopeManager: Scope.ScopeManager): {
9
+ variable?: Scope.Variable;
10
+ bodyReferences: MemberExpression[];
11
+ headersReferences: MemberExpression[];
12
+ statusReferences: MemberExpression[];
13
+ destructuringBodyVariable?: Scope.Variable;
14
+ destructuringHeadersVariable?: Scope.Variable;
15
+ destructuringHeadersReferences?: MemberExpression[] | undefined;
16
+ };
@@ -0,0 +1,5 @@
1
+ export declare const PLAIN_URL_REGEXP: RegExp;
2
+ export declare const TOKENIZED_URL_REGEXP: RegExp;
3
+ export declare function replaceEndpointUrlPrefixWithBasePath(url: string): string;
4
+ export declare function replaceEndpointUrlPrefixWithDomain(url: string): string;
5
+ export declare function addBasePathUrlDomain(url: string): string;
@@ -1,3 +1,5 @@
1
1
  import type { TSESLint } from '@typescript-eslint/utils';
2
- declare const pluginToExport: TSESLint.FlatConfig.Plugin;
3
- export default pluginToExport;
2
+ declare const defaultToExport: Exclude<TSESLint.FlatConfig.Plugin, 'config'> & {
3
+ configs: Record<string, TSESLint.FlatConfig.Config | TSESLint.FlatConfig.Config[]>;
4
+ };
5
+ export default defaultToExport;
package/package.json CHANGED
@@ -1,96 +1 @@
1
- {
2
- "name": "@checkdigit/eslint-plugin",
3
- "version": "7.2.0",
4
- "description": "Check Digit eslint plugins",
5
- "keywords": [
6
- "eslint",
7
- "eslintplugin"
8
- ],
9
- "homepage": "https://github.com/checkdigit/eslint-plugin#readme",
10
- "bugs": {
11
- "url": "https://github.com/checkdigit/eslint-plugin/issues"
12
- },
13
- "repository": {
14
- "type": "git",
15
- "url": "https://github.com/checkdigit/eslint-plugin"
16
- },
17
- "license": "MIT",
18
- "author": "Check Digit, LLC",
19
- "sideEffects": false,
20
- "type": "module",
21
- "exports": {
22
- ".": {
23
- "types": "./dist-types/index.d.ts",
24
- "import": "./dist-mjs/index.mjs",
25
- "default": "./dist-mjs/index.mjs"
26
- }
27
- },
28
- "files": [
29
- "src",
30
- "dist-types",
31
- "dist-mjs",
32
- "!src/**/test/**",
33
- "!src/**/*.test.ts",
34
- "!src/**/*.spec.ts",
35
- "!dist-types/**/test/**",
36
- "!dist-types/**/*.test.d.ts",
37
- "!dist-types/**/*.spec.d.ts",
38
- "!dist-mjs/**/test/**",
39
- "!dist-mjs/**/*.test.mjs",
40
- "!dist-mjs/**/*.spec.mjs",
41
- "SECURITY.md"
42
- ],
43
- "scripts": {
44
- "build:dist-mjs": "rimraf dist-mjs && npx builder --type=module --sourceMap --outDir=dist-mjs && node dist-mjs/index.mjs",
45
- "build:dist-types": "rimraf dist-types && npx builder --type=types --outDir=dist-types",
46
- "ci:compile": "tsc --noEmit",
47
- "ci:coverage": "NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=true",
48
- "ci:lint": "npm run lint",
49
- "ci:style": "npm run prettier",
50
- "ci:test": "NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=false",
51
- "lint": "eslint --max-warnings 0 .",
52
- "lint:fix": "eslint --max-warnings 0 --fix .",
53
- "prepare": "",
54
- "prepublishOnly": "npm run build:dist-types && npm run build:dist-mjs",
55
- "prettier": "prettier --ignore-path .gitignore --list-different .",
56
- "prettier:fix": "prettier --ignore-path .gitignore --write .",
57
- "test": "npm run ci:compile && npm run ci:test && npm run ci:lint && npm run ci:style"
58
- },
59
- "prettier": "@checkdigit/prettier-config",
60
- "jest": {
61
- "preset": "@checkdigit/jest-config"
62
- },
63
- "dependencies": {
64
- "@typescript-eslint/type-utils": "^8.10.0",
65
- "@typescript-eslint/utils": "^8.10.0",
66
- "ts-api-utils": "^1.3.0"
67
- },
68
- "devDependencies": {
69
- "@checkdigit/jest-config": "^6.0.2",
70
- "@checkdigit/prettier-config": "^5.5.1",
71
- "@checkdigit/typescript-config": "^8.0.0",
72
- "@eslint/js": "^9.13.0",
73
- "@types/eslint": "^9.6.1",
74
- "@types/eslint-config-prettier": "^6.11.3",
75
- "@typescript-eslint/parser": "^8.10.0",
76
- "@typescript-eslint/rule-tester": "^8.10.0",
77
- "eslint": "^9.13.0",
78
- "eslint-config-prettier": "^9.1.0",
79
- "eslint-import-resolver-typescript": "^3.6.3",
80
- "eslint-plugin-eslint-plugin": "^6.2.0",
81
- "eslint-plugin-import": "^2.31.0",
82
- "eslint-plugin-no-only-tests": "^3.3.0",
83
- "eslint-plugin-no-secrets": "^1.0.2",
84
- "eslint-plugin-node": "^11.1.0",
85
- "eslint-plugin-sonarjs": "1.0.4",
86
- "http-status-codes": "^2.3.0",
87
- "rimraf": "^6.0.1",
88
- "typescript-eslint": "^8.10.0"
89
- },
90
- "peerDependencies": {
91
- "eslint": ">=9 <10"
92
- },
93
- "engines": {
94
- "node": ">=20.17"
95
- }
96
- }
1
+ {"name":"@checkdigit/eslint-plugin","version":"7.3.0-PR.75-aa6d","description":"Check Digit eslint plugins","keywords":["eslint","eslintplugin"],"homepage":"https://github.com/checkdigit/eslint-plugin#readme","bugs":{"url":"https://github.com/checkdigit/eslint-plugin/issues"},"repository":{"type":"git","url":"https://github.com/checkdigit/eslint-plugin"},"license":"MIT","author":"Check Digit, LLC","sideEffects":false,"type":"module","exports":{".":{"types":"./dist-types/index.d.ts","import":"./dist-mjs/index.mjs","default":"./dist-mjs/index.mjs"}},"files":["src","dist-types","dist-mjs","!src/**/test/**","!src/**/*.test.ts","!src/**/*.spec.ts","!dist-types/**/test/**","!dist-types/**/*.test.d.ts","!dist-types/**/*.spec.d.ts","!dist-mjs/**/test/**","!dist-mjs/**/*.test.mjs","!dist-mjs/**/*.spec.mjs","SECURITY.md"],"scripts":{"build:dist-mjs":"rimraf dist-mjs && npx builder --type=module --sourceMap --outDir=dist-mjs && node dist-mjs/index.mjs","build:dist-types":"rimraf dist-types && npx builder --type=types --outDir=dist-types","ci:compile":"tsc --noEmit","ci:coverage":"NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=true","ci:lint":"npm run lint","ci:style":"npm run prettier","ci:test":"NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=false","lint":"eslint --max-warnings 0 .","lint:fix":"eslint --max-warnings 0 --fix .","prepare":"","prepublishOnly":"npm run build:dist-types && npm run build:dist-mjs","prettier":"prettier --ignore-path .gitignore --list-different .","prettier:fix":"prettier --ignore-path .gitignore --write .","test":"npm run ci:compile && npm run ci:test && npm run ci:lint && npm run ci:style"},"prettier":"@checkdigit/prettier-config","jest":{"preset":"@checkdigit/jest-config"},"dependencies":{"@typescript-eslint/type-utils":"8.10.0","@typescript-eslint/utils":"8.10.0","debug":"^4.3.7","ts-api-utils":"^1.3.0"},"devDependencies":{"@checkdigit/jest-config":"^6.0.2","@checkdigit/prettier-config":"^5.5.1","@checkdigit/typescript-config":"^8.0.0","@eslint/js":"^9.13.0","@types/debug":"^4.1.12","@types/eslint":"^9.6.1","@types/eslint-config-prettier":"^6.11.3","@typescript-eslint/parser":"8.10.0","@typescript-eslint/rule-tester":"8.10.0","eslint":"^9.13.0","eslint-config-prettier":"^9.1.0","eslint-import-resolver-typescript":"^3.6.3","eslint-plugin-eslint-plugin":"^6.2.0","eslint-plugin-import":"^2.31.0","eslint-plugin-no-only-tests":"^3.3.0","eslint-plugin-no-secrets":"^1.0.2","eslint-plugin-node":"^11.1.0","eslint-plugin-sonarjs":"1.0.4","http-status-codes":"^2.3.0","rimraf":"^6.0.1","typescript-eslint":"8.10.0"},"peerDependencies":{"eslint":">=9 <10"},"engines":{"node":">=20.17"}}
@@ -0,0 +1,76 @@
1
+ // agent/add-url-domain.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
+ import { addBasePathUrlDomain } from './url';
13
+
14
+ export const ruleId = 'add-url-domain';
15
+
16
+ const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
17
+
18
+ const rule: ESLintUtils.RuleModule<'addDomain' | 'unknownError'> = createRule({
19
+ name: ruleId,
20
+ meta: {
21
+ type: 'suggestion',
22
+ docs: {
23
+ description: 'Add HTTP domain to the BASE_PATH like url constant variable.',
24
+ },
25
+ messages: {
26
+ addDomain: 'Add HTTP domain to the BASE_PATH like url constant variable.',
27
+ unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}.',
28
+ },
29
+ fixable: 'code',
30
+ schema: [],
31
+ },
32
+ defaultOptions: [],
33
+ create(context) {
34
+ const sourceCode = context.sourceCode;
35
+
36
+ return {
37
+ 'VariableDeclarator[id.name=/^([A-Z]+_)*BASE_PATH$/]': (basePathDeclarator: TSESTree.VariableDeclarator) => {
38
+ try {
39
+ if (
40
+ basePathDeclarator.init === null ||
41
+ (basePathDeclarator.init.type !== AST_NODE_TYPES.Literal &&
42
+ basePathDeclarator.init.type !== AST_NODE_TYPES.TemplateLiteral)
43
+ ) {
44
+ return;
45
+ }
46
+
47
+ const urlText = sourceCode.getText(basePathDeclarator.init);
48
+ const replacement = addBasePathUrlDomain(urlText);
49
+
50
+ if (replacement !== urlText) {
51
+ context.report({
52
+ messageId: 'addDomain',
53
+ node: basePathDeclarator.init,
54
+ fix(fixer) {
55
+ return fixer.replaceText(basePathDeclarator.init as TSESTree.Node, replacement);
56
+ },
57
+ });
58
+ }
59
+ } catch (error) {
60
+ // eslint-disable-next-line no-console
61
+ console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
62
+ context.report({
63
+ node: basePathDeclarator,
64
+ messageId: 'unknownError',
65
+ data: {
66
+ fileName: context.filename,
67
+ error: error instanceof Error ? error.toString() : JSON.stringify(error),
68
+ },
69
+ });
70
+ }
71
+ },
72
+ };
73
+ },
74
+ });
75
+
76
+ export default rule;
@@ -0,0 +1,204 @@
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
+ import { AST_TOKEN_TYPES, ESLintUtils, TSESTree } from '@typescript-eslint/utils';
11
+ import type { RuleFix, RuleFixer } from '@typescript-eslint/utils/ts-eslint';
12
+ import getDocumentationUrl from '../get-documentation-url';
13
+
14
+ export const ruleId = 'agent-test-wiring';
15
+
16
+ const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
17
+
18
+ const rule: ESLintUtils.RuleModule<'updateTestWiring' | 'unknownError'> = createRule({
19
+ name: ruleId,
20
+ meta: {
21
+ type: 'suggestion',
22
+ docs: {
23
+ description: 'Update test wiring.',
24
+ },
25
+ messages: {
26
+ updateTestWiring: 'Updating test wiring.',
27
+ unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}.',
28
+ },
29
+ fixable: 'code',
30
+ schema: [],
31
+ },
32
+ defaultOptions: [],
33
+ create(context) {
34
+ const sourceCode = context.sourceCode;
35
+ const importDeclarations = new Map<string, TSESTree.ImportDeclaration>();
36
+ let beforeAllCallExpression: TSESTree.CallExpression | undefined;
37
+ let afterAllCallExpression: TSESTree.CallExpression | undefined;
38
+
39
+ return {
40
+ ImportDeclaration(importDeclaration) {
41
+ const moduleName = importDeclaration.source.value;
42
+ importDeclarations.set(moduleName, importDeclaration);
43
+ },
44
+ 'CallExpression[callee.name="beforeAll"]': (callExpression: TSESTree.CallExpression) => {
45
+ beforeAllCallExpression = callExpression;
46
+ },
47
+ 'CallExpression[callee.name="afterAll"]': (callExpression: TSESTree.CallExpression) => {
48
+ afterAllCallExpression = callExpression;
49
+ },
50
+ 'Program:exit'(program) {
51
+ try {
52
+ let jestImportFixer: ((fixer: RuleFixer) => RuleFix) | undefined;
53
+ let agentImportFixer: ((fixer: RuleFixer) => RuleFix) | undefined;
54
+ let fixturePluginImportFixer: ((fixer: RuleFixer) => RuleFix) | undefined;
55
+ let agentDeclarationFixer: ((fixer: RuleFixer) => RuleFix) | undefined;
56
+ let beforeAllFixer: ((fixer: RuleFixer) => RuleFix) | undefined;
57
+ let afterAllFixer: ((fixer: RuleFixer) => RuleFix) | undefined;
58
+
59
+ const lastImportDeclaration = [...importDeclarations.values()].at(-1);
60
+ assert.ok(lastImportDeclaration);
61
+
62
+ const jestImportDeclaration = importDeclarations.get('@jest/globals');
63
+ if (
64
+ jestImportDeclaration &&
65
+ !jestImportDeclaration.specifiers.some(
66
+ (specifier) =>
67
+ specifier.type === TSESTree.AST_NODE_TYPES.ImportSpecifier &&
68
+ specifier.imported.type === TSESTree.AST_NODE_TYPES.Identifier &&
69
+ specifier.imported.name === 'afterAll',
70
+ )
71
+ ) {
72
+ const firstImportSpecifier = jestImportDeclaration.specifiers[0];
73
+ assert.ok(firstImportSpecifier);
74
+ jestImportFixer = (fixer: RuleFixer) => fixer.insertTextBefore(firstImportSpecifier, 'afterAll, ');
75
+ }
76
+
77
+ const agentImportDeclaration = importDeclarations.get('@checkdigit/agent');
78
+ if (!agentImportDeclaration) {
79
+ agentImportFixer = (fixer: RuleFixer) =>
80
+ fixer.insertTextAfter(
81
+ lastImportDeclaration,
82
+ `\nimport createAgent, { type Agent } from '@checkdigit/agent';`,
83
+ );
84
+ }
85
+
86
+ const fixturePluginImportDeclaration = importDeclarations.get('../../plugin/fixture.test');
87
+ if (!fixturePluginImportDeclaration) {
88
+ fixturePluginImportFixer = (fixer: RuleFixer) =>
89
+ fixer.insertTextAfter(lastImportDeclaration, `\nimport fixturePlugin from '../../plugin/fixture.test';`);
90
+ }
91
+
92
+ if (beforeAllCallExpression !== undefined) {
93
+ const beforeAllArrowFunctionExpression = beforeAllCallExpression.arguments[0];
94
+ assert.ok(
95
+ beforeAllArrowFunctionExpression !== undefined &&
96
+ beforeAllArrowFunctionExpression.type === TSESTree.AST_NODE_TYPES.ArrowFunctionExpression,
97
+ );
98
+ const arrowFunctionBody = beforeAllArrowFunctionExpression.body;
99
+ assert.ok(arrowFunctionBody.type === TSESTree.AST_NODE_TYPES.BlockStatement);
100
+
101
+ const targetStatement = arrowFunctionBody.body.find(
102
+ (statement) => sourceCode.getText(statement) === 'await fixture.reset();',
103
+ );
104
+ if (targetStatement !== undefined) {
105
+ const beforeAllBodyText = sourceCode.getText(arrowFunctionBody);
106
+ if (!beforeAllBodyText.includes('agent = await createAgent();')) {
107
+ beforeAllFixer = (fixer: RuleFixer) =>
108
+ fixer.replaceText(
109
+ targetStatement,
110
+ [
111
+ 'agent = await createAgent();',
112
+ 'agent.register(await fixturePlugin(fixture));',
113
+ 'agent.enable();',
114
+ 'await fixture.reset();',
115
+ ].join('\n'),
116
+ );
117
+ agentDeclarationFixer = (fixer: RuleFixer) =>
118
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
119
+ fixer.insertTextBefore(beforeAllCallExpression!, 'let agent: Agent;\n');
120
+ }
121
+ }
122
+ }
123
+
124
+ if (afterAllCallExpression !== undefined) {
125
+ const afterAllArrowFunctionExpression = afterAllCallExpression.arguments[0];
126
+ assert.ok(
127
+ afterAllArrowFunctionExpression !== undefined &&
128
+ afterAllArrowFunctionExpression.type === TSESTree.AST_NODE_TYPES.ArrowFunctionExpression,
129
+ );
130
+ const arrowFunctionBody = afterAllArrowFunctionExpression.body;
131
+ assert.ok(arrowFunctionBody.type === TSESTree.AST_NODE_TYPES.BlockStatement);
132
+
133
+ const afterAllBodyText = sourceCode.getText(arrowFunctionBody);
134
+ if (!afterAllBodyText.includes('await agent[Symbol.asyncDispose]();')) {
135
+ const lastStatement = arrowFunctionBody.body.at(-1);
136
+ assert.ok(lastStatement);
137
+ afterAllFixer = (fixer: RuleFixer) =>
138
+ fixer.insertTextAfter(lastStatement, 'await agent[Symbol.asyncDispose]();');
139
+ }
140
+ } else if (beforeAllCallExpression !== undefined) {
141
+ const nextToken = sourceCode.getTokenAfter(beforeAllCallExpression);
142
+ afterAllFixer = (fixer: RuleFixer) =>
143
+ fixer.insertTextAfter(
144
+ nextToken !== null && nextToken.type === AST_TOKEN_TYPES.Punctuator
145
+ ? nextToken
146
+ : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
147
+ beforeAllCallExpression!,
148
+ `\nafterAll(async () => {
149
+ await agent[Symbol.asyncDispose]();
150
+ });`,
151
+ );
152
+ }
153
+
154
+ if (
155
+ jestImportFixer !== undefined ||
156
+ agentImportFixer !== undefined ||
157
+ fixturePluginImportFixer !== undefined ||
158
+ agentDeclarationFixer !== undefined ||
159
+ beforeAllFixer !== undefined ||
160
+ afterAllFixer !== undefined
161
+ ) {
162
+ context.report({
163
+ messageId: 'updateTestWiring',
164
+ node: program,
165
+ *fix(fixer) {
166
+ if (jestImportFixer !== undefined) {
167
+ yield jestImportFixer(fixer);
168
+ }
169
+ if (agentImportFixer !== undefined) {
170
+ yield agentImportFixer(fixer);
171
+ }
172
+ if (fixturePluginImportFixer !== undefined) {
173
+ yield fixturePluginImportFixer(fixer);
174
+ }
175
+ if (agentDeclarationFixer !== undefined) {
176
+ yield agentDeclarationFixer(fixer);
177
+ }
178
+ if (beforeAllFixer !== undefined) {
179
+ yield beforeAllFixer(fixer);
180
+ }
181
+ if (afterAllFixer !== undefined) {
182
+ yield afterAllFixer(fixer);
183
+ }
184
+ },
185
+ });
186
+ }
187
+ } catch (error) {
188
+ // eslint-disable-next-line no-console
189
+ console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
190
+ context.report({
191
+ node: program,
192
+ messageId: 'unknownError',
193
+ data: {
194
+ fileName: context.filename,
195
+ error: error instanceof Error ? error.toString() : JSON.stringify(error),
196
+ },
197
+ });
198
+ }
199
+ },
200
+ };
201
+ },
202
+ });
203
+
204
+ export default rule;
@@ -0,0 +1,77 @@
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 { AST_NODE_TYPES, ESLintUtils, TSESTree } from '@typescript-eslint/utils';
10
+
11
+ import getDocumentationUrl from '../get-documentation-url';
12
+
13
+ export const ruleId = 'fetch-response-body-json';
14
+
15
+ const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
16
+
17
+ const rule: ESLintUtils.RuleModule<'unknownError' | 'replaceBodyWithJson'> = createRule({
18
+ name: ruleId,
19
+ meta: {
20
+ type: 'suggestion',
21
+ docs: {
22
+ description: 'Replace "response.body" with "await response.json()".',
23
+ },
24
+ messages: {
25
+ replaceBodyWithJson: 'Replace "response.body" with "await response.json()".',
26
+ unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}.',
27
+ },
28
+ fixable: 'code',
29
+ schema: [],
30
+ },
31
+ defaultOptions: [],
32
+ create(context) {
33
+ const parserServices = ESLintUtils.getParserServices(context);
34
+ const typeChecker = parserServices.program.getTypeChecker();
35
+ const sourceCode = context.sourceCode;
36
+
37
+ return {
38
+ 'MemberExpression[property.name="body"]': (responseBody: TSESTree.MemberExpression) => {
39
+ try {
40
+ const responseNode = parserServices.esTreeNodeToTSNodeMap.get(responseBody.object);
41
+ const responseType = typeChecker.getTypeAtLocation(responseNode);
42
+
43
+ const shouldReplace =
44
+ responseType.getProperties().some((symbol) => symbol.name === 'body') &&
45
+ responseType.getProperties().some((symbol) => symbol.name === 'json');
46
+
47
+ if (shouldReplace) {
48
+ const responseText = sourceCode.getText(responseBody.object);
49
+ const needAwait = responseBody.parent.type !== AST_NODE_TYPES.ReturnStatement;
50
+ const replacementText = needAwait ? `(await ${responseText}.json())` : `${responseText}.json()`;
51
+
52
+ context.report({
53
+ messageId: 'replaceBodyWithJson',
54
+ node: responseBody,
55
+ fix(fixer) {
56
+ return fixer.replaceText(responseBody, replacementText);
57
+ },
58
+ });
59
+ }
60
+ } catch (error) {
61
+ // eslint-disable-next-line no-console
62
+ console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
63
+ context.report({
64
+ node: responseBody,
65
+ messageId: 'unknownError',
66
+ data: {
67
+ fileName: context.filename,
68
+ error: error instanceof Error ? error.toString() : JSON.stringify(error),
69
+ },
70
+ });
71
+ }
72
+ },
73
+ };
74
+ },
75
+ });
76
+
77
+ 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;