@checkdigit/eslint-plugin 7.6.0-PR.75-4751 → 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.
- 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,103 +0,0 @@
|
|
|
1
|
-
// agent/no-unused-imports.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 { ESLintUtils, TSESTree } from '@typescript-eslint/utils';
|
|
10
|
-
import type { Scope } from '@typescript-eslint/utils/ts-eslint';
|
|
11
|
-
|
|
12
|
-
import getDocumentationUrl from '../get-documentation-url';
|
|
13
|
-
|
|
14
|
-
export const ruleId = 'no-unused-imports';
|
|
15
|
-
|
|
16
|
-
const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
|
|
17
|
-
|
|
18
|
-
const rule: ESLintUtils.RuleModule<'unknownError' | 'removeUnusedImports'> = createRule({
|
|
19
|
-
name: ruleId,
|
|
20
|
-
meta: {
|
|
21
|
-
type: 'suggestion',
|
|
22
|
-
docs: {
|
|
23
|
-
description: 'Remove unused imports.',
|
|
24
|
-
},
|
|
25
|
-
messages: {
|
|
26
|
-
removeUnusedImports: 'Removing unused imports.',
|
|
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
|
-
function isImportUsed(specifier: TSESTree.ImportClause, scope: Scope.Scope): boolean {
|
|
37
|
-
return (
|
|
38
|
-
specifier.type !== TSESTree.AST_NODE_TYPES.ImportSpecifier ||
|
|
39
|
-
scope.references.some((ref) => ref.identifier.name === specifier.local.name) ||
|
|
40
|
-
scope.childScopes.some((childScope) => isImportUsed(specifier, childScope))
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return {
|
|
45
|
-
ImportDeclaration(importDeclaration) {
|
|
46
|
-
try {
|
|
47
|
-
const moduleName = importDeclaration.source.value;
|
|
48
|
-
if (
|
|
49
|
-
!importDeclaration.specifiers.every(
|
|
50
|
-
(specifier) => specifier.type === TSESTree.AST_NODE_TYPES.ImportSpecifier,
|
|
51
|
-
) ||
|
|
52
|
-
// [TODO:] move to meta schema
|
|
53
|
-
!['@checkdigit/serve-runtime', '@checkdigit/fixture'].includes(moduleName)
|
|
54
|
-
) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const originalSpecifiers = importDeclaration.specifiers;
|
|
59
|
-
const scope = sourceCode.getScope(importDeclaration);
|
|
60
|
-
const usedSpecifiers = originalSpecifiers.filter((specifier) => isImportUsed(specifier, scope));
|
|
61
|
-
if (usedSpecifiers.length === originalSpecifiers.length) {
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (usedSpecifiers.length === 0) {
|
|
66
|
-
context.report({
|
|
67
|
-
messageId: 'removeUnusedImports',
|
|
68
|
-
node: importDeclaration,
|
|
69
|
-
*fix(fixer) {
|
|
70
|
-
yield fixer.remove(importDeclaration);
|
|
71
|
-
},
|
|
72
|
-
});
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const usedSpecifierTexts = usedSpecifiers.map((specifier) => sourceCode.getText(specifier));
|
|
77
|
-
const updatedImportDeclaration = `import ${importDeclaration.importKind === 'type' ? 'type ' : ''}{ ${usedSpecifierTexts.join(', ')} } from '${moduleName}';`;
|
|
78
|
-
|
|
79
|
-
context.report({
|
|
80
|
-
messageId: 'removeUnusedImports',
|
|
81
|
-
node: importDeclaration,
|
|
82
|
-
*fix(fixer) {
|
|
83
|
-
yield fixer.replaceText(importDeclaration, updatedImportDeclaration);
|
|
84
|
-
},
|
|
85
|
-
});
|
|
86
|
-
} catch (error) {
|
|
87
|
-
// eslint-disable-next-line no-console
|
|
88
|
-
console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
|
|
89
|
-
context.report({
|
|
90
|
-
node: importDeclaration,
|
|
91
|
-
messageId: 'unknownError',
|
|
92
|
-
data: {
|
|
93
|
-
fileName: context.filename,
|
|
94
|
-
error: error instanceof Error ? error.toString() : JSON.stringify(error),
|
|
95
|
-
},
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
},
|
|
99
|
-
};
|
|
100
|
-
},
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
export default rule;
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
// agent/no-unused-service-variable.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 { ESLintUtils, TSESTree } from '@typescript-eslint/utils';
|
|
12
|
-
import type { Scope } from '@typescript-eslint/utils/ts-eslint';
|
|
13
|
-
|
|
14
|
-
import getDocumentationUrl from '../get-documentation-url';
|
|
15
|
-
import { getEnclosingScopeNode } from '../library/ts-tree';
|
|
16
|
-
|
|
17
|
-
export const ruleId = 'no-unused-service-variable';
|
|
18
|
-
|
|
19
|
-
const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
|
|
20
|
-
|
|
21
|
-
const rule: ESLintUtils.RuleModule<'unknownError' | 'removeUnusedServiceVariables'> = createRule({
|
|
22
|
-
name: ruleId,
|
|
23
|
-
meta: {
|
|
24
|
-
type: 'suggestion',
|
|
25
|
-
docs: {
|
|
26
|
-
description: 'Remove unused service variables.',
|
|
27
|
-
},
|
|
28
|
-
messages: {
|
|
29
|
-
removeUnusedServiceVariables: 'Removing unused service variables.',
|
|
30
|
-
unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}.',
|
|
31
|
-
},
|
|
32
|
-
fixable: 'code',
|
|
33
|
-
schema: [],
|
|
34
|
-
},
|
|
35
|
-
defaultOptions: [],
|
|
36
|
-
create(context) {
|
|
37
|
-
const sourceCode = context.sourceCode;
|
|
38
|
-
const scopeManager = sourceCode.scopeManager;
|
|
39
|
-
|
|
40
|
-
function isVariableUsed(variableIdentifier: TSESTree.Identifier, scope: Scope.Scope): boolean {
|
|
41
|
-
const variable = scope.variables.find((variableToCheck) => variableToCheck.name === variableIdentifier.name);
|
|
42
|
-
return variable !== undefined && variable.references.length > 1;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return {
|
|
46
|
-
VariableDeclaration(variableDeclaration: TSESTree.VariableDeclaration) {
|
|
47
|
-
try {
|
|
48
|
-
if (
|
|
49
|
-
variableDeclaration.declarations.length !== 1 ||
|
|
50
|
-
!sourceCode.getText(variableDeclaration).includes('.service.')
|
|
51
|
-
) {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const enclosingScopeNode = getEnclosingScopeNode(variableDeclaration);
|
|
56
|
-
assert.ok(enclosingScopeNode, 'enclosingScopeNode is undefined');
|
|
57
|
-
|
|
58
|
-
const declarator = variableDeclaration.declarations[0];
|
|
59
|
-
if (declarator.id.type !== TSESTree.AST_NODE_TYPES.Identifier) {
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const scope = scopeManager?.acquire(enclosingScopeNode);
|
|
64
|
-
assert.ok(scope, 'variable declaration is undefined');
|
|
65
|
-
if (isVariableUsed(declarator.id, scope)) {
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
context.report({
|
|
70
|
-
node: variableDeclaration,
|
|
71
|
-
messageId: 'removeUnusedServiceVariables',
|
|
72
|
-
fix(fixer) {
|
|
73
|
-
return fixer.remove(variableDeclaration);
|
|
74
|
-
},
|
|
75
|
-
});
|
|
76
|
-
} catch (error) {
|
|
77
|
-
// eslint-disable-next-line no-console
|
|
78
|
-
console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
|
|
79
|
-
context.report({
|
|
80
|
-
node: variableDeclaration,
|
|
81
|
-
messageId: 'unknownError',
|
|
82
|
-
data: {
|
|
83
|
-
fileName: context.filename,
|
|
84
|
-
error: error instanceof Error ? error.toString() : JSON.stringify(error),
|
|
85
|
-
},
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
},
|
|
89
|
-
};
|
|
90
|
-
},
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
export default rule;
|
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
// agent/response-reference.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 debug from 'debug';
|
|
12
|
-
|
|
13
|
-
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
|
|
14
|
-
import type { ScopeManager, Variable } from '@typescript-eslint/scope-manager';
|
|
15
|
-
import { getParent } from '../library/ts-tree';
|
|
16
|
-
|
|
17
|
-
const log = debug('eslint-plugin:response-reference');
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* analyze response related variables and their references
|
|
21
|
-
* the implementation is for fixture API, but it can be used for fetch API as well since the tree structure is similar
|
|
22
|
-
* @param variableDeclaration - variable declaration node
|
|
23
|
-
*/
|
|
24
|
-
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
25
|
-
export function analyzeResponseReferences(
|
|
26
|
-
variableDeclaration: TSESTree.VariableDeclaration | undefined,
|
|
27
|
-
scopeManager: ScopeManager,
|
|
28
|
-
): {
|
|
29
|
-
variable?: Variable;
|
|
30
|
-
bodyReferences: TSESTree.MemberExpression[];
|
|
31
|
-
// headersReferences: TSESTree.MemberExpression[];
|
|
32
|
-
statusReferences: TSESTree.MemberExpression[];
|
|
33
|
-
destructuringBodyVariable?: Variable | TSESTree.ObjectPattern;
|
|
34
|
-
destructuringHeadersVariable?: Variable | TSESTree.ObjectPattern;
|
|
35
|
-
destructuringStatusVariable?: Variable | TSESTree.ObjectPattern;
|
|
36
|
-
destructuringHeadersReferences?: TSESTree.MemberExpression[] | undefined;
|
|
37
|
-
} {
|
|
38
|
-
const results: {
|
|
39
|
-
variable?: Variable;
|
|
40
|
-
bodyReferences: TSESTree.MemberExpression[];
|
|
41
|
-
// headersReferences: TSESTree.MemberExpression[];
|
|
42
|
-
statusReferences: TSESTree.MemberExpression[];
|
|
43
|
-
destructuringBodyVariable?: Variable | TSESTree.ObjectPattern;
|
|
44
|
-
destructuringHeadersVariable?: Variable | TSESTree.ObjectPattern;
|
|
45
|
-
destructuringStatusVariable?: Variable | TSESTree.ObjectPattern;
|
|
46
|
-
destructuringHeadersReferences?: TSESTree.MemberExpression[] | undefined;
|
|
47
|
-
} = {
|
|
48
|
-
bodyReferences: [],
|
|
49
|
-
// headersReferences: [],
|
|
50
|
-
statusReferences: [],
|
|
51
|
-
};
|
|
52
|
-
if (!variableDeclaration) {
|
|
53
|
-
return results;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const responseVariables = scopeManager.getDeclaredVariables(variableDeclaration);
|
|
57
|
-
for (const responseVariable of responseVariables) {
|
|
58
|
-
const identifier = responseVariable.identifiers[0];
|
|
59
|
-
assert.ok(identifier);
|
|
60
|
-
const identifierParent = getParent(identifier);
|
|
61
|
-
assert.ok(identifierParent);
|
|
62
|
-
if (identifierParent.type === AST_NODE_TYPES.VariableDeclarator) {
|
|
63
|
-
// e.g. const response = ...
|
|
64
|
-
results.variable = responseVariable;
|
|
65
|
-
const responseReferences = responseVariable.references.map((responseReference) =>
|
|
66
|
-
getParent(responseReference.identifier),
|
|
67
|
-
);
|
|
68
|
-
// e.g. response.body
|
|
69
|
-
results.bodyReferences = responseReferences.filter(
|
|
70
|
-
(node): node is TSESTree.MemberExpression =>
|
|
71
|
-
node?.type === AST_NODE_TYPES.MemberExpression &&
|
|
72
|
-
node.property.type === AST_NODE_TYPES.Identifier &&
|
|
73
|
-
node.property.name === 'body',
|
|
74
|
-
);
|
|
75
|
-
// // e.g. response.headers / response.header / response.get()
|
|
76
|
-
// results.headersReferences = responseReferences.filter(
|
|
77
|
-
// (node): node is TSESTree.MemberExpression =>
|
|
78
|
-
// node?.type === AST_NODE_TYPES.MemberExpression &&
|
|
79
|
-
// node.property.type === AST_NODE_TYPES.Identifier &&
|
|
80
|
-
// (node.property.name === 'header' || node.property.name === 'headers' || node.property.name === 'get'),
|
|
81
|
-
// );
|
|
82
|
-
// e.g. response.status / response.statusCode
|
|
83
|
-
results.statusReferences = responseReferences.filter(
|
|
84
|
-
(node): node is TSESTree.MemberExpression =>
|
|
85
|
-
node?.type === AST_NODE_TYPES.MemberExpression &&
|
|
86
|
-
node.property.type === AST_NODE_TYPES.Identifier &&
|
|
87
|
-
(node.property.name === 'status' || node.property.name === 'statusCode'),
|
|
88
|
-
);
|
|
89
|
-
} else if (
|
|
90
|
-
// body reference through destruction/renaming, e.g. "const { body } = ..."
|
|
91
|
-
identifierParent.type === AST_NODE_TYPES.Property &&
|
|
92
|
-
identifierParent.key.type === AST_NODE_TYPES.Identifier &&
|
|
93
|
-
identifierParent.key.name === 'body'
|
|
94
|
-
) {
|
|
95
|
-
results.destructuringBodyVariable = responseVariable;
|
|
96
|
-
} else if (
|
|
97
|
-
// body reference through destruction/renaming, e.g. "const { body } = ..."
|
|
98
|
-
identifierParent.type === AST_NODE_TYPES.Property &&
|
|
99
|
-
identifierParent.key.type === AST_NODE_TYPES.Identifier &&
|
|
100
|
-
(identifierParent.key.name === 'status' || identifierParent.key.name === 'statusCode')
|
|
101
|
-
) {
|
|
102
|
-
results.destructuringStatusVariable = responseVariable;
|
|
103
|
-
} else if (
|
|
104
|
-
// header reference through destruction/renaming, e.g. "const { headers } = ..."
|
|
105
|
-
identifierParent.type === AST_NODE_TYPES.Property &&
|
|
106
|
-
identifierParent.key.type === AST_NODE_TYPES.Identifier &&
|
|
107
|
-
identifierParent.key.name === 'headers'
|
|
108
|
-
) {
|
|
109
|
-
results.destructuringHeadersVariable = responseVariable;
|
|
110
|
-
results.destructuringHeadersReferences = responseVariable.references
|
|
111
|
-
.map((reference) => reference.identifier)
|
|
112
|
-
.map(getParent)
|
|
113
|
-
.filter(
|
|
114
|
-
(parent): parent is TSESTree.MemberExpression =>
|
|
115
|
-
parent?.type === AST_NODE_TYPES.MemberExpression &&
|
|
116
|
-
parent.property.type === AST_NODE_TYPES.Identifier &&
|
|
117
|
-
parent.property.name !== 'get' &&
|
|
118
|
-
getParent(parent)?.type !== AST_NODE_TYPES.CallExpression,
|
|
119
|
-
);
|
|
120
|
-
} else if (identifierParent.type === AST_NODE_TYPES.Property) {
|
|
121
|
-
const parent = getParent(identifierParent);
|
|
122
|
-
if (parent?.type === AST_NODE_TYPES.ObjectPattern) {
|
|
123
|
-
// body reference through nested destruction, e.g. "const { body: {bodyPropertyName: renamedBodyPropertyName}, headers: {headerPropertyName: renamedHeaderPropertyName} } = ..."
|
|
124
|
-
const parent2 = getParent(parent);
|
|
125
|
-
if (
|
|
126
|
-
parent2?.type === AST_NODE_TYPES.Property &&
|
|
127
|
-
parent2.key.type === AST_NODE_TYPES.Identifier &&
|
|
128
|
-
parent2.key.name === 'body'
|
|
129
|
-
) {
|
|
130
|
-
results.destructuringBodyVariable = parent;
|
|
131
|
-
}
|
|
132
|
-
if (
|
|
133
|
-
parent2?.type === AST_NODE_TYPES.Property &&
|
|
134
|
-
parent2.key.type === AST_NODE_TYPES.Identifier &&
|
|
135
|
-
(parent2.key.name === 'status' || parent2.key.name === 'statusCode')
|
|
136
|
-
) {
|
|
137
|
-
results.destructuringStatusVariable = parent;
|
|
138
|
-
}
|
|
139
|
-
if (
|
|
140
|
-
parent2?.type === AST_NODE_TYPES.Property &&
|
|
141
|
-
parent2.key.type === AST_NODE_TYPES.Identifier &&
|
|
142
|
-
(parent2.key.name === 'header' || parent2.key.name === 'headers')
|
|
143
|
-
) {
|
|
144
|
-
results.destructuringHeadersVariable = parent;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
} else {
|
|
148
|
-
log('+++++++ can not handle identifierParent', identifierParent);
|
|
149
|
-
throw new Error(`Unknown response variable reference: ${responseVariable.name}`);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
return results;
|
|
153
|
-
}
|
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
// agent/supertest-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 { AST_NODE_TYPES, ESLintUtils, TSESTree } from '@typescript-eslint/utils';
|
|
12
|
-
import type { SourceCode } from '@typescript-eslint/utils/ts-eslint';
|
|
13
|
-
|
|
14
|
-
import { getEnclosingFunction, getParent, isUsedInArrayOrAsArgument } from '../library/ts-tree';
|
|
15
|
-
import getDocumentationUrl from '../get-documentation-url';
|
|
16
|
-
import { getIndentation } from '../library/format';
|
|
17
|
-
|
|
18
|
-
export const ruleId = 'supertest-then';
|
|
19
|
-
|
|
20
|
-
interface FixtureCallInformation {
|
|
21
|
-
fixtureNode: TSESTree.CallExpression;
|
|
22
|
-
requestBody?: TSESTree.Expression;
|
|
23
|
-
requestHeaders?: { name: TSESTree.Expression; value: TSESTree.Expression }[];
|
|
24
|
-
assertions?: TSESTree.Expression[][];
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// recursively analyze the fixture/supertest call chain to collect information of request/response
|
|
28
|
-
function analyzeFixtureCall(call: TSESTree.CallExpression, results: FixtureCallInformation, sourceCode: SourceCode) {
|
|
29
|
-
const parent = getParent(call);
|
|
30
|
-
if (!parent) {
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
let nextCall;
|
|
35
|
-
if (parent.type !== AST_NODE_TYPES.MemberExpression) {
|
|
36
|
-
results.fixtureNode = call;
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (parent.property.type === AST_NODE_TYPES.Identifier) {
|
|
41
|
-
if (parent.property.name === 'expect') {
|
|
42
|
-
// supertest assertions
|
|
43
|
-
const assertionCall = getParent(parent);
|
|
44
|
-
assert.ok(assertionCall && assertionCall.type === AST_NODE_TYPES.CallExpression);
|
|
45
|
-
results.assertions = [...(results.assertions ?? []), assertionCall.arguments as TSESTree.Expression[]];
|
|
46
|
-
nextCall = assertionCall;
|
|
47
|
-
}
|
|
48
|
-
} else {
|
|
49
|
-
throw new Error(`Unexpected TSESTree.Expression in fixture/supertest call ${sourceCode.getText(parent)}.`);
|
|
50
|
-
}
|
|
51
|
-
if (nextCall) {
|
|
52
|
-
analyzeFixtureCall(nextCall, results, sourceCode);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
57
|
-
function createResponseAssertions(
|
|
58
|
-
fixtureCallInformation: FixtureCallInformation,
|
|
59
|
-
sourceCode: SourceCode,
|
|
60
|
-
responseVariableName: string,
|
|
61
|
-
) {
|
|
62
|
-
let statusAssertion: string | undefined;
|
|
63
|
-
const nonStatusAssertions: string[] = [];
|
|
64
|
-
for (const expectArguments of fixtureCallInformation.assertions ?? []) {
|
|
65
|
-
if (expectArguments.length === 1) {
|
|
66
|
-
const [assertionArgument] = expectArguments;
|
|
67
|
-
assert.ok(assertionArgument);
|
|
68
|
-
if (
|
|
69
|
-
(assertionArgument.type === AST_NODE_TYPES.MemberExpression &&
|
|
70
|
-
assertionArgument.object.type === AST_NODE_TYPES.Identifier &&
|
|
71
|
-
assertionArgument.object.name === 'StatusCodes') ||
|
|
72
|
-
assertionArgument.type === AST_NODE_TYPES.Literal ||
|
|
73
|
-
sourceCode.getText(assertionArgument).includes('StatusCodes.')
|
|
74
|
-
) {
|
|
75
|
-
// status code assertion
|
|
76
|
-
statusAssertion = `assert.equal(${responseVariableName}.status, ${sourceCode.getText(assertionArgument)})`;
|
|
77
|
-
} else if (assertionArgument.type === AST_NODE_TYPES.ArrowFunctionExpression) {
|
|
78
|
-
// callback assertion using arrow function
|
|
79
|
-
let functionBody = sourceCode.getText(assertionArgument.body);
|
|
80
|
-
|
|
81
|
-
const [originalResponseArgument] = assertionArgument.params;
|
|
82
|
-
assert.ok(originalResponseArgument?.type === AST_NODE_TYPES.Identifier);
|
|
83
|
-
const originalResponseArgumentName = originalResponseArgument.name;
|
|
84
|
-
if (originalResponseArgumentName !== responseVariableName) {
|
|
85
|
-
functionBody = functionBody.replace(
|
|
86
|
-
new RegExp(`\\b${originalResponseArgumentName}\\b`, 'ug'),
|
|
87
|
-
responseVariableName,
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
nonStatusAssertions.push(`assert.doesNotThrow(()=>${functionBody})`);
|
|
91
|
-
} else if (assertionArgument.type === AST_NODE_TYPES.Identifier) {
|
|
92
|
-
// callback assertion using function reference
|
|
93
|
-
nonStatusAssertions.push(
|
|
94
|
-
`assert.doesNotThrow(()=>${sourceCode.getText(assertionArgument)}(${responseVariableName}))`,
|
|
95
|
-
);
|
|
96
|
-
} else if (
|
|
97
|
-
assertionArgument.type === AST_NODE_TYPES.ObjectExpression ||
|
|
98
|
-
assertionArgument.type === AST_NODE_TYPES.CallExpression
|
|
99
|
-
) {
|
|
100
|
-
// body deep equal assertion
|
|
101
|
-
nonStatusAssertions.push(
|
|
102
|
-
`assert.deepEqual(await ${responseVariableName}.json(), ${sourceCode.getText(assertionArgument)})`,
|
|
103
|
-
);
|
|
104
|
-
} else {
|
|
105
|
-
throw new Error(`Unexpected Supertest assertion argument: ".expect(${sourceCode.getText(assertionArgument)})`);
|
|
106
|
-
}
|
|
107
|
-
} else if (expectArguments.length === 2) {
|
|
108
|
-
// header assertion
|
|
109
|
-
const [headerName, headerValue] = expectArguments;
|
|
110
|
-
assert.ok(headerName && headerValue);
|
|
111
|
-
const headersReference = `${responseVariableName}.headers`;
|
|
112
|
-
if (headerValue.type === AST_NODE_TYPES.Literal && headerValue.value instanceof RegExp) {
|
|
113
|
-
nonStatusAssertions.push(
|
|
114
|
-
`assert.ok(${headersReference}.get(${sourceCode.getText(headerName)}).match(${sourceCode.getText(headerValue)}))`,
|
|
115
|
-
);
|
|
116
|
-
} else {
|
|
117
|
-
nonStatusAssertions.push(
|
|
118
|
-
`assert.equal(${headersReference}.get(${sourceCode.getText(headerName)}), ${sourceCode.getText(headerValue)})`,
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
return {
|
|
124
|
-
statusAssertion,
|
|
125
|
-
nonStatusAssertions,
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
|
|
130
|
-
const rule: ESLintUtils.RuleModule<
|
|
131
|
-
'unknownError' | 'preferNativeFetch'
|
|
132
|
-
// | 'shouldUseHeaderGetter'
|
|
133
|
-
> = createRule({
|
|
134
|
-
name: ruleId,
|
|
135
|
-
meta: {
|
|
136
|
-
type: 'suggestion',
|
|
137
|
-
docs: {
|
|
138
|
-
description: 'Prefer native fetch API over customized fixture API.',
|
|
139
|
-
url: getDocumentationUrl(ruleId),
|
|
140
|
-
},
|
|
141
|
-
messages: {
|
|
142
|
-
preferNativeFetch: 'Prefer native fetch API over customized fixture API.',
|
|
143
|
-
// shouldUseHeaderGetter: 'Getter should be used to access response headers.',
|
|
144
|
-
unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}.',
|
|
145
|
-
},
|
|
146
|
-
fixable: 'code',
|
|
147
|
-
schema: [],
|
|
148
|
-
},
|
|
149
|
-
defaultOptions: [],
|
|
150
|
-
create(context) {
|
|
151
|
-
const sourceCode = context.sourceCode;
|
|
152
|
-
const scopeManager = sourceCode.scopeManager;
|
|
153
|
-
assert.ok(scopeManager);
|
|
154
|
-
|
|
155
|
-
return {
|
|
156
|
-
'CallExpression[callee.property.name="expect"]': (supertestCall: TSESTree.CallExpression) => {
|
|
157
|
-
try {
|
|
158
|
-
assert.ok(supertestCall.callee.type === AST_NODE_TYPES.MemberExpression);
|
|
159
|
-
if (
|
|
160
|
-
supertestCall.callee.object.type !== AST_NODE_TYPES.CallExpression ||
|
|
161
|
-
(supertestCall.callee.object.callee.type === AST_NODE_TYPES.MemberExpression &&
|
|
162
|
-
supertestCall.callee.object.callee.property.type === AST_NODE_TYPES.Identifier &&
|
|
163
|
-
supertestCall.callee.object.callee.property.name === 'expect')
|
|
164
|
-
) {
|
|
165
|
-
// skip nested expect calls, only focus on the top level
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
if (!(isUsedInArrayOrAsArgument(supertestCall) || getEnclosingFunction(supertestCall)?.async === false)) {
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
const fixtureFunction = supertestCall.callee.object;
|
|
174
|
-
const indentation = getIndentation(supertestCall, sourceCode);
|
|
175
|
-
|
|
176
|
-
const [urlArgumentNode] = supertestCall.arguments; // e.g. `/sample-service/v1/ping`
|
|
177
|
-
assert.ok(urlArgumentNode !== undefined);
|
|
178
|
-
|
|
179
|
-
const fixtureCallInformation = {} as FixtureCallInformation;
|
|
180
|
-
analyzeFixtureCall(fixtureFunction, fixtureCallInformation, sourceCode);
|
|
181
|
-
|
|
182
|
-
const responseVariableNameToUse = 'res';
|
|
183
|
-
const { statusAssertion, nonStatusAssertions } = createResponseAssertions(
|
|
184
|
-
fixtureCallInformation,
|
|
185
|
-
sourceCode,
|
|
186
|
-
responseVariableNameToUse,
|
|
187
|
-
);
|
|
188
|
-
|
|
189
|
-
// add variable declaration if needed
|
|
190
|
-
const disableLintComment = '// eslint-disable-next-line @checkdigit/no-promise-instance-method';
|
|
191
|
-
const fetchCallText = sourceCode.getText(fixtureFunction);
|
|
192
|
-
const appendingAssignmentAndAssertionText = [
|
|
193
|
-
...(statusAssertion !== undefined ? [statusAssertion] : []),
|
|
194
|
-
...nonStatusAssertions,
|
|
195
|
-
].join(`;\n${indentation}`);
|
|
196
|
-
const replacementText = fixtureCallInformation.assertions
|
|
197
|
-
? [
|
|
198
|
-
disableLintComment,
|
|
199
|
-
`${fetchCallText}.then((${responseVariableNameToUse}) => {`,
|
|
200
|
-
appendingAssignmentAndAssertionText === '' ? '' : ` ${appendingAssignmentAndAssertionText};`,
|
|
201
|
-
` return ${responseVariableNameToUse};`,
|
|
202
|
-
`})`,
|
|
203
|
-
].join(`\n${indentation}`)
|
|
204
|
-
: fetchCallText;
|
|
205
|
-
|
|
206
|
-
context.report({
|
|
207
|
-
node: supertestCall,
|
|
208
|
-
messageId: 'preferNativeFetch',
|
|
209
|
-
fix(fixer) {
|
|
210
|
-
return fixer.replaceText(fixtureCallInformation.fixtureNode, replacementText);
|
|
211
|
-
},
|
|
212
|
-
});
|
|
213
|
-
} catch (error) {
|
|
214
|
-
// eslint-disable-next-line no-console
|
|
215
|
-
console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
|
|
216
|
-
context.report({
|
|
217
|
-
node: supertestCall,
|
|
218
|
-
messageId: 'unknownError',
|
|
219
|
-
data: {
|
|
220
|
-
fileName: context.filename,
|
|
221
|
-
error: error instanceof Error ? error.toString() : JSON.stringify(error),
|
|
222
|
-
},
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
},
|
|
226
|
-
};
|
|
227
|
-
},
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
export default rule;
|
package/src/agent/url.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
// agent/url.ts
|
|
2
|
-
|
|
3
|
-
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
|
|
4
|
-
const PLAIN_URL_REGEXP: RegExp = /^[`']\/\w+(?<serviceNamePart>-\w+)*\/v\d+\/(?<any>.|\r|\n)+[`']$/u;
|
|
5
|
-
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
|
|
6
|
-
const TOKENIZED_URL_REGEXP: RegExp = /^`\$\{(?<serviceNamePart>[A-Z]+_)*BASE_PATH\}\/(?<any>.|\r|\n)+`$/u;
|
|
7
|
-
|
|
8
|
-
export function isServiceApiCallUrl(url: string): boolean {
|
|
9
|
-
return PLAIN_URL_REGEXP.test(url) || TOKENIZED_URL_REGEXP.test(url);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function replaceEndpointUrlPrefixWithBasePath(url: string): string {
|
|
13
|
-
return url.replace(
|
|
14
|
-
/^(?<quotStart>[`'])\/(?<servicename>\w+(?<parts>-\w+)*)(?<path>\/v\d+\/)(?<endpoint>(?<any>.|\r|\n)+)(?<quotEnd>[`'])$/u,
|
|
15
|
-
// eslint-disable-next-line no-template-curly-in-string
|
|
16
|
-
'`${BASE_PATH}/$5`',
|
|
17
|
-
);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function replaceEndpointUrlPrefixWithDomain(url: string): string {
|
|
21
|
-
return url.replace(
|
|
22
|
-
/^(?<quotStart>[`'])\/(?<servicename>\w+(?<parts>-\w+)*)(?<path>\/v\d+\/(?<any>.|\r|\n)+(?<quotEnd>[`'])$)/u,
|
|
23
|
-
'$1https://$2.checkdigit/$2$4',
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function addBasePathUrlDomain(url: string): string {
|
|
28
|
-
return url.replace(
|
|
29
|
-
/^(?<quotStart>[`'])\/(?<servicename>\w+(?<parts>-\w+)*)(?<path>\/v\d+(?<quotEnd>[`'])$)/u,
|
|
30
|
-
'$1https://$2.checkdigit/$2$4',
|
|
31
|
-
);
|
|
32
|
-
}
|