/
max-top-level-suites.js
69 lines (60 loc) · 1.89 KB
/
max-top-level-suites.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict';
/**
* @fileoverview Limit the number of top-level suites in a single file
* @author Alexander Afanasyev
*/
const { isNil } = require('rambda');
const createAstUtils = require('../util/ast');
const defaultSuiteLimit = 1;
function isTopLevelScope(scope) {
return scope.type === 'module' || scope.upper === null;
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'Enforce the number of top-level suites in a single file',
url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/main/docs/rules/max-top-level-suites.md'
},
schema: [
{
type: 'object',
properties: {
limit: {
type: 'integer'
}
},
additionalProperties: false
}
]
},
create(context) {
const astUtils = createAstUtils(context.settings);
const topLevelDescribes = [];
const options = context.options[0] || {};
let suiteLimit;
if (isNil(options.limit)) {
suiteLimit = defaultSuiteLimit;
} else {
suiteLimit = options.limit;
}
return {
CallExpression(node) {
if (astUtils.isDescribe(node)) {
const scope = context.sourceCode.getScope(node);
if (isTopLevelScope(scope)) {
topLevelDescribes.push(node);
}
}
},
'Program:exit'() {
if (topLevelDescribes.length > suiteLimit) {
context.report({
node: topLevelDescribes[suiteLimit],
message: `The number of top-level suites is more than ${ suiteLimit }.`
});
}
}
};
}
};