hypotes 0.0.0-stage → 0.0.0
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/LICENSE +21 -0
- package/README.md +407 -2
- package/dist/equals.d.ts +9 -0
- package/dist/expect.d.ts +14 -0
- package/dist/fast-check.d.ts +26 -0
- package/dist/fast-check.js +32 -0
- package/dist/hoist.d.ts +6 -0
- package/dist/hooks.d.ts +16 -0
- package/dist/index.d.ts +51 -0
- package/dist/index.js +274 -0
- package/dist/mocks-ciQvBYVw.js +281 -0
- package/dist/mocks.d.ts +83 -0
- package/dist/oxlint/index.d.ts +9 -0
- package/dist/oxlint/index.js +831 -0
- package/dist/oxlint/rules.d.ts +16 -0
- package/dist/oxlint/types.d.ts +8 -0
- package/dist/register.d.ts +1 -0
- package/dist/register.js +309 -0
- package/dist/site.d.ts +20 -0
- package/dist/table.d.ts +5 -0
- package/dist/tests-D4v1ZaoR.js +266 -0
- package/dist/tests.d.ts +113 -0
- package/dist/title.d.ts +8 -0
- package/package.json +125 -3
|
@@ -0,0 +1,831 @@
|
|
|
1
|
+
// source/oxlint/rules.ts
|
|
2
|
+
/** Members of `it`, `test`, `describe` and `suite` that are called as they are */
|
|
3
|
+
const VARIANTS = /* @__PURE__ */ new Set([
|
|
4
|
+
"only",
|
|
5
|
+
"skip",
|
|
6
|
+
"todo"
|
|
7
|
+
]);
|
|
8
|
+
/**
|
|
9
|
+
* Members of `it`, `test`, `describe` and `suite` that return the function
|
|
10
|
+
* that declares, as `it.each(rows)` does
|
|
11
|
+
*/
|
|
12
|
+
const FACTORIES = /* @__PURE__ */ new Set([
|
|
13
|
+
"each",
|
|
14
|
+
"for",
|
|
15
|
+
"prop",
|
|
16
|
+
"runIf",
|
|
17
|
+
"skipIf"
|
|
18
|
+
]);
|
|
19
|
+
/**
|
|
20
|
+
* Members of `it`, `test`, `describe` and `suite` that return the function
|
|
21
|
+
* that declares for a table in a template literal, as `` it.each`table` ``
|
|
22
|
+
* does
|
|
23
|
+
*/
|
|
24
|
+
const TABLE_FACTORIES = /* @__PURE__ */ new Set(["each", "for"]);
|
|
25
|
+
/** Members between `expect()` and its matcher */
|
|
26
|
+
const MODIFIERS = /* @__PURE__ */ new Set([
|
|
27
|
+
"not",
|
|
28
|
+
"rejects",
|
|
29
|
+
"resolves"
|
|
30
|
+
]);
|
|
31
|
+
/** Modifiers whose matchers return promises */
|
|
32
|
+
const ASYNC_MODIFIERS = /* @__PURE__ */ new Set(["rejects", "resolves"]);
|
|
33
|
+
/** Matchers of one value, for which others may say more */
|
|
34
|
+
const EQUALITY_MATCHERS = /* @__PURE__ */ new Set([
|
|
35
|
+
"toBe",
|
|
36
|
+
"toEqual",
|
|
37
|
+
"toStrictEqual"
|
|
38
|
+
]);
|
|
39
|
+
const NOT = /* @__PURE__ */ new Set(["not"]);
|
|
40
|
+
const LENGTH = /* @__PURE__ */ new Set(["length"]);
|
|
41
|
+
/** Matchers that take what the error should be, which they may leave out */
|
|
42
|
+
const THROW_MATCHERS = /* @__PURE__ */ new Set(["toThrow", "toThrowError"]);
|
|
43
|
+
/** The methods of a promise that take callbacks, and return another */
|
|
44
|
+
const PROMISE_METHODS = /* @__PURE__ */ new Set([
|
|
45
|
+
"catch",
|
|
46
|
+
"finally",
|
|
47
|
+
"then"
|
|
48
|
+
]);
|
|
49
|
+
/** The methods of `vi` whose calls `hypotes/register` hoists */
|
|
50
|
+
const HOISTED_METHODS = /* @__PURE__ */ new Set([
|
|
51
|
+
"hoisted",
|
|
52
|
+
"mock",
|
|
53
|
+
"unmock"
|
|
54
|
+
]);
|
|
55
|
+
/** The methods of `vi` that do what hoisted ones do, where they're called */
|
|
56
|
+
const UNHOISTED = /* @__PURE__ */ new Map([["mock", "doMock"], ["unmock", "doUnmock"]]);
|
|
57
|
+
/** Files of TypeScript, where a call can take type arguments */
|
|
58
|
+
const TYPESCRIPT = /\.[cm]?tsx?$/u;
|
|
59
|
+
/** The modules that declare tests, which hypotes declares them through */
|
|
60
|
+
const TEST_MODULES = /* @__PURE__ */ new Set([
|
|
61
|
+
"node:test",
|
|
62
|
+
"hypotes",
|
|
63
|
+
"hypotes/fast-check"
|
|
64
|
+
]);
|
|
65
|
+
function roleOfImport(source, name) {
|
|
66
|
+
if (!TEST_MODULES.has(source)) return;
|
|
67
|
+
switch (name) {
|
|
68
|
+
case "describe":
|
|
69
|
+
case "suite": return "describe";
|
|
70
|
+
case "it":
|
|
71
|
+
case "test": return "test";
|
|
72
|
+
case "expect": return source === "hypotes" ? "expect" : void 0;
|
|
73
|
+
case "vi": return source === "hypotes" ? "vi" : void 0;
|
|
74
|
+
default: return;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** The variable that an identifier refers to, from the scopes around it */
|
|
78
|
+
function variableOf(context, identifier) {
|
|
79
|
+
let scope = context.sourceCode.getScope(identifier);
|
|
80
|
+
while (scope !== null) {
|
|
81
|
+
const variable = scope.set.get(identifier.name);
|
|
82
|
+
if (variable !== void 0) return variable;
|
|
83
|
+
scope = scope.upper;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** The named import that an identifier refers to */
|
|
87
|
+
function importOf(context, identifier) {
|
|
88
|
+
const definition = variableOf(context, identifier)?.defs[0];
|
|
89
|
+
if (definition?.type !== "ImportBinding") return;
|
|
90
|
+
const { node: specifier, parent: declaration } = definition;
|
|
91
|
+
if (specifier.type !== "ImportSpecifier" || declaration?.type !== "ImportDeclaration") return;
|
|
92
|
+
const { imported } = specifier;
|
|
93
|
+
const name = imported.type === "Identifier" ? imported.name : imported.value;
|
|
94
|
+
return {
|
|
95
|
+
source: declaration.source.value,
|
|
96
|
+
name
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/** What an identifier is to these rules, from the import that it refers to */
|
|
100
|
+
function roleOf(context, identifier) {
|
|
101
|
+
const imported = importOf(context, identifier);
|
|
102
|
+
return imported && roleOfImport(imported.source, imported.name);
|
|
103
|
+
}
|
|
104
|
+
/** Whether a node is a member named in the source, as `only` is in `it.only` */
|
|
105
|
+
function isStaticMember(node) {
|
|
106
|
+
return node?.type === "MemberExpression" && !node.computed && node.property.type === "Identifier";
|
|
107
|
+
}
|
|
108
|
+
/** Whether a node is a member that one of the names names */
|
|
109
|
+
function isNamedMember(node, names) {
|
|
110
|
+
return isStaticMember(node) && names.has(node.property.name);
|
|
111
|
+
}
|
|
112
|
+
/** The member of a node after it, as `.not` is of `expect(actual)` */
|
|
113
|
+
function memberAfter(node, names) {
|
|
114
|
+
const { parent } = node;
|
|
115
|
+
if (!isStaticMember(parent) || parent.object !== node) return;
|
|
116
|
+
return names === void 0 || names.has(parent.property.name) ? parent : void 0;
|
|
117
|
+
}
|
|
118
|
+
/** The nodes around a node, from its parent out */
|
|
119
|
+
function* ancestorsOf(node) {
|
|
120
|
+
for (let ancestor = node.parent; ancestor !== null; ancestor = ancestor.parent) yield ancestor;
|
|
121
|
+
}
|
|
122
|
+
function isFunction(node) {
|
|
123
|
+
return node.type === "ArrowFunctionExpression" || node.type === "FunctionDeclaration" || node.type === "FunctionExpression";
|
|
124
|
+
}
|
|
125
|
+
/** Whether a call is a test or a describe block */
|
|
126
|
+
function blockOf(context, call) {
|
|
127
|
+
const target = declarerOf(call.callee);
|
|
128
|
+
if (target.type !== "Identifier") return;
|
|
129
|
+
const role = roleOf(context, target);
|
|
130
|
+
return role === "describe" || role === "test" ? role : void 0;
|
|
131
|
+
}
|
|
132
|
+
/** The test or the describe block that a call declares */
|
|
133
|
+
function declarationOf(context, call) {
|
|
134
|
+
const target = declarerOf(call.callee);
|
|
135
|
+
const imported = target.type === "Identifier" ? importOf(context, target) : void 0;
|
|
136
|
+
if (imported === void 0) return;
|
|
137
|
+
const kind = roleOfImport(imported.source, imported.name);
|
|
138
|
+
if (kind !== "describe" && kind !== "test") return;
|
|
139
|
+
const members = [];
|
|
140
|
+
declarerOf(call.callee, members);
|
|
141
|
+
return {
|
|
142
|
+
call,
|
|
143
|
+
kind,
|
|
144
|
+
name: imported.name,
|
|
145
|
+
members
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* What a call's callee declares through: `it` for `it()`, `it.skip()`,
|
|
150
|
+
* `it.each(rows)()`, `` it.each`table`() `` and `it.skipIf(condition).only()`.
|
|
151
|
+
* It adds the members that it passes to `members`, if given.
|
|
152
|
+
*/
|
|
153
|
+
function declarerOf(callee, members) {
|
|
154
|
+
let target = callee;
|
|
155
|
+
for (;;) if (target.type === "CallExpression" && isNamedMember(target.callee, FACTORIES)) {
|
|
156
|
+
members?.push(target.callee);
|
|
157
|
+
target = target.callee.object;
|
|
158
|
+
} else if (target.type === "TaggedTemplateExpression" && isNamedMember(target.tag, TABLE_FACTORIES)) {
|
|
159
|
+
members?.push(target.tag);
|
|
160
|
+
target = target.tag.object;
|
|
161
|
+
} else if (isNamedMember(target, VARIANTS)) {
|
|
162
|
+
members?.push(target);
|
|
163
|
+
target = target.object;
|
|
164
|
+
} else return target;
|
|
165
|
+
}
|
|
166
|
+
/** The member of a declaration with the name, as `.only` of `it.only()` */
|
|
167
|
+
function memberNamed(declaration, name) {
|
|
168
|
+
return declaration.members.find((member) => member.property.name === name);
|
|
169
|
+
}
|
|
170
|
+
/** What a declaration is, in a message */
|
|
171
|
+
function nameOfKind(declaration) {
|
|
172
|
+
return declaration.kind === "test" ? "the test" : "the describe block";
|
|
173
|
+
}
|
|
174
|
+
function isFunctionExpression(node) {
|
|
175
|
+
return node.type === "ArrowFunctionExpression" || node.type === "FunctionExpression";
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* The function that a declaration passes, as hypotes reads it: after the
|
|
179
|
+
* title, or after the options that come before it
|
|
180
|
+
*/
|
|
181
|
+
function functionOf(call) {
|
|
182
|
+
const [, first, second] = call.arguments;
|
|
183
|
+
if (first === void 0 || isFunctionExpression(first)) return first;
|
|
184
|
+
if (second !== void 0 && isFunctionExpression(second)) return second;
|
|
185
|
+
return first.type === "ObjectExpression" ? second : first;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* The option of a declaration with the name, if the declaration passes it
|
|
189
|
+
* in an object written in place, and it's a literal that turns it on, such
|
|
190
|
+
* as `{ skip: true }` or `{ skip: 'a reason' }`
|
|
191
|
+
*/
|
|
192
|
+
function optionNamed(call, name) {
|
|
193
|
+
const options = call.arguments[1];
|
|
194
|
+
if (options?.type !== "ObjectExpression") return;
|
|
195
|
+
for (const property of options.properties) if (property.type === "Property" && keyNameOf(property) === name && isTruthyLiteral(property.value)) return property;
|
|
196
|
+
}
|
|
197
|
+
/** The name of a property's key, if the source names it */
|
|
198
|
+
function keyNameOf(property) {
|
|
199
|
+
const { key } = property;
|
|
200
|
+
if (key.type === "Identifier" && !property.computed) return key.name;
|
|
201
|
+
return key.type === "Literal" && typeof key.value === "string" ? key.value : void 0;
|
|
202
|
+
}
|
|
203
|
+
function isTruthyLiteral(node) {
|
|
204
|
+
return node.type === "Literal" && !("regex" in node) && Boolean(node.value);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Whether a call passes a function written in place: a test's, after its
|
|
208
|
+
* title or its options
|
|
209
|
+
*/
|
|
210
|
+
function hasCallback(call) {
|
|
211
|
+
return call.arguments.some(isFunctionExpression);
|
|
212
|
+
}
|
|
213
|
+
function isExpectCall(context, call) {
|
|
214
|
+
return call.callee.type === "Identifier" && roleOf(context, call.callee) === "expect";
|
|
215
|
+
}
|
|
216
|
+
/** The chain that a call starts, if it calls `expect` */
|
|
217
|
+
function expectChainOf(context, call) {
|
|
218
|
+
if (!isExpectCall(context, call)) return;
|
|
219
|
+
const modifiers = [];
|
|
220
|
+
let end = call;
|
|
221
|
+
let modifier = memberAfter(end, MODIFIERS);
|
|
222
|
+
while (modifier !== void 0) {
|
|
223
|
+
modifiers.push(modifier);
|
|
224
|
+
end = modifier;
|
|
225
|
+
modifier = memberAfter(end, MODIFIERS);
|
|
226
|
+
}
|
|
227
|
+
const matcher = memberAfter(end);
|
|
228
|
+
const parent = matcher?.parent;
|
|
229
|
+
return {
|
|
230
|
+
expect: call,
|
|
231
|
+
modifiers,
|
|
232
|
+
matcher,
|
|
233
|
+
matcherCall: parent?.type === "CallExpression" && parent.callee === matcher ? parent : void 0
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
/** The chain that a call starts, if it compares with toBe(), toEqual() or toStrictEqual() */
|
|
237
|
+
function comparisonOf(context, call) {
|
|
238
|
+
const chain = expectChainOf(context, call);
|
|
239
|
+
if (chain === void 0) return;
|
|
240
|
+
const { matcher, matcherCall } = chain;
|
|
241
|
+
if (matcher === void 0 || matcherCall === void 0 || !EQUALITY_MATCHERS.has(matcher.property.name)) return;
|
|
242
|
+
return {
|
|
243
|
+
...chain,
|
|
244
|
+
matcher,
|
|
245
|
+
matcherCall
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
function hasModifier(chain, names) {
|
|
249
|
+
return chain.modifiers.some((modifier) => names.has(modifier.property.name));
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Whether an expect() call runs in a test, or in a function of its own,
|
|
253
|
+
* which a test may call. A function passed to a call, such as the callback
|
|
254
|
+
* of `forEach()`, runs where the call runs.
|
|
255
|
+
*/
|
|
256
|
+
function runsInTest(context, call) {
|
|
257
|
+
for (const ancestor of ancestorsOf(call)) if (ancestor.type === "CallExpression") {
|
|
258
|
+
const block = blockOf(context, ancestor);
|
|
259
|
+
if (block !== void 0) return block === "test";
|
|
260
|
+
} else if (isFunction(ancestor) && ancestor.parent?.type !== "CallExpression") return true;
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Whether a promise, which a call returns or a variable holds, is awaited or
|
|
265
|
+
* returned, by itself or in an array that a method of Promise takes, as in
|
|
266
|
+
* `await Promise.all([...])`
|
|
267
|
+
*/
|
|
268
|
+
function isAwaitedOrReturned(promise) {
|
|
269
|
+
const { parent } = promise;
|
|
270
|
+
if (parent.type === "AwaitExpression" || parent.type === "ReturnStatement" || parent.type === "ArrowFunctionExpression" && parent.body === promise) return true;
|
|
271
|
+
const combined = promiseCallOf(promise);
|
|
272
|
+
return combined !== void 0 && isAwaitedOrReturned(combined);
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* The call of a method of Promise, such as `Promise.all()`, with an array
|
|
276
|
+
* that the node is an element of
|
|
277
|
+
*/
|
|
278
|
+
function promiseCallOf(node) {
|
|
279
|
+
const array = node.parent;
|
|
280
|
+
const call = array?.parent;
|
|
281
|
+
if (array?.type !== "ArrayExpression" || call?.type !== "CallExpression" || call.callee === array || !isStaticMember(call.callee)) return;
|
|
282
|
+
const { object } = call.callee;
|
|
283
|
+
return object.type === "Identifier" && object.name === "Promise" ? call : void 0;
|
|
284
|
+
}
|
|
285
|
+
function isNull(node) {
|
|
286
|
+
return node.type === "Literal" && !("regex" in node) && node.value === null;
|
|
287
|
+
}
|
|
288
|
+
function isIdentifierNamed(node, name) {
|
|
289
|
+
return node.type === "Identifier" && node.name === name;
|
|
290
|
+
}
|
|
291
|
+
/** Whether a node is a string, number, bigint or boolean written literally */
|
|
292
|
+
function isPrimitiveLiteral(node) {
|
|
293
|
+
switch (node.type) {
|
|
294
|
+
case "Literal": return !("regex" in node) && node.value !== null;
|
|
295
|
+
case "TemplateLiteral": return node.expressions.length === 0;
|
|
296
|
+
case "UnaryExpression": return node.operator === "-" && node.argument.type === "Literal" && (typeof node.argument.value === "number" || typeof node.argument.value === "bigint");
|
|
297
|
+
default: return false;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* The matcher that says more than toBe(), toEqual() or toStrictEqual() of
|
|
302
|
+
* the value expected: toBeNull() of null, for example, or toBe() of a
|
|
303
|
+
* primitive
|
|
304
|
+
*/
|
|
305
|
+
function preferredMatcher(comparison, expected) {
|
|
306
|
+
if (isNull(expected)) return "toBeNull";
|
|
307
|
+
if (isIdentifierNamed(expected, "undefined")) return hasModifier(comparison, NOT) ? "toBeDefined" : "toBeUndefined";
|
|
308
|
+
if (isIdentifierNamed(expected, "NaN")) return "toBeNaN";
|
|
309
|
+
return !(comparison.matcher.property.name === "toBe") && isPrimitiveLiteral(expected) ? "toBe" : void 0;
|
|
310
|
+
}
|
|
311
|
+
/** Writes the preferred matcher in place of the comparison's */
|
|
312
|
+
function replaceMatcher(comparison, preferred) {
|
|
313
|
+
const { expect, matcher, matcherCall, modifiers } = comparison;
|
|
314
|
+
if (preferred === "toBe") return (fixer) => fixer.replaceText(matcher.property, "toBe");
|
|
315
|
+
if (preferred === "toBeDefined") {
|
|
316
|
+
const kept = modifiers.filter((modifier) => modifier.property.name !== "not").map((modifier) => `.${modifier.property.name}`).join("");
|
|
317
|
+
return (fixer) => fixer.replaceTextRange([expect.end, matcherCall.end], `${kept}.toBeDefined()`);
|
|
318
|
+
}
|
|
319
|
+
return (fixer) => fixer.replaceTextRange([matcher.property.start, matcherCall.end], `${preferred}()`);
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Whether a declaration is marked as to do, which it may be without a
|
|
323
|
+
* function
|
|
324
|
+
*/
|
|
325
|
+
function isToDo(declaration) {
|
|
326
|
+
return memberNamed(declaration, "todo") !== void 0 || optionNamed(declaration.call, "todo") !== void 0;
|
|
327
|
+
}
|
|
328
|
+
/** Whether an if statement is around a node, which runs only on its condition */
|
|
329
|
+
function isInIfStatement(node) {
|
|
330
|
+
for (const ancestor of ancestorsOf(node)) if (ancestor.type === "IfStatement") return true;
|
|
331
|
+
return false;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* What a describe block's function returns: the first return statement at
|
|
335
|
+
* the top of its body, or the call that an arrow function's body is
|
|
336
|
+
*/
|
|
337
|
+
function returnOf(callback) {
|
|
338
|
+
const { body } = callback;
|
|
339
|
+
if (body?.type !== "BlockStatement") return body?.type === "CallExpression" ? body : void 0;
|
|
340
|
+
return body.body.find((statement) => statement.type === "ReturnStatement");
|
|
341
|
+
}
|
|
342
|
+
function titleOf(context, call) {
|
|
343
|
+
const [title] = call.arguments;
|
|
344
|
+
if (title?.type === "Literal" && typeof title.value === "string") {
|
|
345
|
+
const raw = context.sourceCode.getText(title).slice(1, -1);
|
|
346
|
+
return {
|
|
347
|
+
node: title,
|
|
348
|
+
cooked: title.value,
|
|
349
|
+
raw
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
if (title?.type !== "TemplateLiteral" || title.expressions.length > 0) return;
|
|
353
|
+
const [{ value }] = title.quasis;
|
|
354
|
+
return value.cooked === null ? void 0 : {
|
|
355
|
+
node: title,
|
|
356
|
+
cooked: value.cooked,
|
|
357
|
+
raw: value.raw
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
/** The range of a title's text, between its quotes */
|
|
361
|
+
function textRangeOf(title) {
|
|
362
|
+
return [title.node.start + 1, title.node.end - 1];
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Whether the cooked text and the raw text of a title have as much
|
|
366
|
+
* whitespace at their starts and their ends, which trimming the raw text
|
|
367
|
+
* removes, unless the raw text escapes it, as in `'\t'`
|
|
368
|
+
*/
|
|
369
|
+
function canTrimRaw(cooked, raw) {
|
|
370
|
+
return cooked.length - cooked.trimStart().length === raw.length - raw.trimStart().length && cooked.length - cooked.trimEnd().length === raw.length - raw.trimEnd().length;
|
|
371
|
+
}
|
|
372
|
+
/** Whether a text's first word, before its first space, is the word */
|
|
373
|
+
function startsWithWord(text, word) {
|
|
374
|
+
return text.startsWith(word) && (text.length === word.length || text[word.length] === " ");
|
|
375
|
+
}
|
|
376
|
+
function reportSpaces(context, title) {
|
|
377
|
+
const { cooked, raw } = title;
|
|
378
|
+
const canFix = canTrimRaw(cooked, raw);
|
|
379
|
+
context.report({
|
|
380
|
+
node: title.node,
|
|
381
|
+
message: "Expected a title without spaces at its start or end",
|
|
382
|
+
fix: (fixer) => canFix ? fixer.replaceTextRange(textRangeOf(title), raw.trim()) : null
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
/** Reports a title whose first word is the name that declares it */
|
|
386
|
+
function reportPrefix(context, title, name) {
|
|
387
|
+
const { cooked, raw } = title;
|
|
388
|
+
const rest = cooked.slice(name.length + 1);
|
|
389
|
+
const rawRest = raw.slice(name.length + 1);
|
|
390
|
+
const canFix = rest.length > 0 && raw.startsWith(`${name} `) && canTrimRaw(rest, rawRest);
|
|
391
|
+
context.report({
|
|
392
|
+
node: title.node,
|
|
393
|
+
message: `Expected the title not to start with ${name}, which declares it`,
|
|
394
|
+
fix: (fixer) => canFix ? fixer.replaceTextRange(textRangeOf(title), rawRest.trim()) : null
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* The call of .then(), .catch() or .finally() that takes a function as one
|
|
399
|
+
* of its callbacks, its first two arguments
|
|
400
|
+
*/
|
|
401
|
+
function promiseCallTaking(fn) {
|
|
402
|
+
const call = fn.parent;
|
|
403
|
+
if (call.type !== "CallExpression" || !isNamedMember(call.callee, PROMISE_METHODS)) return;
|
|
404
|
+
const [onFulfilled, onRejected] = call.arguments;
|
|
405
|
+
return fn === onFulfilled || fn === onRejected ? call : void 0;
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* The last call of the chain of .then(), .catch() and .finally() that a
|
|
409
|
+
* call is in, whose promise the chain returns
|
|
410
|
+
*/
|
|
411
|
+
function chainEndOf(call) {
|
|
412
|
+
let end = call;
|
|
413
|
+
for (;;) {
|
|
414
|
+
const member = memberAfter(end, PROMISE_METHODS);
|
|
415
|
+
const next = member?.parent;
|
|
416
|
+
if (next?.type !== "CallExpression" || next.callee !== member) return end;
|
|
417
|
+
end = next;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* The promises whose callbacks run a node, from the inside out, until the
|
|
422
|
+
* declaration that runs it: the test waits for the node only if it waits
|
|
423
|
+
* for each of them
|
|
424
|
+
*/
|
|
425
|
+
function* promisesAround(context, node) {
|
|
426
|
+
for (const ancestor of ancestorsOf(node)) {
|
|
427
|
+
if (ancestor.type === "CallExpression" && blockOf(context, ancestor) !== void 0) return;
|
|
428
|
+
const call = isFunctionExpression(ancestor) ? promiseCallTaking(ancestor) : void 0;
|
|
429
|
+
if (call !== void 0) yield chainEndOf(call);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Whether a promise is awaited or returned, or expected to resolve or
|
|
434
|
+
* reject, where it is, or through the variable that holds it
|
|
435
|
+
*/
|
|
436
|
+
function isWaitedFor(context, promise) {
|
|
437
|
+
if (isWaitedForInPlace(context, promise)) return true;
|
|
438
|
+
return variableHolding(context, promise)?.references.some((reference) => reference.isRead() && isWaitedForInPlace(context, reference.identifier)) ?? false;
|
|
439
|
+
}
|
|
440
|
+
function isWaitedForInPlace(context, promise) {
|
|
441
|
+
return isAwaitedOrReturned(promise) || isExpectedToSettle(context, promise);
|
|
442
|
+
}
|
|
443
|
+
/** Whether expect() takes a promise, to assert with .resolves or .rejects */
|
|
444
|
+
function isExpectedToSettle(context, promise) {
|
|
445
|
+
const { parent } = promise;
|
|
446
|
+
if (parent.type !== "CallExpression" || parent.arguments[0] !== promise) return false;
|
|
447
|
+
const chain = expectChainOf(context, parent);
|
|
448
|
+
return chain !== void 0 && hasModifier(chain, ASYNC_MODIFIERS);
|
|
449
|
+
}
|
|
450
|
+
/** The variable that a promise is assigned to, where it's declared or later */
|
|
451
|
+
function variableHolding(context, promise) {
|
|
452
|
+
const { parent } = promise;
|
|
453
|
+
if (parent.type === "VariableDeclarator" && parent.init === promise && parent.id.type === "Identifier") return variableOf(context, parent.id);
|
|
454
|
+
if (parent.type === "AssignmentExpression" && parent.right === promise && parent.left.type === "Identifier") return variableOf(context, parent.left);
|
|
455
|
+
}
|
|
456
|
+
/** The member of hypotes' `vi` that a call calls, as `vi.fn` */
|
|
457
|
+
function viMethodOf(context, call) {
|
|
458
|
+
const { callee } = call;
|
|
459
|
+
return isStaticMember(callee) && callee.object.type === "Identifier" && roleOf(context, callee.object) === "vi" ? callee : void 0;
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Whether a call of a hoisted method of `vi` is where `hypotes/register`
|
|
463
|
+
* hoists it from: a statement of its own at the module's top level, which
|
|
464
|
+
* may await `vi.hoisted()`, and declare the one variable that it returns
|
|
465
|
+
*/
|
|
466
|
+
function isHoistable(call, method) {
|
|
467
|
+
const expression = method === "hoisted" && call.parent.type === "AwaitExpression" ? call.parent : call;
|
|
468
|
+
const { parent } = expression;
|
|
469
|
+
if (parent?.type === "ExpressionStatement") return parent.parent.type === "Program";
|
|
470
|
+
if (method !== "hoisted" || parent?.type !== "VariableDeclarator" || parent.init !== expression) return false;
|
|
471
|
+
const declaration = parent.parent;
|
|
472
|
+
return declaration.type === "VariableDeclaration" && declaration.declarations.length === 1 && declaration.parent.type === "Program";
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* The fixes that a hoisted call can take: moving its statement to the
|
|
476
|
+
* module's top level, after the imports, or calling the method of `vi`
|
|
477
|
+
* that isn't hoisted, such as `vi.doMock()`
|
|
478
|
+
*/
|
|
479
|
+
function hoistingSuggestions(context, call, method) {
|
|
480
|
+
const suggestions = [];
|
|
481
|
+
const statement = call.parent;
|
|
482
|
+
if (statement.type === "ExpressionStatement") suggestions.push(moveToTopLevel(context, statement));
|
|
483
|
+
const unhoisted = UNHOISTED.get(method.property.name);
|
|
484
|
+
if (unhoisted !== void 0) suggestions.push({
|
|
485
|
+
desc: `Call vi.${unhoisted}(), which runs where it's called`,
|
|
486
|
+
fix: (fixer) => fixer.replaceText(method.property, unhoisted)
|
|
487
|
+
});
|
|
488
|
+
return suggestions;
|
|
489
|
+
}
|
|
490
|
+
function moveToTopLevel(context, statement) {
|
|
491
|
+
const { sourceCode } = context;
|
|
492
|
+
const text = sourceCode.getText(statement);
|
|
493
|
+
const end = sourceCode.ast.body.findLast((node) => node.type === "ImportDeclaration")?.end ?? 0;
|
|
494
|
+
return {
|
|
495
|
+
desc: "Move it to the module's top level, after its imports",
|
|
496
|
+
fix: (fixer) => [fixer.remove(statement), fixer.insertTextAfterRange([end, end], `\n${text}`)]
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
// source/oxlint/index.ts
|
|
500
|
+
const plugin = {
|
|
501
|
+
meta: { name: "hypotes" },
|
|
502
|
+
rules: {
|
|
503
|
+
"no-focused-tests": {
|
|
504
|
+
meta: {
|
|
505
|
+
type: "problem",
|
|
506
|
+
hasSuggestions: true,
|
|
507
|
+
docs: { description: "Disallows .only and the only option, which run the tests that they mark rather than every test" }
|
|
508
|
+
},
|
|
509
|
+
create(context) {
|
|
510
|
+
return { CallExpression(call) {
|
|
511
|
+
const declaration = declarationOf(context, call);
|
|
512
|
+
if (declaration === void 0) return;
|
|
513
|
+
const only = memberNamed(declaration, "only");
|
|
514
|
+
if (only !== void 0) context.report({
|
|
515
|
+
node: only.property,
|
|
516
|
+
message: "Expected every test to run, rather than those that .only marks",
|
|
517
|
+
suggest: [{
|
|
518
|
+
desc: "Remove .only",
|
|
519
|
+
fix: (fixer) => fixer.removeRange([only.object.end, only.end])
|
|
520
|
+
}]
|
|
521
|
+
});
|
|
522
|
+
const option = optionNamed(call, "only");
|
|
523
|
+
if (option !== void 0) context.report({
|
|
524
|
+
node: option,
|
|
525
|
+
message: "Expected every test to run, rather than those that the only option marks"
|
|
526
|
+
});
|
|
527
|
+
} };
|
|
528
|
+
}
|
|
529
|
+
},
|
|
530
|
+
"no-disabled-tests": {
|
|
531
|
+
meta: {
|
|
532
|
+
type: "problem",
|
|
533
|
+
docs: { description: "Disallows .skip and the skip option, and tests without functions, which are to do unless marked so" }
|
|
534
|
+
},
|
|
535
|
+
create(context) {
|
|
536
|
+
return { CallExpression(call) {
|
|
537
|
+
const declaration = declarationOf(context, call);
|
|
538
|
+
if (declaration === void 0) return;
|
|
539
|
+
const kind = nameOfKind(declaration);
|
|
540
|
+
const skip = memberNamed(declaration, "skip");
|
|
541
|
+
if (skip !== void 0) context.report({
|
|
542
|
+
node: skip.property,
|
|
543
|
+
message: `Expected ${kind} to run, rather than be skipped with .skip`
|
|
544
|
+
});
|
|
545
|
+
const option = optionNamed(call, "skip");
|
|
546
|
+
if (option !== void 0) context.report({
|
|
547
|
+
node: option,
|
|
548
|
+
message: `Expected ${kind} to run, rather than be skipped with the skip option`
|
|
549
|
+
});
|
|
550
|
+
if (declaration.kind === "test" && functionOf(call) === void 0 && !isToDo(declaration)) context.report({
|
|
551
|
+
node: call,
|
|
552
|
+
message: "Expected the test to have a function, without which it's to do"
|
|
553
|
+
});
|
|
554
|
+
} };
|
|
555
|
+
}
|
|
556
|
+
},
|
|
557
|
+
"warn-todo": {
|
|
558
|
+
meta: {
|
|
559
|
+
type: "problem",
|
|
560
|
+
docs: { description: "Disallows .todo and the todo option, which mark tests that are to be written" }
|
|
561
|
+
},
|
|
562
|
+
create(context) {
|
|
563
|
+
return { CallExpression(call) {
|
|
564
|
+
const declaration = declarationOf(context, call);
|
|
565
|
+
if (declaration === void 0) return;
|
|
566
|
+
const kind = nameOfKind(declaration);
|
|
567
|
+
const todo = memberNamed(declaration, "todo");
|
|
568
|
+
if (todo !== void 0) context.report({
|
|
569
|
+
node: todo.property,
|
|
570
|
+
message: `Expected ${kind} to be written, rather than marked as to do with .todo`
|
|
571
|
+
});
|
|
572
|
+
const option = optionNamed(call, "todo");
|
|
573
|
+
if (option !== void 0) context.report({
|
|
574
|
+
node: option,
|
|
575
|
+
message: `Expected ${kind} to be written, rather than marked as to do with the todo option`
|
|
576
|
+
});
|
|
577
|
+
} };
|
|
578
|
+
}
|
|
579
|
+
},
|
|
580
|
+
"no-conditional-tests": {
|
|
581
|
+
meta: {
|
|
582
|
+
type: "problem",
|
|
583
|
+
docs: { description: "Disallows declaring tests and describe blocks in if statements, rather than skipping them on a condition with skipIf() or runIf()" }
|
|
584
|
+
},
|
|
585
|
+
create(context) {
|
|
586
|
+
return { CallExpression(call) {
|
|
587
|
+
const declaration = declarationOf(context, call);
|
|
588
|
+
if (declaration === void 0 || !isInIfStatement(call)) return;
|
|
589
|
+
context.report({
|
|
590
|
+
node: call.callee,
|
|
591
|
+
message: `Expected ${nameOfKind(declaration)} to be declared unconditionally, rather than in an if statement: skipIf() and runIf() skip it on a condition`
|
|
592
|
+
});
|
|
593
|
+
} };
|
|
594
|
+
}
|
|
595
|
+
},
|
|
596
|
+
"valid-title": {
|
|
597
|
+
meta: {
|
|
598
|
+
type: "problem",
|
|
599
|
+
fixable: "code",
|
|
600
|
+
docs: { description: "Requires the title of a test or a describe block to have text, without spaces at its start or end, and not to start with the name that declares it, as in it('it works'). TypeScript checks that it's a string." }
|
|
601
|
+
},
|
|
602
|
+
create(context) {
|
|
603
|
+
return { CallExpression(call) {
|
|
604
|
+
const declaration = declarationOf(context, call);
|
|
605
|
+
const title = declaration && titleOf(context, call);
|
|
606
|
+
if (declaration === void 0 || title === void 0) return;
|
|
607
|
+
if (title.cooked === "") {
|
|
608
|
+
context.report({
|
|
609
|
+
node: title.node,
|
|
610
|
+
message: "Expected a title that has text"
|
|
611
|
+
});
|
|
612
|
+
return;
|
|
613
|
+
}
|
|
614
|
+
if (/^\s|\s$/u.test(title.cooked)) reportSpaces(context, title);
|
|
615
|
+
if (startsWithWord(title.cooked, declaration.name)) reportPrefix(context, title, declaration.name);
|
|
616
|
+
} };
|
|
617
|
+
}
|
|
618
|
+
},
|
|
619
|
+
"valid-describe-callback": {
|
|
620
|
+
meta: {
|
|
621
|
+
type: "problem",
|
|
622
|
+
docs: { description: "Requires a describe block to pass a function written in place, which takes no parameters, unless it declares a block for each row, and returns nothing. It may be async, and its options may come before it." }
|
|
623
|
+
},
|
|
624
|
+
create(context) {
|
|
625
|
+
return { CallExpression(call) {
|
|
626
|
+
const declaration = declarationOf(context, call);
|
|
627
|
+
if (declaration?.kind !== "describe") return;
|
|
628
|
+
const callback = functionOf(call);
|
|
629
|
+
if (callback === void 0) {
|
|
630
|
+
if (!isToDo(declaration)) context.report({
|
|
631
|
+
node: call.arguments[0] ?? call,
|
|
632
|
+
message: "Expected a title and a function for the describe block"
|
|
633
|
+
});
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
if (!isFunctionExpression(callback)) {
|
|
637
|
+
context.report({
|
|
638
|
+
node: callback,
|
|
639
|
+
message: "Expected the describe block's function to be written in place"
|
|
640
|
+
});
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
if ((memberNamed(declaration, "each") ?? memberNamed(declaration, "for")) === void 0 && callback.params.length > 0) context.report({
|
|
644
|
+
node: callback,
|
|
645
|
+
message: "Expected the describe block's function to take no parameters"
|
|
646
|
+
});
|
|
647
|
+
const returned = returnOf(callback);
|
|
648
|
+
if (returned !== void 0) context.report({
|
|
649
|
+
node: returned,
|
|
650
|
+
message: "Expected the describe block's function to return nothing"
|
|
651
|
+
});
|
|
652
|
+
} };
|
|
653
|
+
}
|
|
654
|
+
},
|
|
655
|
+
"expect-expect": {
|
|
656
|
+
meta: {
|
|
657
|
+
type: "problem",
|
|
658
|
+
docs: { description: "Requires each test written in place to call expect(), or a function whose name starts with expect" }
|
|
659
|
+
},
|
|
660
|
+
create(context) {
|
|
661
|
+
const unasserted = /* @__PURE__ */ new Set();
|
|
662
|
+
return {
|
|
663
|
+
CallExpression(call) {
|
|
664
|
+
const { callee } = call;
|
|
665
|
+
if (callee.type === "Identifier" && callee.name.startsWith("expect")) {
|
|
666
|
+
for (const ancestor of ancestorsOf(call)) if (ancestor.type === "CallExpression") unasserted.delete(ancestor);
|
|
667
|
+
} else if (hasCallback(call) && blockOf(context, call) === "test") unasserted.add(call);
|
|
668
|
+
},
|
|
669
|
+
"Program:exit"() {
|
|
670
|
+
for (const test of unasserted) context.report({
|
|
671
|
+
node: test.callee,
|
|
672
|
+
message: "Expected the test to call expect(), or a function whose name starts with expect"
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
};
|
|
676
|
+
}
|
|
677
|
+
},
|
|
678
|
+
"no-standalone-expect": {
|
|
679
|
+
meta: {
|
|
680
|
+
type: "problem",
|
|
681
|
+
docs: { description: "Requires expect() to run in a test, or in a function of its own, rather than in a describe block or at the top of a module" }
|
|
682
|
+
},
|
|
683
|
+
create(context) {
|
|
684
|
+
return { CallExpression(call) {
|
|
685
|
+
if (isExpectCall(context, call) && !runsInTest(context, call)) context.report({
|
|
686
|
+
node: call,
|
|
687
|
+
message: "Expected expect() in a test, or in a function of its own, rather than in a describe block or at the top of the module"
|
|
688
|
+
});
|
|
689
|
+
} };
|
|
690
|
+
}
|
|
691
|
+
},
|
|
692
|
+
"valid-expect": {
|
|
693
|
+
meta: {
|
|
694
|
+
type: "problem",
|
|
695
|
+
docs: { description: "Requires a matcher to be called on expect(), and the promise of .resolves or .rejects to be awaited or returned. TypeScript checks the arguments of expect()." }
|
|
696
|
+
},
|
|
697
|
+
create(context) {
|
|
698
|
+
return { CallExpression(call) {
|
|
699
|
+
const chain = expectChainOf(context, call);
|
|
700
|
+
if (chain === void 0) return;
|
|
701
|
+
const { matcherCall } = chain;
|
|
702
|
+
if (matcherCall === void 0) context.report({
|
|
703
|
+
node: call,
|
|
704
|
+
message: "Expected a matcher, such as toBe(), to be called on expect()"
|
|
705
|
+
});
|
|
706
|
+
else if (hasModifier(chain, ASYNC_MODIFIERS) && !isAwaitedOrReturned(matcherCall)) context.report({
|
|
707
|
+
node: matcherCall,
|
|
708
|
+
message: "Expected the promise of .resolves or .rejects to be awaited or returned"
|
|
709
|
+
});
|
|
710
|
+
} };
|
|
711
|
+
}
|
|
712
|
+
},
|
|
713
|
+
"valid-expect-in-promise": {
|
|
714
|
+
meta: {
|
|
715
|
+
type: "problem",
|
|
716
|
+
docs: { description: "Requires a promise whose callback calls expect(), as .then() does, to be awaited or returned, by itself or through the variable that holds it, so that the test waits for the assertion" }
|
|
717
|
+
},
|
|
718
|
+
create(context) {
|
|
719
|
+
const reported = /* @__PURE__ */ new Set();
|
|
720
|
+
return { CallExpression(call) {
|
|
721
|
+
if (!isExpectCall(context, call)) return;
|
|
722
|
+
for (const promise of promisesAround(context, call)) if (!reported.has(promise) && runsInTest(context, promise) && !isWaitedFor(context, promise)) {
|
|
723
|
+
reported.add(promise);
|
|
724
|
+
context.report({
|
|
725
|
+
node: promise,
|
|
726
|
+
message: "Expected the promise whose callback calls expect() to be awaited or returned"
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
} };
|
|
730
|
+
}
|
|
731
|
+
},
|
|
732
|
+
"require-to-throw-message": {
|
|
733
|
+
meta: {
|
|
734
|
+
type: "problem",
|
|
735
|
+
docs: { description: "Requires toThrow() and toThrowError() to be given what the error should be, such as its message, unless they follow .not" }
|
|
736
|
+
},
|
|
737
|
+
create(context) {
|
|
738
|
+
return { CallExpression(call) {
|
|
739
|
+
const chain = expectChainOf(context, call);
|
|
740
|
+
const matcher = chain?.matcher;
|
|
741
|
+
if (chain === void 0 || matcher === void 0 || chain.matcherCall?.arguments.length !== 0 || !THROW_MATCHERS.has(matcher.property.name) || hasModifier(chain, NOT)) return;
|
|
742
|
+
context.report({
|
|
743
|
+
node: matcher.property,
|
|
744
|
+
message: `Expected ${matcher.property.name}() to be given what the error should be, such as its message`
|
|
745
|
+
});
|
|
746
|
+
} };
|
|
747
|
+
}
|
|
748
|
+
},
|
|
749
|
+
"prefer-to-be": {
|
|
750
|
+
meta: {
|
|
751
|
+
type: "suggestion",
|
|
752
|
+
fixable: "code",
|
|
753
|
+
docs: { description: "Prefers toBe() of a primitive, which compares with Object.is, to toEqual() and toStrictEqual(), and toBeNull(), toBeUndefined(), toBeDefined() and toBeNaN() to comparisons with those values" }
|
|
754
|
+
},
|
|
755
|
+
create(context) {
|
|
756
|
+
return { CallExpression(call) {
|
|
757
|
+
const comparison = comparisonOf(context, call);
|
|
758
|
+
const [expected] = comparison?.matcherCall.arguments ?? [];
|
|
759
|
+
if (comparison === void 0 || expected === void 0 || comparison.matcherCall.arguments.length !== 1) return;
|
|
760
|
+
const preferred = preferredMatcher(comparison, expected);
|
|
761
|
+
if (preferred === void 0) return;
|
|
762
|
+
const matcher = comparison.matcher.property.name;
|
|
763
|
+
const found = preferred === "toBeDefined" ? `not.${matcher}` : matcher;
|
|
764
|
+
const value = context.sourceCode.getText(expected);
|
|
765
|
+
context.report({
|
|
766
|
+
node: comparison.matcher.property,
|
|
767
|
+
message: `Use ${preferred}() rather than ${found}(${value})`,
|
|
768
|
+
fix: replaceMatcher(comparison, preferred)
|
|
769
|
+
});
|
|
770
|
+
} };
|
|
771
|
+
}
|
|
772
|
+
},
|
|
773
|
+
"prefer-to-have-length": {
|
|
774
|
+
meta: {
|
|
775
|
+
type: "suggestion",
|
|
776
|
+
fixable: "code",
|
|
777
|
+
docs: { description: "Prefers expect(values).toHaveLength(n) to comparing expect(values.length) with n" }
|
|
778
|
+
},
|
|
779
|
+
create(context) {
|
|
780
|
+
return { CallExpression(call) {
|
|
781
|
+
const comparison = comparisonOf(context, call);
|
|
782
|
+
const [actual] = call.arguments;
|
|
783
|
+
if (comparison === void 0 || call.arguments.length !== 1 || actual === void 0 || !isNamedMember(actual, LENGTH)) return;
|
|
784
|
+
const values = context.sourceCode.getText(actual.object);
|
|
785
|
+
context.report({
|
|
786
|
+
node: actual,
|
|
787
|
+
message: `Use toHaveLength() rather than comparing ${values}.length`,
|
|
788
|
+
fix: (fixer) => [fixer.replaceText(actual, values), fixer.replaceText(comparison.matcher.property, "toHaveLength")]
|
|
789
|
+
});
|
|
790
|
+
} };
|
|
791
|
+
}
|
|
792
|
+
},
|
|
793
|
+
"hoisted-apis-on-top": {
|
|
794
|
+
meta: {
|
|
795
|
+
type: "problem",
|
|
796
|
+
hasSuggestions: true,
|
|
797
|
+
docs: { description: "Requires vi.mock(), vi.unmock() and vi.hoisted() to be called in statements of their own at the module's top level, which hypotes/register hoists them from" }
|
|
798
|
+
},
|
|
799
|
+
create(context) {
|
|
800
|
+
return { CallExpression(call) {
|
|
801
|
+
const method = viMethodOf(context, call);
|
|
802
|
+
const name = method?.property.name;
|
|
803
|
+
if (method === void 0 || name === void 0 || !HOISTED_METHODS.has(name) || isHoistable(call, name)) return;
|
|
804
|
+
context.report({
|
|
805
|
+
node: call,
|
|
806
|
+
message: `Expected vi.${name}() in a statement of its own at the module's top level: it's hoisted above the module's code`,
|
|
807
|
+
suggest: hoistingSuggestions(context, call, method)
|
|
808
|
+
});
|
|
809
|
+
} };
|
|
810
|
+
}
|
|
811
|
+
},
|
|
812
|
+
"require-mock-type-parameters": {
|
|
813
|
+
meta: {
|
|
814
|
+
type: "problem",
|
|
815
|
+
docs: { description: "Requires vi.fn() to be given the type of the function that it mocks, in TypeScript" }
|
|
816
|
+
},
|
|
817
|
+
create(context) {
|
|
818
|
+
if (!TYPESCRIPT.test(context.filename)) return {};
|
|
819
|
+
return { CallExpression(call) {
|
|
820
|
+
const method = viMethodOf(context, call);
|
|
821
|
+
if (method?.property.name !== "fn" || call.typeArguments) return;
|
|
822
|
+
context.report({
|
|
823
|
+
node: method.property,
|
|
824
|
+
message: "Expected vi.fn() to be given the type of the function that it mocks, as in vi.fn<() => void>()"
|
|
825
|
+
});
|
|
826
|
+
} };
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
};
|
|
831
|
+
export { plugin as default };
|