/
no-restricted-jest-methods.ts
59 lines (53 loc) · 1.44 KB
/
no-restricted-jest-methods.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { createRule, getAccessorValue, parseJestFnCall } from './utils';
const messages = {
restrictedJestMethod: 'Use of `{{ restriction }}` is disallowed',
restrictedJestMethodWithMessage: '{{ message }}',
};
export default createRule<
[Record<string, string | null>],
keyof typeof messages
>({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Disallow specific `jest.` methods',
recommended: false,
},
type: 'suggestion',
schema: [
{
type: 'object',
additionalProperties: {
type: ['string', 'null'],
},
},
],
messages,
},
defaultOptions: [{}],
create(context, [restrictedMethods]) {
return {
CallExpression(node) {
const jestFnCall = parseJestFnCall(node, context);
if (jestFnCall?.type !== 'jest') {
return;
}
const method = getAccessorValue(jestFnCall.members[0]);
if (method in restrictedMethods) {
const message = restrictedMethods[method];
context.report({
messageId: message
? 'restrictedJestMethodWithMessage'
: 'restrictedJestMethod',
data: { message, restriction: method },
loc: {
start: jestFnCall.members[0].loc.start,
end: jestFnCall.members[jestFnCall.members.length - 1].loc.end,
},
});
}
},
};
},
});