UNPKG

eslint-plugin-mocha

Version:

Eslint rules for mocha.

77 lines 3.02 kB
import { getStaticValue } from '@eslint-community/eslint-utils'; import { isCallExpression, isFunction, isIdentifier, isLiteral, isMemberExpression } from "../ast/node-types.js"; import { visitChildNodes } from "../ast/visit-child-nodes.js"; import { configCallNames } from "./descriptors.js"; const suiteConfig = new Set(configCallNames); const maximumTimeout = Number.parseInt('2147483647', 10); function isMochaConfigCallName(value) { return suiteConfig.has(value); } function isFiniteNumber(value) { return Number.isFinite(value); } function getNamedConfigPropertyName(property) { const name = isMemberExpression(property) && !property.computed && isIdentifier(property.property) ? property.property.name : null; return name !== null && isMochaConfigCallName(name) ? name : null; } function getComputedConfigPropertyName(property) { const name = isMemberExpression(property) && property.computed && isLiteral(property.property) ? String(property.property.value) : null; return name !== null && isMochaConfigCallName(name) ? name : null; } function getPropertyName(property) { return getNamedConfigPropertyName(property) ?? getComputedConfigPropertyName(property); } function getMochaContextConfigExpression(callee) { if (!isMemberExpression(callee) || callee.object.type !== 'ThisExpression') { return null; } return getPropertyName(callee) === null ? null : callee; } function getFirstArgument(node) { const [firstArgument] = node.arguments; return firstArgument?.type === 'SpreadElement' ? undefined : firstArgument; } function isMochaContextConfigCall(node, configName) { if (!isCallExpression(node)) { return false; } const propertyName = getPropertyName(node.callee); const matchingContextExpression = getMochaContextConfigExpression(node.callee); return matchingContextExpression !== null && (configName === undefined || propertyName === configName); } export function isSuiteConfigCall(node) { return isMochaContextConfigCall(node); } export function getStaticNumericConfigValue(node, sourceCode) { const firstArgument = getFirstArgument(node); if (firstArgument === undefined) { return null; } const staticValue = getStaticValue(firstArgument, sourceCode.getScope(firstArgument)); const value = staticValue?.value; return isFiniteNumber(value) ? value : null; } export function isDisabledTimeoutValue(value) { return value <= 0 || value >= maximumTimeout; } export function visitMochaContextConfigCalls(sourceCode, node, configName, visitor) { if (isMochaContextConfigCall(node, configName)) { visitor(node); } if (isFunction(node) && node.type !== 'ArrowFunctionExpression') { return; } visitChildNodes(sourceCode, node, function (childNode) { visitMochaContextConfigCalls(sourceCode, childNode, configName, visitor); }); } //# sourceMappingURL=config-call.js.map