Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/eslint-plugin-import
Path: blob/main/src/rules/max-dependencies.js
829 views
1
import moduleVisitor from 'eslint-module-utils/moduleVisitor';
2
import docsUrl from '../docsUrl';
3
4
const DEFAULT_MAX = 10;
5
const DEFAULT_IGNORE_TYPE_IMPORTS = false;
6
const TYPE_IMPORT = 'type';
7
8
const countDependencies = (dependencies, lastNode, context) => {
9
const { max } = context.options[0] || { max: DEFAULT_MAX };
10
11
if (dependencies.size > max) {
12
context.report(lastNode, `Maximum number of dependencies (${max}) exceeded.`);
13
}
14
};
15
16
module.exports = {
17
meta: {
18
type: 'suggestion',
19
docs: {
20
url: docsUrl('max-dependencies'),
21
},
22
23
schema: [
24
{
25
'type': 'object',
26
'properties': {
27
'max': { 'type': 'number' },
28
'ignoreTypeImports': { 'type': 'boolean' },
29
},
30
'additionalProperties': false,
31
},
32
],
33
},
34
35
create: context => {
36
const {
37
ignoreTypeImports = DEFAULT_IGNORE_TYPE_IMPORTS,
38
} = context.options[0] || {};
39
40
const dependencies = new Set(); // keep track of dependencies
41
let lastNode; // keep track of the last node to report on
42
43
return Object.assign({
44
'Program:exit': function () {
45
countDependencies(dependencies, lastNode, context);
46
},
47
}, moduleVisitor((source, { importKind }) => {
48
if (importKind !== TYPE_IMPORT || !ignoreTypeImports) {
49
dependencies.add(source.value);
50
}
51
lastNode = source;
52
}, { commonjs: true }));
53
},
54
};
55
56