-
Notifications
You must be signed in to change notification settings - Fork 238
/
no-duplicate-hooks.ts
50 lines (44 loc) · 1.23 KB
/
no-duplicate-hooks.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
import { createRule, isTypeOfJestFnCall, parseJestFnCall } from './utils';
export default createRule({
name: __filename,
meta: {
docs: {
description: 'Disallow duplicate setup and teardown hooks',
},
messages: {
noDuplicateHook: 'Duplicate {{hook}} in describe block',
},
schema: [],
type: 'suggestion',
},
defaultOptions: [],
create(context) {
const hookContexts: Array<Record<string, number>> = [{}];
return {
CallExpression(node) {
const jestFnCall = parseJestFnCall(node, context);
if (jestFnCall?.type === 'describe') {
hookContexts.push({});
}
if (jestFnCall?.type !== 'hook') {
return;
}
const currentLayer = hookContexts[hookContexts.length - 1];
currentLayer[jestFnCall.name] ||= 0;
currentLayer[jestFnCall.name] += 1;
if (currentLayer[jestFnCall.name] > 1) {
context.report({
messageId: 'noDuplicateHook',
data: { hook: jestFnCall.name },
node,
});
}
},
'CallExpression:exit'(node) {
if (isTypeOfJestFnCall(node, context, ['describe'])) {
hookContexts.pop();
}
},
};
},
});