@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.
- 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 -273
- 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 -397
- 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 -346
- 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 -78
- 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 -17
- 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 -359
- 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,17 +0,0 @@
|
|
|
1
|
-
import { TSESTree } from '@typescript-eslint/utils';
|
|
2
|
-
import type { ScopeManager, Variable } from '@typescript-eslint/scope-manager';
|
|
3
|
-
/**
|
|
4
|
-
* analyze response related variables and their references
|
|
5
|
-
* the implementation is for fixture API, but it can be used for fetch API as well since the tree structure is similar
|
|
6
|
-
* @param variableDeclaration - variable declaration node
|
|
7
|
-
*/
|
|
8
|
-
export declare function analyzeResponseReferences(variableDeclaration: TSESTree.VariableDeclaration | undefined, scopeManager: ScopeManager): {
|
|
9
|
-
variable?: Variable;
|
|
10
|
-
bodyReferences: TSESTree.MemberExpression[];
|
|
11
|
-
headersReferences: TSESTree.MemberExpression[];
|
|
12
|
-
statusReferences: TSESTree.MemberExpression[];
|
|
13
|
-
destructuringBodyVariable?: Variable | TSESTree.ObjectPattern;
|
|
14
|
-
destructuringHeadersVariable?: Variable | TSESTree.ObjectPattern;
|
|
15
|
-
destructuringStatusVariable?: Variable | TSESTree.ObjectPattern;
|
|
16
|
-
destructuringHeadersReferences?: TSESTree.MemberExpression[] | undefined;
|
|
17
|
-
};
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
export declare function isServiceApiCallUrl(url: string): boolean;
|
|
2
|
-
export declare function replaceEndpointUrlPrefixWithBasePath(url: string): string;
|
|
3
|
-
export declare function replaceEndpointUrlPrefixWithDomain(url: string): string;
|
|
4
|
-
export declare function addBasePathUrlDomain(url: string): string;
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
// agent/add-url-domain.ts
|
|
2
|
-
|
|
3
|
-
/*
|
|
4
|
-
* Copyright (c) 2021-2024 Check Digit, LLC
|
|
5
|
-
*
|
|
6
|
-
* This code is licensed under the MIT license (see LICENSE.txt for details).
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { strict as assert } from 'node:assert';
|
|
10
|
-
|
|
11
|
-
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils';
|
|
12
|
-
|
|
13
|
-
import getDocumentationUrl from '../get-documentation-url';
|
|
14
|
-
|
|
15
|
-
export const ruleId = 'add-assert-import';
|
|
16
|
-
|
|
17
|
-
const ASSERT_IMPORT_STATEMENT = "import { strict as assert } from 'node:assert';";
|
|
18
|
-
|
|
19
|
-
const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
|
|
20
|
-
|
|
21
|
-
const rule: ESLintUtils.RuleModule<'addAssertImport'> = createRule({
|
|
22
|
-
name: ruleId,
|
|
23
|
-
meta: {
|
|
24
|
-
type: 'suggestion',
|
|
25
|
-
docs: {
|
|
26
|
-
description: 'Add import of assert module of node.',
|
|
27
|
-
},
|
|
28
|
-
messages: {
|
|
29
|
-
addAssertImport: 'Add import of assert module of node.',
|
|
30
|
-
},
|
|
31
|
-
fixable: 'code',
|
|
32
|
-
schema: [],
|
|
33
|
-
},
|
|
34
|
-
defaultOptions: [],
|
|
35
|
-
create(context) {
|
|
36
|
-
let isAssertImported = false;
|
|
37
|
-
let isAssertUsed = false;
|
|
38
|
-
|
|
39
|
-
return {
|
|
40
|
-
ImportDeclaration: (node) => {
|
|
41
|
-
if (node.source.value === 'assert' || node.source.value === 'node:assert') {
|
|
42
|
-
isAssertImported = true;
|
|
43
|
-
}
|
|
44
|
-
},
|
|
45
|
-
CallExpression: (callExpression) => {
|
|
46
|
-
// detect if assert is used
|
|
47
|
-
if (
|
|
48
|
-
(callExpression.callee.type === AST_NODE_TYPES.Identifier && callExpression.callee.name === 'assert') ||
|
|
49
|
-
(callExpression.callee.type === AST_NODE_TYPES.MemberExpression &&
|
|
50
|
-
callExpression.callee.object.type === AST_NODE_TYPES.Identifier &&
|
|
51
|
-
callExpression.callee.object.name === 'assert')
|
|
52
|
-
) {
|
|
53
|
-
isAssertUsed = true;
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
'Program:exit': (program) => {
|
|
57
|
-
// add assert import if necessary
|
|
58
|
-
if (isAssertUsed && !isAssertImported) {
|
|
59
|
-
const firstStatement = program.body[0];
|
|
60
|
-
assert(firstStatement);
|
|
61
|
-
context.report({
|
|
62
|
-
node: program,
|
|
63
|
-
messageId: 'addAssertImport',
|
|
64
|
-
fix(fixer) {
|
|
65
|
-
return fixer.insertTextBefore(firstStatement, `${ASSERT_IMPORT_STATEMENT}\n`);
|
|
66
|
-
},
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
};
|
|
71
|
-
},
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
export default rule;
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
// agent/add-url-domain.ts
|
|
2
|
-
|
|
3
|
-
/*
|
|
4
|
-
* Copyright (c) 2021-2024 Check Digit, LLC
|
|
5
|
-
*
|
|
6
|
-
* This code is licensed under the MIT license (see LICENSE.txt for details).
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { 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 { getProjectRootFolder, getSwaggerPathByIndexFile, isApiIndexFile, loadPackageJson, loadSwagger } from './file';
|
|
15
|
-
|
|
16
|
-
export const ruleId = 'add-base-path-const';
|
|
17
|
-
|
|
18
|
-
const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
|
|
19
|
-
|
|
20
|
-
const rule: ESLintUtils.RuleModule<'addBasePathConst'> = createRule({
|
|
21
|
-
name: ruleId,
|
|
22
|
-
meta: {
|
|
23
|
-
type: 'suggestion',
|
|
24
|
-
docs: {
|
|
25
|
-
description: 'Add BASE_PATH const variable.',
|
|
26
|
-
},
|
|
27
|
-
messages: {
|
|
28
|
-
addBasePathConst: 'Add BASE_PATH const variable.',
|
|
29
|
-
},
|
|
30
|
-
fixable: 'code',
|
|
31
|
-
schema: [],
|
|
32
|
-
},
|
|
33
|
-
defaultOptions: [],
|
|
34
|
-
create(context) {
|
|
35
|
-
const sourceCode = context.sourceCode;
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
Program: (program: TSESTree.Program) => {
|
|
39
|
-
if (!isApiIndexFile(context.filename)) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const scope = sourceCode.getScope(program).childScopes[0];
|
|
44
|
-
assert(scope);
|
|
45
|
-
|
|
46
|
-
const foundBasePathConst = scope.variables.find((variable) => variable.name === 'BASE_PATH');
|
|
47
|
-
if (foundBasePathConst) {
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const swaggerPath = getSwaggerPathByIndexFile(context.filename);
|
|
52
|
-
const swaggerFileContents = loadSwagger(swaggerPath);
|
|
53
|
-
const baseUrlLine = swaggerFileContents
|
|
54
|
-
.split('\n')
|
|
55
|
-
.find((line) => /^\s*-\s*url:\s*\/.*$/u.test(line) || /^basePath:.*/u.test(line));
|
|
56
|
-
const baseUrl = baseUrlLine?.split(':')[1]?.trim();
|
|
57
|
-
assert(baseUrl !== undefined);
|
|
58
|
-
|
|
59
|
-
const packageRoot = getProjectRootFolder(context.filename);
|
|
60
|
-
const packageJson = JSON.parse(loadPackageJson(packageRoot)) as { name: string };
|
|
61
|
-
const serviceName = packageJson.name.split('/')[1];
|
|
62
|
-
assert(serviceName !== undefined);
|
|
63
|
-
|
|
64
|
-
const domain = `https://${serviceName}.checkdigit${baseUrl}`;
|
|
65
|
-
|
|
66
|
-
const lastImportStatement = program.body.findLast((node) => node.type === AST_NODE_TYPES.ImportDeclaration);
|
|
67
|
-
assert(lastImportStatement);
|
|
68
|
-
|
|
69
|
-
context.report({
|
|
70
|
-
messageId: 'addBasePathConst',
|
|
71
|
-
node: program,
|
|
72
|
-
fix(fixer) {
|
|
73
|
-
return fixer.insertTextAfter(lastImportStatement, `\nexport const BASE_PATH = '${domain}';\n`);
|
|
74
|
-
},
|
|
75
|
-
});
|
|
76
|
-
},
|
|
77
|
-
};
|
|
78
|
-
},
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
export default rule;
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
// agent/add-url-domain.ts
|
|
2
|
-
|
|
3
|
-
/*
|
|
4
|
-
* Copyright (c) 2021-2024 Check Digit, LLC
|
|
5
|
-
*
|
|
6
|
-
* This code is licensed under the MIT license (see LICENSE.txt for details).
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { strict as assert } from 'node:assert';
|
|
10
|
-
|
|
11
|
-
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils';
|
|
12
|
-
|
|
13
|
-
import getDocumentationUrl from '../get-documentation-url';
|
|
14
|
-
import { getApiIndexPathByFilename } from './file';
|
|
15
|
-
|
|
16
|
-
export const ruleId = 'add-base-path-import';
|
|
17
|
-
|
|
18
|
-
const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
|
|
19
|
-
|
|
20
|
-
const rule: ESLintUtils.RuleModule<'addBasePathImport'> = createRule({
|
|
21
|
-
name: ruleId,
|
|
22
|
-
meta: {
|
|
23
|
-
type: 'suggestion',
|
|
24
|
-
docs: {
|
|
25
|
-
description: 'Add import of BASE_PATH if it is used but not imported.',
|
|
26
|
-
},
|
|
27
|
-
messages: {
|
|
28
|
-
addBasePathImport: 'Add import of BASE_PATH.',
|
|
29
|
-
},
|
|
30
|
-
fixable: 'code',
|
|
31
|
-
schema: [],
|
|
32
|
-
},
|
|
33
|
-
defaultOptions: [],
|
|
34
|
-
create(context) {
|
|
35
|
-
const sourceCode = context.sourceCode;
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
Program: (program) => {
|
|
39
|
-
const isBasePathUsed = sourceCode.text.includes(`$\{BASE_PATH}`);
|
|
40
|
-
if (isBasePathUsed) {
|
|
41
|
-
const topScope = sourceCode.getScope(program).childScopes[0];
|
|
42
|
-
assert(topScope);
|
|
43
|
-
if (topScope.variables.some((variable) => variable.name === 'BASE_PATH')) {
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const apiIndexPath = getApiIndexPathByFilename(context.filename);
|
|
48
|
-
if (apiIndexPath !== undefined) {
|
|
49
|
-
const lastImportStatement = program.body.findLast(
|
|
50
|
-
(statement) => statement.type === AST_NODE_TYPES.ImportDeclaration,
|
|
51
|
-
);
|
|
52
|
-
assert(lastImportStatement);
|
|
53
|
-
|
|
54
|
-
const basePathImportStatement = `\nimport { BASE_PATH } from '${apiIndexPath}';\n`;
|
|
55
|
-
context.report({
|
|
56
|
-
node: program,
|
|
57
|
-
messageId: 'addBasePathImport',
|
|
58
|
-
fix(fixer) {
|
|
59
|
-
return fixer.insertTextAfter(lastImportStatement, basePathImportStatement);
|
|
60
|
-
},
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
},
|
|
65
|
-
};
|
|
66
|
-
},
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
export default rule;
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
// agent/add-url-domain.ts
|
|
2
|
-
|
|
3
|
-
/*
|
|
4
|
-
* Copyright (c) 2021-2024 Check Digit, LLC
|
|
5
|
-
*
|
|
6
|
-
* This code is licensed under the MIT license (see LICENSE.txt for details).
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { AST_NODE_TYPES, ESLintUtils, TSESTree } from '@typescript-eslint/utils';
|
|
10
|
-
|
|
11
|
-
import getDocumentationUrl from '../get-documentation-url';
|
|
12
|
-
import { addBasePathUrlDomain } from './url';
|
|
13
|
-
|
|
14
|
-
export const ruleId = 'add-url-domain';
|
|
15
|
-
|
|
16
|
-
const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
|
|
17
|
-
|
|
18
|
-
const rule: ESLintUtils.RuleModule<'addDomain' | 'unknownError'> = createRule({
|
|
19
|
-
name: ruleId,
|
|
20
|
-
meta: {
|
|
21
|
-
type: 'suggestion',
|
|
22
|
-
docs: {
|
|
23
|
-
description: 'Add HTTP domain to the BASE_PATH like url constant variable.',
|
|
24
|
-
},
|
|
25
|
-
messages: {
|
|
26
|
-
addDomain: 'Add HTTP domain to the BASE_PATH like url constant variable.',
|
|
27
|
-
unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}.',
|
|
28
|
-
},
|
|
29
|
-
fixable: 'code',
|
|
30
|
-
schema: [],
|
|
31
|
-
},
|
|
32
|
-
defaultOptions: [],
|
|
33
|
-
create(context) {
|
|
34
|
-
const sourceCode = context.sourceCode;
|
|
35
|
-
|
|
36
|
-
return {
|
|
37
|
-
'VariableDeclarator[id.name=/^([A-Z]+_)*BASE_PATH$/]': (basePathDeclarator: TSESTree.VariableDeclarator) => {
|
|
38
|
-
try {
|
|
39
|
-
if (
|
|
40
|
-
basePathDeclarator.init === null ||
|
|
41
|
-
(basePathDeclarator.init.type !== AST_NODE_TYPES.Literal &&
|
|
42
|
-
basePathDeclarator.init.type !== AST_NODE_TYPES.TemplateLiteral)
|
|
43
|
-
) {
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const urlText = sourceCode.getText(basePathDeclarator.init);
|
|
48
|
-
const replacement = addBasePathUrlDomain(urlText);
|
|
49
|
-
|
|
50
|
-
if (replacement !== urlText) {
|
|
51
|
-
context.report({
|
|
52
|
-
messageId: 'addDomain',
|
|
53
|
-
node: basePathDeclarator.init,
|
|
54
|
-
fix(fixer) {
|
|
55
|
-
return fixer.replaceText(basePathDeclarator.init as TSESTree.Node, replacement);
|
|
56
|
-
},
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
} catch (error) {
|
|
60
|
-
// eslint-disable-next-line no-console
|
|
61
|
-
console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
|
|
62
|
-
context.report({
|
|
63
|
-
node: basePathDeclarator,
|
|
64
|
-
messageId: 'unknownError',
|
|
65
|
-
data: {
|
|
66
|
-
fileName: context.filename,
|
|
67
|
-
error: error instanceof Error ? error.toString() : JSON.stringify(error),
|
|
68
|
-
},
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
};
|
|
73
|
-
},
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
export default rule;
|
|
@@ -1,273 +0,0 @@
|
|
|
1
|
-
// agent/agent-test-wiring.ts
|
|
2
|
-
|
|
3
|
-
/*
|
|
4
|
-
* Copyright (c) 2021-2024 Check Digit, LLC
|
|
5
|
-
*
|
|
6
|
-
* This code is licensed under the MIT license (see LICENSE.txt for details).
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { strict as assert } from 'node:assert';
|
|
10
|
-
|
|
11
|
-
import { AST_TOKEN_TYPES, ESLintUtils, TSESTree } from '@typescript-eslint/utils';
|
|
12
|
-
import type { RuleFix, RuleFixer } from '@typescript-eslint/utils/ts-eslint';
|
|
13
|
-
import debug from 'debug';
|
|
14
|
-
|
|
15
|
-
import getDocumentationUrl from '../get-documentation-url';
|
|
16
|
-
|
|
17
|
-
export const ruleId = 'agent-test-wiring';
|
|
18
|
-
const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
|
|
19
|
-
const log = debug('eslint-plugin:agent:agent-test-wiring');
|
|
20
|
-
|
|
21
|
-
const STATEMENT_FIXTURE_RESET = 'fixture.reset()';
|
|
22
|
-
const STATEMENT_FIXTURE_RESET_AWAITED = `await ${STATEMENT_FIXTURE_RESET};`;
|
|
23
|
-
const STATEMENT_AGENT_DECLARATION = 'let agent: Agent;';
|
|
24
|
-
const STATEMENT_AGENT_CREATION = 'agent = await createAgent();';
|
|
25
|
-
const STATEMENT_AGENT_REGISTER = 'agent.register(await fixturePlugin(fixture));';
|
|
26
|
-
const STATEMENT_AGENT_ENABLE = 'agent.enable();';
|
|
27
|
-
const STATEMENT_AGENT_DISPOSE = 'await agent[Symbol.asyncDispose]();';
|
|
28
|
-
|
|
29
|
-
const rule: ESLintUtils.RuleModule<'updateTestWiring' | 'unknownError'> = createRule({
|
|
30
|
-
name: ruleId,
|
|
31
|
-
meta: {
|
|
32
|
-
type: 'suggestion',
|
|
33
|
-
docs: {
|
|
34
|
-
description: 'Update test wiring.',
|
|
35
|
-
},
|
|
36
|
-
messages: {
|
|
37
|
-
updateTestWiring: 'Updating test wiring.',
|
|
38
|
-
unknownError: 'Unknown error occurred in file "{{fileName}}": {{ error }}.',
|
|
39
|
-
},
|
|
40
|
-
fixable: 'code',
|
|
41
|
-
schema: [],
|
|
42
|
-
},
|
|
43
|
-
defaultOptions: [],
|
|
44
|
-
// eslint-disable-next-line max-lines-per-function
|
|
45
|
-
create(context) {
|
|
46
|
-
log('Processing file:', context.filename);
|
|
47
|
-
const sourceCode = context.sourceCode;
|
|
48
|
-
const importDeclarations = new Map<string, TSESTree.ImportDeclaration>();
|
|
49
|
-
let isFixtureUsed = false;
|
|
50
|
-
let beforeAll: TSESTree.CallExpression | undefined;
|
|
51
|
-
let beforeEach: TSESTree.CallExpression | undefined;
|
|
52
|
-
let afterAll: TSESTree.CallExpression | undefined;
|
|
53
|
-
|
|
54
|
-
return {
|
|
55
|
-
ImportDeclaration(importDeclaration) {
|
|
56
|
-
const moduleName = importDeclaration.source.value;
|
|
57
|
-
importDeclarations.set(moduleName, importDeclaration);
|
|
58
|
-
if (
|
|
59
|
-
moduleName === '@checkdigit/fixture' &&
|
|
60
|
-
importDeclaration.specifiers.some(
|
|
61
|
-
(specifier) =>
|
|
62
|
-
specifier.type === TSESTree.AST_NODE_TYPES.ImportSpecifier &&
|
|
63
|
-
specifier.imported.type === TSESTree.AST_NODE_TYPES.Identifier &&
|
|
64
|
-
specifier.imported.name === 'createFixture',
|
|
65
|
-
)
|
|
66
|
-
) {
|
|
67
|
-
isFixtureUsed = true;
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
'CallExpression[callee.name="beforeAll"]': (callExpression: TSESTree.CallExpression) => {
|
|
71
|
-
beforeAll = callExpression;
|
|
72
|
-
},
|
|
73
|
-
'CallExpression[callee.name="beforeEach"]': (callExpression: TSESTree.CallExpression) => {
|
|
74
|
-
beforeEach = callExpression;
|
|
75
|
-
},
|
|
76
|
-
'CallExpression[callee.name="afterAll"]': (callExpression: TSESTree.CallExpression) => {
|
|
77
|
-
afterAll = callExpression;
|
|
78
|
-
},
|
|
79
|
-
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
80
|
-
'Program:exit'(program) {
|
|
81
|
-
if (!isFixtureUsed || (beforeAll === undefined && beforeEach === undefined)) {
|
|
82
|
-
// only update test wiring if fixture is used
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
try {
|
|
87
|
-
let jestImportFixer: ((fixer: RuleFixer) => RuleFix) | undefined;
|
|
88
|
-
let agentImportFixer: ((fixer: RuleFixer) => RuleFix) | undefined;
|
|
89
|
-
let fixturePluginImportFixer: ((fixer: RuleFixer) => RuleFix) | undefined;
|
|
90
|
-
let agentDeclarationFixer: ((fixer: RuleFixer) => RuleFix) | undefined;
|
|
91
|
-
let beforeAllOrEachFixer: ((fixer: RuleFixer) => RuleFix) | undefined;
|
|
92
|
-
let afterAllFixer: ((fixer: RuleFixer) => RuleFix) | undefined;
|
|
93
|
-
|
|
94
|
-
const lastImportDeclaration = [...importDeclarations.values()].at(-1);
|
|
95
|
-
assert.ok(lastImportDeclaration);
|
|
96
|
-
|
|
97
|
-
// make sure that afterAll is imported from jest
|
|
98
|
-
const jestImportDeclaration = importDeclarations.get('@jest/globals');
|
|
99
|
-
assert.ok(jestImportDeclaration);
|
|
100
|
-
const importsToAdd = ['afterAll', 'beforeAll'].filter(
|
|
101
|
-
(jestHook) =>
|
|
102
|
-
!jestImportDeclaration.specifiers.some(
|
|
103
|
-
(specifier) =>
|
|
104
|
-
specifier.type === TSESTree.AST_NODE_TYPES.ImportSpecifier &&
|
|
105
|
-
specifier.imported.type === TSESTree.AST_NODE_TYPES.Identifier &&
|
|
106
|
-
specifier.imported.name === jestHook,
|
|
107
|
-
),
|
|
108
|
-
);
|
|
109
|
-
if (importsToAdd.length > 0) {
|
|
110
|
-
const firstImportSpecifier = jestImportDeclaration.specifiers[0];
|
|
111
|
-
assert.ok(firstImportSpecifier);
|
|
112
|
-
jestImportFixer = (fixer: RuleFixer) =>
|
|
113
|
-
fixer.insertTextBefore(firstImportSpecifier, `${importsToAdd.join(', ')}, `);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// make sure that agent is imported
|
|
117
|
-
const agentImportDeclaration = importDeclarations.get('@checkdigit/agent');
|
|
118
|
-
if (!agentImportDeclaration) {
|
|
119
|
-
agentImportFixer = (fixer: RuleFixer) =>
|
|
120
|
-
fixer.insertTextAfter(
|
|
121
|
-
lastImportDeclaration,
|
|
122
|
-
`\nimport createAgent, { type Agent } from '@checkdigit/agent';`,
|
|
123
|
-
);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// make sure that fixture plugin is imported
|
|
127
|
-
const pathLets = context.filename.split('/');
|
|
128
|
-
const currentFileIndex = pathLets.length - 1;
|
|
129
|
-
const pluginFolderIndex = pathLets.lastIndexOf('src') + 1;
|
|
130
|
-
// it should be safe to assume that the test code is always at least one level deeper than the plugin folder
|
|
131
|
-
const fixturePluginImportPath = `${'../'.repeat(currentFileIndex - pluginFolderIndex)}plugin/fixture.test`;
|
|
132
|
-
if (!importDeclarations.get(fixturePluginImportPath)) {
|
|
133
|
-
fixturePluginImportFixer = (fixer: RuleFixer) =>
|
|
134
|
-
fixer.insertTextAfter(lastImportDeclaration, `\nimport fixturePlugin from '${fixturePluginImportPath}';`);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// inject agent declaration and initialization
|
|
138
|
-
if (beforeAll === undefined) {
|
|
139
|
-
// create `beforeAll` block if it doesn't exist
|
|
140
|
-
beforeAllOrEachFixer = (fixer: RuleFixer) =>
|
|
141
|
-
fixer.insertTextBefore(
|
|
142
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
143
|
-
beforeEach!,
|
|
144
|
-
[
|
|
145
|
-
STATEMENT_AGENT_DECLARATION,
|
|
146
|
-
`beforeAll(async () => {`,
|
|
147
|
-
[STATEMENT_AGENT_CREATION, STATEMENT_AGENT_REGISTER, STATEMENT_AGENT_ENABLE].join('\n'),
|
|
148
|
-
`});\n`,
|
|
149
|
-
].join('\n'),
|
|
150
|
-
);
|
|
151
|
-
} else {
|
|
152
|
-
const beforeAllArgument = beforeAll.arguments[0];
|
|
153
|
-
assert.ok(beforeAllArgument !== undefined);
|
|
154
|
-
if (!sourceCode.getText(beforeAllArgument).includes(STATEMENT_AGENT_CREATION)) {
|
|
155
|
-
if (
|
|
156
|
-
beforeAllArgument.type === TSESTree.AST_NODE_TYPES.ArrowFunctionExpression &&
|
|
157
|
-
beforeAllArgument.body.type === TSESTree.AST_NODE_TYPES.BlockStatement
|
|
158
|
-
) {
|
|
159
|
-
const fixtureResetStatement = beforeAllArgument.body.body.find(
|
|
160
|
-
(statement) => sourceCode.getText(statement) === STATEMENT_FIXTURE_RESET_AWAITED,
|
|
161
|
-
);
|
|
162
|
-
assert.ok(fixtureResetStatement !== undefined);
|
|
163
|
-
beforeAllOrEachFixer = (fixer: RuleFixer) =>
|
|
164
|
-
fixer.replaceText(
|
|
165
|
-
fixtureResetStatement,
|
|
166
|
-
[
|
|
167
|
-
STATEMENT_AGENT_CREATION,
|
|
168
|
-
STATEMENT_AGENT_REGISTER,
|
|
169
|
-
STATEMENT_AGENT_ENABLE,
|
|
170
|
-
STATEMENT_FIXTURE_RESET_AWAITED,
|
|
171
|
-
].join('\n'),
|
|
172
|
-
);
|
|
173
|
-
} else {
|
|
174
|
-
beforeAllOrEachFixer = (fixer: RuleFixer) =>
|
|
175
|
-
fixer.replaceText(
|
|
176
|
-
beforeAllArgument,
|
|
177
|
-
[
|
|
178
|
-
`async () => {`,
|
|
179
|
-
STATEMENT_AGENT_CREATION,
|
|
180
|
-
STATEMENT_AGENT_REGISTER,
|
|
181
|
-
STATEMENT_AGENT_ENABLE,
|
|
182
|
-
STATEMENT_FIXTURE_RESET_AWAITED,
|
|
183
|
-
`}`,
|
|
184
|
-
].join('\n'),
|
|
185
|
-
);
|
|
186
|
-
}
|
|
187
|
-
agentDeclarationFixer = (fixer: RuleFixer) =>
|
|
188
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
189
|
-
fixer.insertTextBefore(beforeAll!, `${STATEMENT_AGENT_DECLARATION}\n`);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
// inject agent disposal to `afterAll` block
|
|
194
|
-
if (afterAll !== undefined) {
|
|
195
|
-
const afterAllArrowFunctionExpression = afterAll.arguments[0];
|
|
196
|
-
assert.ok(
|
|
197
|
-
afterAllArrowFunctionExpression !== undefined &&
|
|
198
|
-
afterAllArrowFunctionExpression.type === TSESTree.AST_NODE_TYPES.ArrowFunctionExpression,
|
|
199
|
-
);
|
|
200
|
-
const arrowFunctionBody = afterAllArrowFunctionExpression.body;
|
|
201
|
-
assert.ok(arrowFunctionBody.type === TSESTree.AST_NODE_TYPES.BlockStatement);
|
|
202
|
-
|
|
203
|
-
const afterAllBodyText = sourceCode.getText(arrowFunctionBody);
|
|
204
|
-
if (!afterAllBodyText.includes(STATEMENT_AGENT_DISPOSE)) {
|
|
205
|
-
const lastStatement = arrowFunctionBody.body.at(-1);
|
|
206
|
-
assert.ok(lastStatement);
|
|
207
|
-
afterAllFixer = (fixer: RuleFixer) => fixer.insertTextAfter(lastStatement, STATEMENT_AGENT_DISPOSE);
|
|
208
|
-
}
|
|
209
|
-
} else {
|
|
210
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
211
|
-
const nextToken = sourceCode.getTokenAfter(beforeAll ?? beforeEach!);
|
|
212
|
-
afterAllFixer = (fixer: RuleFixer) =>
|
|
213
|
-
fixer.insertTextAfter(
|
|
214
|
-
nextToken !== null && nextToken.type === AST_TOKEN_TYPES.Punctuator
|
|
215
|
-
? nextToken
|
|
216
|
-
: // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
217
|
-
beforeAll!,
|
|
218
|
-
['', `afterAll(async () => {`, STATEMENT_AGENT_DISPOSE, `});`].join('\n'),
|
|
219
|
-
);
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
if (
|
|
223
|
-
jestImportFixer !== undefined ||
|
|
224
|
-
agentImportFixer !== undefined ||
|
|
225
|
-
fixturePluginImportFixer !== undefined ||
|
|
226
|
-
agentDeclarationFixer !== undefined ||
|
|
227
|
-
beforeAllOrEachFixer !== undefined ||
|
|
228
|
-
afterAllFixer !== undefined
|
|
229
|
-
) {
|
|
230
|
-
context.report({
|
|
231
|
-
messageId: 'updateTestWiring',
|
|
232
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
233
|
-
node: beforeAll ?? beforeEach!,
|
|
234
|
-
*fix(fixer) {
|
|
235
|
-
if (jestImportFixer !== undefined) {
|
|
236
|
-
yield jestImportFixer(fixer);
|
|
237
|
-
}
|
|
238
|
-
if (agentImportFixer !== undefined) {
|
|
239
|
-
yield agentImportFixer(fixer);
|
|
240
|
-
}
|
|
241
|
-
if (fixturePluginImportFixer !== undefined) {
|
|
242
|
-
yield fixturePluginImportFixer(fixer);
|
|
243
|
-
}
|
|
244
|
-
if (agentDeclarationFixer !== undefined) {
|
|
245
|
-
yield agentDeclarationFixer(fixer);
|
|
246
|
-
}
|
|
247
|
-
if (beforeAllOrEachFixer !== undefined) {
|
|
248
|
-
yield beforeAllOrEachFixer(fixer);
|
|
249
|
-
}
|
|
250
|
-
if (afterAllFixer !== undefined) {
|
|
251
|
-
yield afterAllFixer(fixer);
|
|
252
|
-
}
|
|
253
|
-
},
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
} catch (error) {
|
|
257
|
-
// eslint-disable-next-line no-console
|
|
258
|
-
console.error(`Failed to apply ${ruleId} rule for file "${context.filename}":`, error);
|
|
259
|
-
context.report({
|
|
260
|
-
node: program,
|
|
261
|
-
messageId: 'unknownError',
|
|
262
|
-
data: {
|
|
263
|
-
fileName: context.filename,
|
|
264
|
-
error: error instanceof Error ? error.toString() : JSON.stringify(error),
|
|
265
|
-
},
|
|
266
|
-
});
|
|
267
|
-
}
|
|
268
|
-
},
|
|
269
|
-
};
|
|
270
|
-
},
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
export default rule;
|