@virmator/lint 14.27.1 → 14.27.3
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/configs/eslint.config.base.ts +5 -0
- package/dist/rules/assertions-in-tests.lint.d.ts +21 -0
- package/dist/rules/assertions-in-tests.lint.js +234 -0
- package/dist/rules/prefer-params-object.lint.js +18 -0
- package/package.json +14 -14
- package/src/rules/assertions-in-tests.lint.ts +333 -0
- package/src/rules/prefer-params-object.lint.ts +23 -0
|
@@ -6,6 +6,7 @@ import sonarJsEslint from 'eslint-plugin-sonarjs';
|
|
|
6
6
|
import eslintPluginUnicorn from 'eslint-plugin-unicorn';
|
|
7
7
|
import globals from 'globals';
|
|
8
8
|
import tsEslint from 'typescript-eslint';
|
|
9
|
+
import assertionsInTestsRule from '../src/rules/assertions-in-tests.lint.js';
|
|
9
10
|
import errorNameAsClassFieldRule from '../src/rules/error-name-as-class-field.lint.js';
|
|
10
11
|
import noRawDateRule from '../src/rules/no-raw-date.lint.js';
|
|
11
12
|
import noReadonlyPrimitiveRule from '../src/rules/no-readonly-primitive.lint.js';
|
|
@@ -72,6 +73,7 @@ export function defineEslintConfig(repoDir: string) {
|
|
|
72
73
|
unicorn: eslintPluginUnicorn,
|
|
73
74
|
'@virmator': {
|
|
74
75
|
rules: {
|
|
76
|
+
'assertions-in-tests': assertionsInTestsRule,
|
|
75
77
|
'error-name-as-class-field': errorNameAsClassFieldRule,
|
|
76
78
|
'no-raw-date': noRawDateRule,
|
|
77
79
|
'no-readonly-primitive': noReadonlyPrimitiveRule,
|
|
@@ -134,6 +136,8 @@ export function defineEslintConfig(repoDir: string) {
|
|
|
134
136
|
'sonarjs/use-type-alias': 'off',
|
|
135
137
|
'sonarjs/void-use': 'off',
|
|
136
138
|
'sonarjs/deprecation': 'off', // duplicates @typescript-eslint/no-deprecated
|
|
139
|
+
// replaced by @virmator/assertions-in-tests, which also recognizes @augment-vir/assert
|
|
140
|
+
'sonarjs/assertions-in-tests': 'off',
|
|
137
141
|
|
|
138
142
|
'no-async-promise-executor': 'off',
|
|
139
143
|
'no-prototype-builtins': 'off',
|
|
@@ -190,6 +194,7 @@ export function defineEslintConfig(repoDir: string) {
|
|
|
190
194
|
'@jsdoc/no-undefined-types': 'error',
|
|
191
195
|
'@typescript-eslint/await-thenable': 'error',
|
|
192
196
|
'@typescript-eslint/no-unused-vars': 'error',
|
|
197
|
+
'@virmator/assertions-in-tests': 'error',
|
|
193
198
|
'@virmator/error-name-as-class-field': 'error',
|
|
194
199
|
'@virmator/no-raw-date': 'error',
|
|
195
200
|
'@virmator/no-readonly-primitive': 'error',
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type Rule } from 'eslint';
|
|
2
|
+
/**
|
|
3
|
+
* Options for the `assertions-in-tests` rule. Both lists are merged with the built-in defaults, so
|
|
4
|
+
* the config only needs to add project-specific entries.
|
|
5
|
+
*
|
|
6
|
+
* @category Rules
|
|
7
|
+
*/
|
|
8
|
+
export type AssertionsInTestsOptions = {
|
|
9
|
+
/**
|
|
10
|
+
* Additional module specifiers whose exports should all be treated as assertion utilities (in
|
|
11
|
+
* addition to {@link assertionModules}).
|
|
12
|
+
*/
|
|
13
|
+
additionalAssertionModules?: ReadonlyArray<string> | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Additional exported names that should be treated as assertion utilities even when imported
|
|
16
|
+
* through a relative path (in addition to {@link augmentVirAssertionNames}).
|
|
17
|
+
*/
|
|
18
|
+
additionalAssertionNames?: ReadonlyArray<string> | undefined;
|
|
19
|
+
};
|
|
20
|
+
declare const rule: Rule.RuleModule;
|
|
21
|
+
export default rule;
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Module specifiers whose exports are all treated as assertion utilities. Any identifier imported
|
|
3
|
+
* from one of these counts as an assertion entry point.
|
|
4
|
+
*/
|
|
5
|
+
const assertionModules = [
|
|
6
|
+
'@augment-vir/assert',
|
|
7
|
+
'chai',
|
|
8
|
+
'sinon',
|
|
9
|
+
'vitest',
|
|
10
|
+
'supertest',
|
|
11
|
+
'@playwright/test',
|
|
12
|
+
'assert',
|
|
13
|
+
'node:assert',
|
|
14
|
+
];
|
|
15
|
+
/**
|
|
16
|
+
* Names of `@augment-vir/assert` exports that count as assertion entry points even when imported
|
|
17
|
+
* through a relative path. This is necessary because the `@augment-vir/assert` package's own tests
|
|
18
|
+
* import these guards via relative paths rather than the package name.
|
|
19
|
+
*/
|
|
20
|
+
const augmentVirAssertionNames = [
|
|
21
|
+
'assert',
|
|
22
|
+
'assertWrap',
|
|
23
|
+
'check',
|
|
24
|
+
'checkWrap',
|
|
25
|
+
'waitUntil',
|
|
26
|
+
];
|
|
27
|
+
/** Names of the test-defining functions whose callbacks must contain at least one assertion. */
|
|
28
|
+
const testFunctionNames = [
|
|
29
|
+
'it',
|
|
30
|
+
'test',
|
|
31
|
+
];
|
|
32
|
+
/**
|
|
33
|
+
* Builds the set of local identifier names that refer to assertion utilities in the current file,
|
|
34
|
+
* based on its imports.
|
|
35
|
+
*/
|
|
36
|
+
function collectAssertionIdentifiers(body, { modules, names }) {
|
|
37
|
+
const assertionIdentifiers = new Set();
|
|
38
|
+
body.forEach((node) => {
|
|
39
|
+
if (node.type !== 'ImportDeclaration') {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const importDeclaration = node;
|
|
43
|
+
const source = importDeclaration.source.value;
|
|
44
|
+
const isAssertionModule = typeof source === 'string' && modules.includes(source);
|
|
45
|
+
importDeclaration.specifiers.forEach((specifier) => {
|
|
46
|
+
const importedName = specifier.type === 'ImportSpecifier' && specifier.imported.type === 'Identifier'
|
|
47
|
+
? specifier.imported.name
|
|
48
|
+
: specifier.local.name;
|
|
49
|
+
if (isAssertionModule || names.includes(importedName)) {
|
|
50
|
+
assertionIdentifiers.add(specifier.local.name);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
return assertionIdentifiers;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Walks the left side of a member/call/optional-chain expression down to the root identifier (for
|
|
58
|
+
* example, `assert` in `assert.tsType(value).equals<T>()`).
|
|
59
|
+
*/
|
|
60
|
+
function getRootIdentifier(node) {
|
|
61
|
+
if (node.type === 'Identifier') {
|
|
62
|
+
return node;
|
|
63
|
+
}
|
|
64
|
+
else if (node.type === 'MemberExpression') {
|
|
65
|
+
return getRootIdentifier(node.object);
|
|
66
|
+
}
|
|
67
|
+
else if (node.type === 'CallExpression' || node.type === 'NewExpression') {
|
|
68
|
+
return getRootIdentifier(node.callee);
|
|
69
|
+
}
|
|
70
|
+
else if (node.type === 'ChainExpression') {
|
|
71
|
+
return getRootIdentifier(node.expression);
|
|
72
|
+
}
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
function isAssertionCall(node, assertionIdentifiers) {
|
|
76
|
+
const root = getRootIdentifier(node.callee);
|
|
77
|
+
return root != undefined && assertionIdentifiers.has(root.name);
|
|
78
|
+
}
|
|
79
|
+
function childrenOf(node, visitorKeys) {
|
|
80
|
+
const keys = visitorKeys[node.type] || [];
|
|
81
|
+
return keys.flatMap((key) => {
|
|
82
|
+
const value = node[key];
|
|
83
|
+
if (Array.isArray(value)) {
|
|
84
|
+
return value.filter((item) => item != undefined && typeof item.type === 'string');
|
|
85
|
+
}
|
|
86
|
+
else if (value != undefined && typeof value.type === 'string') {
|
|
87
|
+
return [value];
|
|
88
|
+
}
|
|
89
|
+
return [];
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Builds a map of every function defined in the file (named function declarations and
|
|
94
|
+
* function/arrow expressions assigned to a variable) keyed by name, so the assertion walk can
|
|
95
|
+
* follow calls into local test helpers.
|
|
96
|
+
*/
|
|
97
|
+
function collectLocalFunctions(program, visitorKeys) {
|
|
98
|
+
const localFunctions = new Map();
|
|
99
|
+
function walk(node) {
|
|
100
|
+
if (node.type === 'FunctionDeclaration') {
|
|
101
|
+
localFunctions.set(node.id.name, node);
|
|
102
|
+
}
|
|
103
|
+
else if (node.type === 'VariableDeclarator' &&
|
|
104
|
+
node.id.type === 'Identifier' &&
|
|
105
|
+
node.init != undefined &&
|
|
106
|
+
(node.init.type === 'ArrowFunctionExpression' ||
|
|
107
|
+
node.init.type === 'FunctionExpression')) {
|
|
108
|
+
localFunctions.set(node.id.name, node.init);
|
|
109
|
+
}
|
|
110
|
+
childrenOf(node, visitorKeys).forEach(walk);
|
|
111
|
+
}
|
|
112
|
+
walk(program);
|
|
113
|
+
return localFunctions;
|
|
114
|
+
}
|
|
115
|
+
function containsAssertion(node, search) {
|
|
116
|
+
if (search.visited.has(node)) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
search.visited.add(node);
|
|
120
|
+
if (node.type === 'CallExpression') {
|
|
121
|
+
if (isAssertionCall(node, search.assertionIdentifiers)) {
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
/** Follow calls into functions defined in the same file (e.g. shared test helpers). */
|
|
125
|
+
if (node.callee.type === 'Identifier') {
|
|
126
|
+
const localFunction = search.localFunctions.get(node.callee.name);
|
|
127
|
+
if (localFunction && containsAssertion(localFunction, search)) {
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return childrenOf(node, search.visitorKeys).some((child) => containsAssertion(child, search));
|
|
133
|
+
}
|
|
134
|
+
function isTestCall(node) {
|
|
135
|
+
const callee = node.callee;
|
|
136
|
+
if (callee.type === 'Identifier') {
|
|
137
|
+
return testFunctionNames.includes(callee.name);
|
|
138
|
+
}
|
|
139
|
+
else if (callee.type === 'MemberExpression' && callee.object.type === 'Identifier') {
|
|
140
|
+
/** Catches `it.only(...)`, `it.skip(...)`, `test.only(...)`, etc. */
|
|
141
|
+
return testFunctionNames.includes(callee.object.name);
|
|
142
|
+
}
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
function getTestCallback(node) {
|
|
146
|
+
return node.arguments.find((argument) => argument.type === 'FunctionExpression' || argument.type === 'ArrowFunctionExpression');
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* A `@ts-expect-error` (or `@ts-ignore`) directive inside a test is a deliberate compile-time
|
|
150
|
+
* assertion that the following code is a type error, so such tests are treated as having an
|
|
151
|
+
* assertion.
|
|
152
|
+
*/
|
|
153
|
+
const tsDirectiveRegExp = /@ts-(?:expect-error|ignore)\b/;
|
|
154
|
+
function hasTypeErrorDirective(callback, sourceCode) {
|
|
155
|
+
return sourceCode
|
|
156
|
+
.getCommentsInside(callback)
|
|
157
|
+
.some((comment) => tsDirectiveRegExp.test(comment.value));
|
|
158
|
+
}
|
|
159
|
+
const rule = {
|
|
160
|
+
meta: {
|
|
161
|
+
type: 'problem',
|
|
162
|
+
messages: {
|
|
163
|
+
noAssertion: 'Add at least one assertion to this test case.',
|
|
164
|
+
},
|
|
165
|
+
schema: [
|
|
166
|
+
{
|
|
167
|
+
type: 'object',
|
|
168
|
+
properties: {
|
|
169
|
+
additionalAssertionModules: {
|
|
170
|
+
type: 'array',
|
|
171
|
+
items: {
|
|
172
|
+
type: 'string',
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
additionalAssertionNames: {
|
|
176
|
+
type: 'array',
|
|
177
|
+
items: {
|
|
178
|
+
type: 'string',
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
additionalProperties: false,
|
|
183
|
+
},
|
|
184
|
+
],
|
|
185
|
+
},
|
|
186
|
+
create(context) {
|
|
187
|
+
const options = context.options[0] || {};
|
|
188
|
+
const modules = [
|
|
189
|
+
...assertionModules,
|
|
190
|
+
...(options.additionalAssertionModules || []),
|
|
191
|
+
];
|
|
192
|
+
const names = [
|
|
193
|
+
...augmentVirAssertionNames,
|
|
194
|
+
...(options.additionalAssertionNames || []),
|
|
195
|
+
];
|
|
196
|
+
const assertionIdentifiers = collectAssertionIdentifiers(context.sourceCode.ast.body, {
|
|
197
|
+
modules,
|
|
198
|
+
names,
|
|
199
|
+
});
|
|
200
|
+
/**
|
|
201
|
+
* Only check files that actually import an assertion utility. Files that don't reference
|
|
202
|
+
* any known assertion mechanism are left alone to avoid false positives.
|
|
203
|
+
*/
|
|
204
|
+
if (!assertionIdentifiers.size) {
|
|
205
|
+
return {};
|
|
206
|
+
}
|
|
207
|
+
const visitorKeys = context.sourceCode.visitorKeys;
|
|
208
|
+
const localFunctions = collectLocalFunctions(context.sourceCode.ast, visitorKeys);
|
|
209
|
+
return {
|
|
210
|
+
CallExpression(node) {
|
|
211
|
+
if (!isTestCall(node)) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
const callback = getTestCallback(node);
|
|
215
|
+
if (!callback) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
const hasAssertion = containsAssertion(callback.body, {
|
|
219
|
+
assertionIdentifiers,
|
|
220
|
+
visitorKeys,
|
|
221
|
+
localFunctions,
|
|
222
|
+
visited: new Set(),
|
|
223
|
+
});
|
|
224
|
+
if (!hasAssertion && !hasTypeErrorDirective(callback, context.sourceCode)) {
|
|
225
|
+
context.report({
|
|
226
|
+
node,
|
|
227
|
+
messageId: 'noAssertion',
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
};
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
export default rule;
|
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inline callbacks (function or arrow expressions passed directly as call arguments, like
|
|
3
|
+
* `array.reduce((accum, entry, index, wholeArray) => ...)`) have a signature dictated by their
|
|
4
|
+
* caller, so they cannot adopt a params object. Such callbacks should be skipped.
|
|
5
|
+
*/
|
|
6
|
+
function isInlineCallbackArgument(node) {
|
|
7
|
+
if (node.type === 'FunctionDeclaration') {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
const parent = node.parent;
|
|
11
|
+
if (!parent || parent.type !== 'CallExpression') {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
return parent.arguments.includes(node);
|
|
15
|
+
}
|
|
1
16
|
/**
|
|
2
17
|
* Excludes a leading `this` parameter (which is a type-only annotation in TypeScript, not a real
|
|
3
18
|
* positional argument).
|
|
@@ -91,6 +106,9 @@ const rule = {
|
|
|
91
106
|
create(context) {
|
|
92
107
|
const sourceCode = context.sourceCode;
|
|
93
108
|
function check(node) {
|
|
109
|
+
if (isInlineCallbackArgument(node)) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
94
112
|
const realParams = getRealParams(node.params);
|
|
95
113
|
const messageId = realParams.length > 3
|
|
96
114
|
? 'tooManyPositionalParams'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@virmator/lint",
|
|
3
|
-
"version": "14.27.
|
|
3
|
+
"version": "14.27.3",
|
|
4
4
|
"description": "Default ESLint linting plugin for virmator.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"virmator",
|
|
@@ -32,32 +32,32 @@
|
|
|
32
32
|
"test:update": "npm test update"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@augment-vir/assert": "^
|
|
36
|
-
"@augment-vir/common": "^
|
|
37
|
-
"@augment-vir/node": "^
|
|
38
|
-
"@virmator/core": "^14.27.
|
|
39
|
-
"globals": "^17.
|
|
35
|
+
"@augment-vir/assert": "^32.0.0",
|
|
36
|
+
"@augment-vir/common": "^32.0.0",
|
|
37
|
+
"@augment-vir/node": "^32.0.0",
|
|
38
|
+
"@virmator/core": "^14.27.3",
|
|
39
|
+
"globals": "^17.7.0",
|
|
40
40
|
"jiti": "^2.7.0",
|
|
41
41
|
"mri": "^1.2.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@augment-vir/test": "^
|
|
44
|
+
"@augment-vir/test": "^32.0.0",
|
|
45
45
|
"@eslint/js": "10.0.1",
|
|
46
46
|
"@stylistic/eslint-plugin": "^5.10.0",
|
|
47
47
|
"@stylistic/eslint-plugin-ts": "^4.4.1",
|
|
48
48
|
"@types/estree": "^1.0.9",
|
|
49
|
-
"@types/node": "^
|
|
50
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
49
|
+
"@types/node": "^26.0.1",
|
|
50
|
+
"@typescript-eslint/eslint-plugin": "^8.62.0",
|
|
51
51
|
"c8": "^11.0.0",
|
|
52
|
-
"eslint": "^10.
|
|
52
|
+
"eslint": "^10.5.0",
|
|
53
53
|
"eslint-config-prettier": "^10.1.8",
|
|
54
|
-
"eslint-plugin-jsdoc": "^63.0.
|
|
54
|
+
"eslint-plugin-jsdoc": "^63.0.9",
|
|
55
55
|
"eslint-plugin-playwright": "^2.10.4",
|
|
56
56
|
"eslint-plugin-prettier": "^5.5.6",
|
|
57
|
-
"eslint-plugin-sonarjs": "^4.0
|
|
58
|
-
"eslint-plugin-unicorn": "^
|
|
57
|
+
"eslint-plugin-sonarjs": "^4.1.0",
|
|
58
|
+
"eslint-plugin-unicorn": "^69.0.0",
|
|
59
59
|
"istanbul-smart-text-reporter": "^1.1.5",
|
|
60
|
-
"typescript-eslint": "^8.
|
|
60
|
+
"typescript-eslint": "^8.62.0"
|
|
61
61
|
},
|
|
62
62
|
"engines": {
|
|
63
63
|
"node": ">=22"
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import {type Rule} from 'eslint';
|
|
2
|
+
import type {
|
|
3
|
+
ArrowFunctionExpression,
|
|
4
|
+
CallExpression,
|
|
5
|
+
FunctionExpression,
|
|
6
|
+
Identifier,
|
|
7
|
+
ImportDeclaration,
|
|
8
|
+
Node,
|
|
9
|
+
Program,
|
|
10
|
+
} from 'estree';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Module specifiers whose exports are all treated as assertion utilities. Any identifier imported
|
|
14
|
+
* from one of these counts as an assertion entry point.
|
|
15
|
+
*/
|
|
16
|
+
const assertionModules: ReadonlyArray<string> = [
|
|
17
|
+
'@augment-vir/assert',
|
|
18
|
+
'chai',
|
|
19
|
+
'sinon',
|
|
20
|
+
'vitest',
|
|
21
|
+
'supertest',
|
|
22
|
+
'@playwright/test',
|
|
23
|
+
'assert',
|
|
24
|
+
'node:assert',
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Names of `@augment-vir/assert` exports that count as assertion entry points even when imported
|
|
29
|
+
* through a relative path. This is necessary because the `@augment-vir/assert` package's own tests
|
|
30
|
+
* import these guards via relative paths rather than the package name.
|
|
31
|
+
*/
|
|
32
|
+
const augmentVirAssertionNames: ReadonlyArray<string> = [
|
|
33
|
+
'assert',
|
|
34
|
+
'assertWrap',
|
|
35
|
+
'check',
|
|
36
|
+
'checkWrap',
|
|
37
|
+
'waitUntil',
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
/** Names of the test-defining functions whose callbacks must contain at least one assertion. */
|
|
41
|
+
const testFunctionNames: ReadonlyArray<string> = [
|
|
42
|
+
'it',
|
|
43
|
+
'test',
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Options for the `assertions-in-tests` rule. Both lists are merged with the built-in defaults, so
|
|
48
|
+
* the config only needs to add project-specific entries.
|
|
49
|
+
*
|
|
50
|
+
* @category Rules
|
|
51
|
+
*/
|
|
52
|
+
export type AssertionsInTestsOptions = {
|
|
53
|
+
/**
|
|
54
|
+
* Additional module specifiers whose exports should all be treated as assertion utilities (in
|
|
55
|
+
* addition to {@link assertionModules}).
|
|
56
|
+
*/
|
|
57
|
+
additionalAssertionModules?: ReadonlyArray<string> | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* Additional exported names that should be treated as assertion utilities even when imported
|
|
60
|
+
* through a relative path (in addition to {@link augmentVirAssertionNames}).
|
|
61
|
+
*/
|
|
62
|
+
additionalAssertionNames?: ReadonlyArray<string> | undefined;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Builds the set of local identifier names that refer to assertion utilities in the current file,
|
|
67
|
+
* based on its imports.
|
|
68
|
+
*/
|
|
69
|
+
function collectAssertionIdentifiers(
|
|
70
|
+
body: Program['body'],
|
|
71
|
+
{modules, names}: Readonly<{modules: ReadonlyArray<string>; names: ReadonlyArray<string>}>,
|
|
72
|
+
): Set<string> {
|
|
73
|
+
const assertionIdentifiers = new Set<string>();
|
|
74
|
+
|
|
75
|
+
body.forEach((node) => {
|
|
76
|
+
if (node.type !== 'ImportDeclaration') {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const importDeclaration: ImportDeclaration = node;
|
|
81
|
+
const source = importDeclaration.source.value;
|
|
82
|
+
const isAssertionModule = typeof source === 'string' && modules.includes(source);
|
|
83
|
+
|
|
84
|
+
importDeclaration.specifiers.forEach((specifier) => {
|
|
85
|
+
const importedName =
|
|
86
|
+
specifier.type === 'ImportSpecifier' && specifier.imported.type === 'Identifier'
|
|
87
|
+
? specifier.imported.name
|
|
88
|
+
: specifier.local.name;
|
|
89
|
+
|
|
90
|
+
if (isAssertionModule || names.includes(importedName)) {
|
|
91
|
+
assertionIdentifiers.add(specifier.local.name);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
return assertionIdentifiers;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Walks the left side of a member/call/optional-chain expression down to the root identifier (for
|
|
101
|
+
* example, `assert` in `assert.tsType(value).equals<T>()`).
|
|
102
|
+
*/
|
|
103
|
+
function getRootIdentifier(node: Node): Identifier | undefined {
|
|
104
|
+
if (node.type === 'Identifier') {
|
|
105
|
+
return node;
|
|
106
|
+
} else if (node.type === 'MemberExpression') {
|
|
107
|
+
return getRootIdentifier(node.object);
|
|
108
|
+
} else if (node.type === 'CallExpression' || node.type === 'NewExpression') {
|
|
109
|
+
return getRootIdentifier(node.callee);
|
|
110
|
+
} else if (node.type === 'ChainExpression') {
|
|
111
|
+
return getRootIdentifier(node.expression);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function isAssertionCall(node: CallExpression, assertionIdentifiers: ReadonlySet<string>): boolean {
|
|
118
|
+
const root = getRootIdentifier(node.callee);
|
|
119
|
+
|
|
120
|
+
return root != undefined && assertionIdentifiers.has(root.name);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function childrenOf(
|
|
124
|
+
node: Node,
|
|
125
|
+
visitorKeys: Rule.RuleContext['sourceCode']['visitorKeys'],
|
|
126
|
+
): Node[] {
|
|
127
|
+
const keys = visitorKeys[node.type] || [];
|
|
128
|
+
|
|
129
|
+
return keys.flatMap((key): Node[] => {
|
|
130
|
+
const value = (node as unknown as Record<string, unknown>)[key];
|
|
131
|
+
|
|
132
|
+
if (Array.isArray(value)) {
|
|
133
|
+
return value.filter(
|
|
134
|
+
(item): item is Node =>
|
|
135
|
+
item != undefined && typeof (item as Node).type === 'string',
|
|
136
|
+
);
|
|
137
|
+
} else if (value != undefined && typeof (value as Node).type === 'string') {
|
|
138
|
+
return [value as Node];
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return [];
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* State shared across the recursive {@link containsAssertion} walk. `localFunctions` maps the names
|
|
147
|
+
* of functions defined in the current file to their nodes so the walk can follow calls into local
|
|
148
|
+
* test helpers, and `visited` guards against infinite recursion.
|
|
149
|
+
*/
|
|
150
|
+
type AssertionSearch = {
|
|
151
|
+
assertionIdentifiers: ReadonlySet<string>;
|
|
152
|
+
visitorKeys: Rule.RuleContext['sourceCode']['visitorKeys'];
|
|
153
|
+
localFunctions: ReadonlyMap<string, Node>;
|
|
154
|
+
visited: Set<Node>;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Builds a map of every function defined in the file (named function declarations and
|
|
159
|
+
* function/arrow expressions assigned to a variable) keyed by name, so the assertion walk can
|
|
160
|
+
* follow calls into local test helpers.
|
|
161
|
+
*/
|
|
162
|
+
function collectLocalFunctions(
|
|
163
|
+
program: Program,
|
|
164
|
+
visitorKeys: Rule.RuleContext['sourceCode']['visitorKeys'],
|
|
165
|
+
): Map<string, Node> {
|
|
166
|
+
const localFunctions = new Map<string, Node>();
|
|
167
|
+
|
|
168
|
+
function walk(node: Node) {
|
|
169
|
+
if (node.type === 'FunctionDeclaration') {
|
|
170
|
+
localFunctions.set(node.id.name, node);
|
|
171
|
+
} else if (
|
|
172
|
+
node.type === 'VariableDeclarator' &&
|
|
173
|
+
node.id.type === 'Identifier' &&
|
|
174
|
+
node.init != undefined &&
|
|
175
|
+
(node.init.type === 'ArrowFunctionExpression' ||
|
|
176
|
+
node.init.type === 'FunctionExpression')
|
|
177
|
+
) {
|
|
178
|
+
localFunctions.set(node.id.name, node.init);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
childrenOf(node, visitorKeys).forEach(walk);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
walk(program);
|
|
185
|
+
|
|
186
|
+
return localFunctions;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function containsAssertion(node: Node, search: AssertionSearch): boolean {
|
|
190
|
+
if (search.visited.has(node)) {
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
search.visited.add(node);
|
|
194
|
+
|
|
195
|
+
if (node.type === 'CallExpression') {
|
|
196
|
+
if (isAssertionCall(node, search.assertionIdentifiers)) {
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Follow calls into functions defined in the same file (e.g. shared test helpers). */
|
|
201
|
+
if (node.callee.type === 'Identifier') {
|
|
202
|
+
const localFunction = search.localFunctions.get(node.callee.name);
|
|
203
|
+
if (localFunction && containsAssertion(localFunction, search)) {
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return childrenOf(node, search.visitorKeys).some((child) => containsAssertion(child, search));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function isTestCall(node: CallExpression): boolean {
|
|
213
|
+
const callee = node.callee;
|
|
214
|
+
|
|
215
|
+
if (callee.type === 'Identifier') {
|
|
216
|
+
return testFunctionNames.includes(callee.name);
|
|
217
|
+
} else if (callee.type === 'MemberExpression' && callee.object.type === 'Identifier') {
|
|
218
|
+
/** Catches `it.only(...)`, `it.skip(...)`, `test.only(...)`, etc. */
|
|
219
|
+
return testFunctionNames.includes(callee.object.name);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function getTestCallback(
|
|
226
|
+
node: CallExpression,
|
|
227
|
+
): ArrowFunctionExpression | FunctionExpression | undefined {
|
|
228
|
+
return node.arguments.find(
|
|
229
|
+
(argument): argument is ArrowFunctionExpression | FunctionExpression =>
|
|
230
|
+
argument.type === 'FunctionExpression' || argument.type === 'ArrowFunctionExpression',
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* A `@ts-expect-error` (or `@ts-ignore`) directive inside a test is a deliberate compile-time
|
|
236
|
+
* assertion that the following code is a type error, so such tests are treated as having an
|
|
237
|
+
* assertion.
|
|
238
|
+
*/
|
|
239
|
+
const tsDirectiveRegExp = /@ts-(?:expect-error|ignore)\b/;
|
|
240
|
+
|
|
241
|
+
function hasTypeErrorDirective(
|
|
242
|
+
callback: ArrowFunctionExpression | FunctionExpression,
|
|
243
|
+
sourceCode: Rule.RuleContext['sourceCode'],
|
|
244
|
+
): boolean {
|
|
245
|
+
return sourceCode
|
|
246
|
+
.getCommentsInside(callback as unknown as Rule.Node)
|
|
247
|
+
.some((comment) => tsDirectiveRegExp.test(comment.value));
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const rule: Rule.RuleModule = {
|
|
251
|
+
meta: {
|
|
252
|
+
type: 'problem',
|
|
253
|
+
messages: {
|
|
254
|
+
noAssertion: 'Add at least one assertion to this test case.',
|
|
255
|
+
},
|
|
256
|
+
schema: [
|
|
257
|
+
{
|
|
258
|
+
type: 'object',
|
|
259
|
+
properties: {
|
|
260
|
+
additionalAssertionModules: {
|
|
261
|
+
type: 'array',
|
|
262
|
+
items: {
|
|
263
|
+
type: 'string',
|
|
264
|
+
},
|
|
265
|
+
},
|
|
266
|
+
additionalAssertionNames: {
|
|
267
|
+
type: 'array',
|
|
268
|
+
items: {
|
|
269
|
+
type: 'string',
|
|
270
|
+
},
|
|
271
|
+
},
|
|
272
|
+
},
|
|
273
|
+
additionalProperties: false,
|
|
274
|
+
},
|
|
275
|
+
],
|
|
276
|
+
},
|
|
277
|
+
create(context) {
|
|
278
|
+
const options: AssertionsInTestsOptions = context.options[0] || {};
|
|
279
|
+
const modules = [
|
|
280
|
+
...assertionModules,
|
|
281
|
+
...(options.additionalAssertionModules || []),
|
|
282
|
+
];
|
|
283
|
+
const names = [
|
|
284
|
+
...augmentVirAssertionNames,
|
|
285
|
+
...(options.additionalAssertionNames || []),
|
|
286
|
+
];
|
|
287
|
+
|
|
288
|
+
const assertionIdentifiers = collectAssertionIdentifiers(context.sourceCode.ast.body, {
|
|
289
|
+
modules,
|
|
290
|
+
names,
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Only check files that actually import an assertion utility. Files that don't reference
|
|
295
|
+
* any known assertion mechanism are left alone to avoid false positives.
|
|
296
|
+
*/
|
|
297
|
+
if (!assertionIdentifiers.size) {
|
|
298
|
+
return {};
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const visitorKeys = context.sourceCode.visitorKeys;
|
|
302
|
+
const localFunctions = collectLocalFunctions(context.sourceCode.ast, visitorKeys);
|
|
303
|
+
|
|
304
|
+
return {
|
|
305
|
+
CallExpression(node: Rule.Node & CallExpression) {
|
|
306
|
+
if (!isTestCall(node)) {
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const callback = getTestCallback(node);
|
|
311
|
+
if (!callback) {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const hasAssertion = containsAssertion(callback.body, {
|
|
316
|
+
assertionIdentifiers,
|
|
317
|
+
visitorKeys,
|
|
318
|
+
localFunctions,
|
|
319
|
+
visited: new Set<Node>(),
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
if (!hasAssertion && !hasTypeErrorDirective(callback, context.sourceCode)) {
|
|
323
|
+
context.report({
|
|
324
|
+
node,
|
|
325
|
+
messageId: 'noAssertion',
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
},
|
|
329
|
+
};
|
|
330
|
+
},
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
export default rule;
|
|
@@ -26,6 +26,25 @@ type FixableParam = {
|
|
|
26
26
|
optional: boolean;
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Inline callbacks (function or arrow expressions passed directly as call arguments, like
|
|
31
|
+
* `array.reduce((accum, entry, index, wholeArray) => ...)`) have a signature dictated by their
|
|
32
|
+
* caller, so they cannot adopt a params object. Such callbacks should be skipped.
|
|
33
|
+
*/
|
|
34
|
+
function isInlineCallbackArgument(node: AnyFunctionNode): boolean {
|
|
35
|
+
if (node.type === 'FunctionDeclaration') {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const parent = (node as Rule.Node).parent;
|
|
40
|
+
|
|
41
|
+
if (!parent || parent.type !== 'CallExpression') {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return parent.arguments.includes(node);
|
|
46
|
+
}
|
|
47
|
+
|
|
29
48
|
/**
|
|
30
49
|
* Excludes a leading `this` parameter (which is a type-only annotation in TypeScript, not a real
|
|
31
50
|
* positional argument).
|
|
@@ -157,6 +176,10 @@ const rule: Rule.RuleModule = {
|
|
|
157
176
|
const sourceCode = context.sourceCode;
|
|
158
177
|
|
|
159
178
|
function check(node: AnyFunctionNode) {
|
|
179
|
+
if (isInlineCallbackArgument(node)) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
160
183
|
const realParams = getRealParams(node.params);
|
|
161
184
|
|
|
162
185
|
const messageId =
|