UNPKG

eslint-plugin-mocha

Version:

Eslint rules for mocha.

55 lines 1.99 kB
import { createMochaVisitors } from "../ast/mocha-visitors.js"; import { getRuleOption } from "../rule-options.js"; const defaultSuiteLimit = 1; const optionSchema = { type: 'object', properties: { limit: { type: 'integer' } }, additionalProperties: false }; const defaultOption = { limit: defaultSuiteLimit }; function isTopLevelScope(scope) { return scope.type === 'module' || scope.upper === null; } export const maxTopLevelSuitesRule = { meta: { type: 'suggestion', docs: { description: 'Enforce the number of top-level suites in a single file', recommended: false, url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/main/documentation/rules/max-top-level-suites.md' }, schema: [optionSchema], defaultOptions: [defaultOption], messages: { tooManyTopLevelSuites: 'The number of top-level suites is more than {{limit}}.' }, languages: ['js/js'] }, create(context) { const topLevelSuites = []; const { limit: suiteLimit } = getRuleOption(context); return createMochaVisitors(context, { suite(visitorContext) { const scope = context.sourceCode.getScope(visitorContext.node); if (isTopLevelScope(scope)) { topLevelSuites.push(visitorContext.node); } }, 'Program:exit'() { if (topLevelSuites.length > suiteLimit) { context.report({ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- ok in this case node: topLevelSuites[suiteLimit], messageId: 'tooManyTopLevelSuites', data: { limit: String(suiteLimit) } }); } } }); } }; //# sourceMappingURL=max-top-level-suites.js.map