@checkdigit/eslint-plugin 7.5.0-PR.75-0252 → 7.5.0-PR.93-6f1c
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.
- package/dist-mjs/index.mjs +4 -146
- package/dist-mjs/require-fixed-services-import.mjs +1 -1
- package/dist-mjs/require-resolve-full-response.mjs +1 -1
- package/dist-types/index.d.ts +1 -1
- package/package.json +1 -1
- package/src/index.ts +2 -144
- package/src/require-fixed-services-import.ts +1 -1
- package/src/require-resolve-full-response.ts +0 -2
- package/dist-mjs/agent/add-assert-import.mjs +0 -58
- package/dist-mjs/agent/add-base-path-const.mjs +0 -65
- package/dist-mjs/agent/add-base-path-import.mjs +0 -60
- package/dist-mjs/agent/add-url-domain.mjs +0 -61
- package/dist-mjs/agent/agent-test-wiring.mjs +0 -221
- package/dist-mjs/agent/fetch-response-body-json.mjs +0 -146
- package/dist-mjs/agent/fetch-response-header-getter.mjs +0 -117
- package/dist-mjs/agent/fetch-response-status.mjs +0 -66
- package/dist-mjs/agent/fetch-then.mjs +0 -269
- package/dist-mjs/agent/fetch.mjs +0 -38
- package/dist-mjs/agent/file.mjs +0 -43
- package/dist-mjs/agent/fix-function-call-arguments.mjs +0 -153
- package/dist-mjs/agent/no-fixture.mjs +0 -361
- package/dist-mjs/agent/no-mapped-response.mjs +0 -75
- package/dist-mjs/agent/no-service-wrapper.mjs +0 -185
- package/dist-mjs/agent/no-status-code.mjs +0 -59
- package/dist-mjs/agent/no-unused-function-argument.mjs +0 -79
- package/dist-mjs/agent/no-unused-imports.mjs +0 -81
- package/dist-mjs/agent/no-unused-service-variable.mjs +0 -74
- package/dist-mjs/agent/response-reference.mjs +0 -70
- package/dist-mjs/agent/url.mjs +0 -32
- package/dist-types/agent/add-assert-import.d.ts +0 -4
- package/dist-types/agent/add-base-path-const.d.ts +0 -4
- package/dist-types/agent/add-base-path-import.d.ts +0 -4
- package/dist-types/agent/add-url-domain.d.ts +0 -4
- package/dist-types/agent/agent-test-wiring.d.ts +0 -4
- package/dist-types/agent/fetch-response-body-json.d.ts +0 -4
- package/dist-types/agent/fetch-response-header-getter.d.ts +0 -4
- package/dist-types/agent/fetch-response-status.d.ts +0 -4
- package/dist-types/agent/fetch-then.d.ts +0 -4
- package/dist-types/agent/fetch.d.ts +0 -5
- package/dist-types/agent/file.d.ts +0 -7
- package/dist-types/agent/fix-function-call-arguments.d.ts +0 -9
- package/dist-types/agent/no-fixture.d.ts +0 -4
- package/dist-types/agent/no-mapped-response.d.ts +0 -4
- package/dist-types/agent/no-service-wrapper.d.ts +0 -4
- package/dist-types/agent/no-status-code.d.ts +0 -4
- package/dist-types/agent/no-unused-function-argument.d.ts +0 -4
- package/dist-types/agent/no-unused-imports.d.ts +0 -4
- package/dist-types/agent/no-unused-service-variable.d.ts +0 -4
- package/dist-types/agent/response-reference.d.ts +0 -16
- package/dist-types/agent/url.d.ts +0 -4
- package/src/agent/add-assert-import.ts +0 -74
- package/src/agent/add-base-path-const.ts +0 -81
- package/src/agent/add-base-path-import.ts +0 -69
- package/src/agent/add-url-domain.ts +0 -76
- package/src/agent/agent-test-wiring.ts +0 -273
- package/src/agent/fetch-response-body-json.ts +0 -197
- package/src/agent/fetch-response-header-getter.ts +0 -148
- package/src/agent/fetch-response-status.ts +0 -87
- package/src/agent/fetch-then.ts +0 -357
- package/src/agent/fetch.ts +0 -57
- package/src/agent/file.ts +0 -42
- package/src/agent/fix-function-call-arguments.ts +0 -200
- package/src/agent/no-fixture.ts +0 -521
- package/src/agent/no-mapped-response.ts +0 -84
- package/src/agent/no-service-wrapper.ts +0 -241
- package/src/agent/no-status-code.ts +0 -72
- package/src/agent/no-unused-function-argument.ts +0 -98
- package/src/agent/no-unused-imports.ts +0 -103
- package/src/agent/no-unused-service-variable.ts +0 -93
- package/src/agent/response-reference.ts +0 -129
- package/src/agent/url.ts +0 -32
|
@@ -1,197 +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
|
-
|
|
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;
|
|
@@ -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,87 +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
|
-
|
|
13
|
-
export const ruleId = 'fetch-response-status';
|
|
14
|
-
|
|
15
|
-
const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
|
|
16
|
-
|
|
17
|
-
const rule: ESLintUtils.RuleModule<'unknownError' | 'renameStatusCodeProperty'> = createRule({
|
|
18
|
-
name: ruleId,
|
|
19
|
-
meta: {
|
|
20
|
-
type: 'problem',
|
|
21
|
-
docs: {
|
|
22
|
-
description: 'Replace "response.body" with "await response.json()".',
|
|
23
|
-
},
|
|
24
|
-
messages: {
|
|
25
|
-
renameStatusCodeProperty: 'Rename "statusCode" with "status".',
|
|
26
|
-
unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}.',
|
|
27
|
-
},
|
|
28
|
-
fixable: 'code',
|
|
29
|
-
schema: [],
|
|
30
|
-
},
|
|
31
|
-
defaultOptions: [],
|
|
32
|
-
create(context) {
|
|
33
|
-
return {
|
|
34
|
-
VariableDeclaration: (variableDeclaration: TSESTree.VariableDeclaration) => {
|
|
35
|
-
const variableInit = variableDeclaration.declarations[0]?.init;
|
|
36
|
-
if (
|
|
37
|
-
!variableInit ||
|
|
38
|
-
variableInit.type !== AST_NODE_TYPES.AwaitExpression ||
|
|
39
|
-
variableInit.argument.type !== AST_NODE_TYPES.CallExpression ||
|
|
40
|
-
variableInit.argument.callee.type !== AST_NODE_TYPES.Identifier ||
|
|
41
|
-
variableInit.argument.callee.name !== 'fetch'
|
|
42
|
-
) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const variableId = variableDeclaration.declarations[0]?.id;
|
|
47
|
-
if (variableId.type !== AST_NODE_TYPES.ObjectPattern) {
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
const statusCodeProperty = variableId.properties.find<TSESTree.Property>(
|
|
51
|
-
(property): property is TSESTree.Property =>
|
|
52
|
-
property.type === AST_NODE_TYPES.Property &&
|
|
53
|
-
property.key.type === AST_NODE_TYPES.Identifier &&
|
|
54
|
-
property.key.name === 'statusCode',
|
|
55
|
-
);
|
|
56
|
-
if (!statusCodeProperty) {
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
try {
|
|
61
|
-
context.report({
|
|
62
|
-
node: statusCodeProperty,
|
|
63
|
-
messageId: 'renameStatusCodeProperty',
|
|
64
|
-
fix(fixer) {
|
|
65
|
-
return statusCodeProperty.shorthand
|
|
66
|
-
? fixer.replaceText(statusCodeProperty, 'status: statusCode')
|
|
67
|
-
: fixer.replaceText(statusCodeProperty.key, 'status');
|
|
68
|
-
},
|
|
69
|
-
});
|
|
70
|
-
} catch (error) {
|
|
71
|
-
// eslint-disable-next-line no-console
|
|
72
|
-
console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
|
|
73
|
-
context.report({
|
|
74
|
-
node: statusCodeProperty,
|
|
75
|
-
messageId: 'unknownError',
|
|
76
|
-
data: {
|
|
77
|
-
fileName: context.filename,
|
|
78
|
-
error: error instanceof Error ? error.toString() : JSON.stringify(error),
|
|
79
|
-
},
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
|
-
};
|
|
84
|
-
},
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
export default rule;
|