/
no-top-level-hooks.js
44 lines (38 loc) · 1.24 KB
/
no-top-level-hooks.js
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
'use strict';
const createAstUtils = require('../util/ast');
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'Disallow top-level hooks',
url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/main/docs/rules/no-top-level-hooks.md'
},
schema: []
},
create(context) {
const astUtils = createAstUtils(context.settings);
const testSuiteStack = [];
return {
CallExpression(node) {
if (astUtils.isDescribe(node)) {
testSuiteStack.push(node);
return;
}
if (!astUtils.isHookIdentifier(node.callee)) {
return;
}
if (testSuiteStack.length === 0) {
context.report({
node: node.callee,
message: `Unexpected use of Mocha \`${ node.callee.name }\` hook outside of a test suite`
});
}
},
'CallExpression:exit'(node) {
if (testSuiteStack[testSuiteStack.length - 1] === node) {
testSuiteStack.pop();
}
}
};
}
};