@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,194 +0,0 @@
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
- import { isFetchResponse } from './fetch';
16
-
17
- export const ruleId = 'fetch-response-body-json';
18
-
19
- const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
20
-
21
- interface Change {
22
- enclosingFunction: TSESTree.FunctionExpression | TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration;
23
- enclosingStatement: TSESTree.VariableDeclaration | TSESTree.ExpressionStatement | TSESTree.ReturnStatement;
24
- enclosingStatementIndex: number;
25
- responseBodyNode: TSESTree.MemberExpression;
26
- responseVariableName: string;
27
- responseBodyVariableName: string;
28
- isResponseBodyVariableDeclared: boolean;
29
- // replacementText: string;
30
- }
31
-
32
- const rule: ESLintUtils.RuleModule<'unknownError' | 'replaceBodyWithJson' | 'refactorNeeded'> = createRule({
33
- name: ruleId,
34
- meta: {
35
- type: 'suggestion',
36
- docs: {
37
- description: 'Replace "response.body" with "await response.json()".',
38
- },
39
- messages: {
40
- refactorNeeded:
41
- 'Please extract the fetch call and check its reponse status code before accessing its response body.',
42
- replaceBodyWithJson: 'Replace "response.body" with "await response.json()".',
43
- unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}.',
44
- },
45
- fixable: 'code',
46
- schema: [],
47
- },
48
- defaultOptions: [],
49
- create(context) {
50
- const parserServices = ESLintUtils.getParserServices(context);
51
- const typeChecker = parserServices.program.getTypeChecker();
52
- const allChanges = new Map<TSESTree.Node, Map<string, Change[]>>();
53
-
54
- return {
55
- 'MemberExpression[property.name="body"]': (responseBodyNode: TSESTree.MemberExpression) => {
56
- try {
57
- const responseNode = parserServices.esTreeNodeToTSNodeMap.get(responseBodyNode.object);
58
- const responseType = typeChecker.getTypeAtLocation(responseNode);
59
-
60
- if (isFetchResponse(responseType)) {
61
- if (responseBodyNode.object.type !== AST_NODE_TYPES.Identifier) {
62
- context.report({
63
- node: responseBodyNode,
64
- messageId: 'refactorNeeded',
65
- });
66
- return;
67
- }
68
-
69
- const enclosingFunction = getAncestor(
70
- responseBodyNode,
71
- (node: TSESTree.Node) =>
72
- node.type === AST_NODE_TYPES.ArrowFunctionExpression ||
73
- node.type === AST_NODE_TYPES.FunctionExpression ||
74
- node.type === AST_NODE_TYPES.FunctionDeclaration,
75
- ) as TSESTree.FunctionExpression | TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration;
76
- const enclosingStatement = getAncestor(
77
- responseBodyNode,
78
- (node: TSESTree.Node) =>
79
- (node.type === AST_NODE_TYPES.VariableDeclaration ||
80
- node.type === AST_NODE_TYPES.ExpressionStatement ||
81
- node.type === AST_NODE_TYPES.ReturnStatement) &&
82
- node.parent.type === AST_NODE_TYPES.BlockStatement,
83
- ) as TSESTree.VariableDeclaration | TSESTree.ExpressionStatement | TSESTree.ReturnStatement;
84
- const enclosingStatementIndex = (enclosingFunction.body as TSESTree.BlockStatement).body.indexOf(
85
- enclosingStatement,
86
- );
87
- const responseVariableName = responseBodyNode.object.name;
88
- const isResponseBodyVariableDeclared =
89
- enclosingStatement.type === AST_NODE_TYPES.VariableDeclaration &&
90
- enclosingStatement.declarations.some(
91
- (declaration) =>
92
- declaration.init === responseBodyNode ||
93
- (declaration.init?.type === AST_NODE_TYPES.TSAsExpression &&
94
- declaration.init.expression === responseBodyNode),
95
- );
96
- const responseBodyVariableName = isResponseBodyVariableDeclared
97
- ? (enclosingStatement.declarations.find(
98
- (declaration) =>
99
- declaration.init === responseBodyNode ||
100
- (declaration.init?.type === AST_NODE_TYPES.TSAsExpression &&
101
- declaration.init.expression === responseBodyNode),
102
- )?.id as unknown as string)
103
- : `${responseBodyNode.object.name}Body`;
104
-
105
- const change: Change = {
106
- enclosingFunction,
107
- enclosingStatement,
108
- enclosingStatementIndex,
109
- responseVariableName,
110
- responseBodyNode,
111
- responseBodyVariableName,
112
- isResponseBodyVariableDeclared,
113
- };
114
-
115
- const changesByFunction = allChanges.get(enclosingFunction) ?? new Map<string, Change[]>();
116
- const changesByResponse = changesByFunction.get(responseVariableName) ?? [];
117
- changesByResponse.push(change);
118
- changesByFunction.set(responseVariableName, changesByResponse);
119
- allChanges.set(enclosingFunction, changesByFunction);
120
- }
121
- } catch (error) {
122
- // eslint-disable-next-line no-console
123
- console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
124
- context.report({
125
- node: responseBodyNode,
126
- messageId: 'unknownError',
127
- data: {
128
- fileName: context.filename,
129
- error: error instanceof Error ? error.toString() : JSON.stringify(error),
130
- },
131
- });
132
- }
133
- },
134
-
135
- 'Program:exit': () => {
136
- if (allChanges.size === 0) {
137
- return;
138
- }
139
-
140
- const fixes: { node: TSESTree.Node | TSESTree.Token; text: string; insert: boolean }[] = [];
141
- for (const changesByFunction of allChanges.values()) {
142
- for (const changesByResponse of changesByFunction.values()) {
143
- const orderedChanges = changesByResponse.sort(
144
- (changeA, changeB) => changeA.enclosingStatementIndex - changeB.enclosingStatementIndex,
145
- );
146
- const firstChange = orderedChanges[0];
147
- assert(firstChange);
148
-
149
- const {
150
- responseBodyNode,
151
- responseVariableName,
152
- responseBodyVariableName,
153
- isResponseBodyVariableDeclared,
154
- enclosingStatement,
155
- } = firstChange;
156
-
157
- let remainingChanges;
158
- if (!isResponseBodyVariableDeclared) {
159
- fixes.push({
160
- node: context.sourceCode.getTokenBefore(enclosingStatement) as TSESTree.Token,
161
- text: `\nconst ${responseBodyVariableName} = await ${responseVariableName}.json();`,
162
- insert: true,
163
- });
164
- remainingChanges = orderedChanges;
165
- } else {
166
- fixes.push({
167
- node: responseBodyNode,
168
- text: `await ${responseVariableName}.json()`,
169
- insert: false,
170
- });
171
- remainingChanges = orderedChanges.slice(1);
172
- }
173
-
174
- for (const change of remainingChanges) {
175
- fixes.push({ node: change.responseBodyNode, text: responseBodyVariableName, insert: false });
176
- }
177
- }
178
- }
179
-
180
- for (const fix of fixes.reverse()) {
181
- context.report({
182
- node: fix.node,
183
- messageId: 'replaceBodyWithJson',
184
- fix(fixer) {
185
- return fix.insert ? fixer.insertTextAfter(fix.node, fix.text) : fixer.replaceText(fix.node, fix.text);
186
- },
187
- });
188
- }
189
- },
190
- };
191
- },
192
- });
193
-
194
- export default rule;
@@ -1,148 +0,0 @@
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;
@@ -1,100 +0,0 @@
1
- // agent/fetch-response-status.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 { isFetchResponse } from './fetch';
13
-
14
- export const ruleId = 'fetch-response-status';
15
-
16
- const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
17
-
18
- const rule: ESLintUtils.RuleModule<'unknownError' | 'renameStatusCodeProperty'> = createRule({
19
- name: ruleId,
20
- meta: {
21
- type: 'problem',
22
- docs: {
23
- description: 'Replace "response.body" with "await response.json()".',
24
- },
25
- messages: {
26
- renameStatusCodeProperty: 'Rename "statusCode" with "status".',
27
- unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}.',
28
- },
29
- fixable: 'code',
30
- schema: [],
31
- },
32
- defaultOptions: [],
33
- create(context) {
34
- const parserServices = ESLintUtils.getParserServices(context);
35
- const typeChecker = parserServices.program.getTypeChecker();
36
-
37
- return {
38
- VariableDeclaration: (variableDeclaration: TSESTree.VariableDeclaration) => {
39
- const variableInit = variableDeclaration.declarations[0]?.init;
40
- if (
41
- !variableInit ||
42
- variableInit.type !== AST_NODE_TYPES.AwaitExpression ||
43
- variableInit.argument.type !== AST_NODE_TYPES.CallExpression
44
- ) {
45
- return;
46
- }
47
-
48
- const variableId = variableDeclaration.declarations[0]?.id;
49
- if (variableId.type !== AST_NODE_TYPES.ObjectPattern) {
50
- return;
51
- }
52
- const statusCodeProperty = variableId.properties.find<TSESTree.Property>(
53
- (property): property is TSESTree.Property =>
54
- property.type === AST_NODE_TYPES.Property &&
55
- property.key.type === AST_NODE_TYPES.Identifier &&
56
- property.key.name === 'statusCode',
57
- );
58
- if (!statusCodeProperty) {
59
- return;
60
- }
61
-
62
- if (
63
- variableInit.argument.callee.type !== AST_NODE_TYPES.Identifier ||
64
- variableInit.argument.callee.name !== 'fetch'
65
- ) {
66
- const variableNode = parserServices.esTreeNodeToTSNodeMap.get(variableId);
67
- const variableType = typeChecker.getTypeAtLocation(variableNode);
68
- if (!isFetchResponse(variableType)) {
69
- return;
70
- }
71
- }
72
-
73
- try {
74
- context.report({
75
- node: statusCodeProperty,
76
- messageId: 'renameStatusCodeProperty',
77
- fix(fixer) {
78
- return statusCodeProperty.shorthand
79
- ? fixer.replaceText(statusCodeProperty, 'status: statusCode')
80
- : fixer.replaceText(statusCodeProperty.key, 'status');
81
- },
82
- });
83
- } catch (error) {
84
- // eslint-disable-next-line no-console
85
- console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
86
- context.report({
87
- node: statusCodeProperty,
88
- messageId: 'unknownError',
89
- data: {
90
- fileName: context.filename,
91
- error: error instanceof Error ? error.toString() : JSON.stringify(error),
92
- },
93
- });
94
- }
95
- },
96
- };
97
- },
98
- });
99
-
100
- export default rule;