eslint-plugin-mocha
Version:
Eslint rules for mocha.
129 lines • 4.93 kB
JavaScript
import { createMochaVisitors } from "../ast/mocha-visitors.js";
import { isCallExpression } from "../ast/node-types.js";
import { getStaticNumericConfigValue, isDisabledTimeoutValue, visitMochaContextConfigCalls } from "../mocha/config-call.js";
import { getRuleOption } from "../rule-options.js";
import { disallowDisabledModeOptionSchema, disallowModeOptionSchema, maximumNumericMochaConfigOptionSchema, rangeNumericMochaConfigOptionSchema } from "./numeric-mocha-config-option-schemas.js";
const optionSchema = {
oneOf: [
disallowModeOptionSchema,
disallowDisabledModeOptionSchema,
maximumNumericMochaConfigOptionSchema,
rangeNumericMochaConfigOptionSchema
]
};
const defaultOption = { mode: 'disallow' };
function validateOption(option) {
if (option.mode !== 'range') {
return;
}
if (option.min > option.max) {
throw new TypeError('`min` must be less than or equal to `max`.');
}
}
function reportUnexpectedTimeout(context, node, descriptor) {
context.report({
...descriptor,
node
});
}
function reportDisabledTimeout(context, node) {
reportUnexpectedTimeout(context, node, {
messageId: 'unexpectedDisabledTimeout'
});
}
function reportTimeoutAboveMax(context, node, maximumValue, timeoutValue) {
reportUnexpectedTimeout(context, node, {
messageId: 'unexpectedTimeoutAboveMax',
data: {
max: String(maximumValue),
value: String(timeoutValue)
}
});
}
function reportTimeoutOutsideRange(context, node, option, timeoutValue) {
reportUnexpectedTimeout(context, node, {
messageId: 'unexpectedTimeoutOutsideRange',
data: {
max: String(option.max),
min: String(option.min),
value: String(timeoutValue)
}
});
}
function readStaticTimeoutValue(context, node) {
return getStaticNumericConfigValue(node, context.sourceCode);
}
function checkDisabledTimeoutCall(context, node, timeoutValue) {
if (isDisabledTimeoutValue(timeoutValue)) {
reportDisabledTimeout(context, node);
}
}
function checkMaximumTimeoutCall(context, node, maximumValue, timeoutValue) {
if (timeoutValue > maximumValue) {
reportTimeoutAboveMax(context, node, maximumValue, timeoutValue);
}
}
function checkTimeoutRangeCall(context, node, option, timeoutValue) {
if (timeoutValue < option.min || timeoutValue > option.max) {
reportTimeoutOutsideRange(context, node, option, timeoutValue);
}
}
function checkConfiguredTimeoutCall(context, node, option, timeoutValue) {
if (option.mode === 'disallowDisabled') {
checkDisabledTimeoutCall(context, node, timeoutValue);
return;
}
if (option.mode === 'max') {
checkMaximumTimeoutCall(context, node, option.max, timeoutValue);
return;
}
checkTimeoutRangeCall(context, node, option, timeoutValue);
}
export const limitTimeoutRule = {
meta: {
type: 'suggestion',
docs: {
description: 'Enforce limits for Mocha timeouts',
recommended: false,
url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/main/documentation/rules/limit-timeout.md'
},
schema: [optionSchema],
defaultOptions: [defaultOption],
messages: {
unexpectedTimeout: 'Unexpected use of Mocha timeout configuration.',
unexpectedDisabledTimeout: 'Unexpected disabled Mocha timeout.',
unexpectedTimeoutAboveMax: 'Unexpected Mocha timeout value {{value}}. Maximum allowed is {{max}}.',
unexpectedTimeoutOutsideRange: 'Unexpected Mocha timeout value {{value}}. Expected a value between {{min}} and {{max}}.'
},
languages: ['js/js']
},
create(context) {
const option = getRuleOption(context);
validateOption(option);
function checkTimeoutCall(node) {
if (option.mode === 'disallow') {
reportUnexpectedTimeout(context, node, {
messageId: 'unexpectedTimeout'
});
return;
}
const timeoutValue = readStaticTimeoutValue(context, node);
if (timeoutValue === null) {
return;
}
checkConfiguredTimeoutCall(context, node, option, timeoutValue);
}
return createMochaVisitors(context, {
config(visitorContext) {
const { node } = visitorContext;
if (visitorContext.config === 'timeout' && isCallExpression(node)) {
checkTimeoutCall(node);
}
},
anyTestEntityCallback(visitorContext) {
visitMochaContextConfigCalls(context.sourceCode, visitorContext.node.body, 'timeout', checkTimeoutCall);
}
});
}
};
//# sourceMappingURL=limit-timeout.js.map