Path: blob/main/src/rules/max-dependencies.js
829 views
import moduleVisitor from 'eslint-module-utils/moduleVisitor';1import docsUrl from '../docsUrl';23const DEFAULT_MAX = 10;4const DEFAULT_IGNORE_TYPE_IMPORTS = false;5const TYPE_IMPORT = 'type';67const countDependencies = (dependencies, lastNode, context) => {8const { max } = context.options[0] || { max: DEFAULT_MAX };910if (dependencies.size > max) {11context.report(lastNode, `Maximum number of dependencies (${max}) exceeded.`);12}13};1415module.exports = {16meta: {17type: 'suggestion',18docs: {19url: docsUrl('max-dependencies'),20},2122schema: [23{24'type': 'object',25'properties': {26'max': { 'type': 'number' },27'ignoreTypeImports': { 'type': 'boolean' },28},29'additionalProperties': false,30},31],32},3334create: context => {35const {36ignoreTypeImports = DEFAULT_IGNORE_TYPE_IMPORTS,37} = context.options[0] || {};3839const dependencies = new Set(); // keep track of dependencies40let lastNode; // keep track of the last node to report on4142return Object.assign({43'Program:exit': function () {44countDependencies(dependencies, lastNode, context);45},46}, moduleVisitor((source, { importKind }) => {47if (importKind !== TYPE_IMPORT || !ignoreTypeImports) {48dependencies.add(source.value);49}50lastNode = source;51}, { commonjs: true }));52},53};545556