UNPKG

eslint-plugin-mocha

Version:

Eslint rules for mocha.

67 lines 2.32 kB
import { createMochaVisitors } from "../ast/mocha-visitors.js"; import { expectNodeRange } from "../ast/node-location.js"; import { isFunction } from "../ast/node-types.js"; import { visitChildNodes } from "../ast/visit-child-nodes.js"; function containsDirectAwait(sourceCode, node) { if (node.type === 'AwaitExpression') { return true; } if (isFunction(node)) { return false; } let hasDirectAwait = false; visitChildNodes(sourceCode, node, function (childNode) { if (!hasDirectAwait && containsDirectAwait(sourceCode, childNode)) { hasDirectAwait = true; } }); return hasDirectAwait; } function isAsyncFunction(node) { return (node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') && node.async === true; } function fixAsyncFunction(sourceCode, fixer, fn) { if (!containsDirectAwait(sourceCode, fn.body)) { const asyncPrefixLength = 'async '.length; const [start] = expectNodeRange(fn); return fixer.removeRange([start, start + asyncPrefixLength]); } return null; } export const noAsyncSuiteRule = { meta: { type: 'problem', docs: { description: 'Disallow async functions passed to a suite', recommended: true, url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/main/documentation/rules/no-async-suite.md' }, fixable: 'code', schema: [], messages: { unexpectedAsyncSuite: 'Unexpected async function in {{name}}' }, languages: ['js/js'] }, create(context) { const { sourceCode } = context; return createMochaVisitors(context, { suiteCallback(visitorContext) { const { node } = visitorContext; if (isAsyncFunction(node)) { context.report({ node, messageId: 'unexpectedAsyncSuite', data: { name: visitorContext.name }, fix(fixer) { return fixAsyncFunction(sourceCode, fixer, node); } }); } } }); } }; //# sourceMappingURL=no-async-suite.js.map