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