UNPKG

eslint-plugin-mocha

Version:

Eslint rules for mocha.

74 lines 2.54 kB
import { getStringIfConstant } from '@eslint-community/eslint-utils'; import { createMochaVisitors } from "../ast/mocha-visitors.js"; import { isCallExpression } from "../ast/node-types.js"; import { getRuleOption } from "../rule-options.js"; const ERROR_MESSAGE = 'Unexpected empty test description.'; const optionSchema = { type: 'object', properties: { message: { type: 'string' } }, additionalProperties: false }; const defaultOption = {}; function isValidDescriptionArgumentNode(node) { if (node === undefined) { return false; } return [ 'Literal', 'TemplateLiteral', 'TaggedTemplateExpression', 'Identifier', 'MemberExpression', 'CallExpression', 'LogicalExpression', 'BinaryExpression', 'ConditionalExpression', 'UnaryExpression', 'SpreadElement', 'AwaitExpression', 'YieldExpression', 'NewExpression' ] .includes(node.type); } export const noEmptyTitleRule = { meta: { type: 'suggestion', docs: { description: 'Disallow empty suite and test descriptions', recommended: true, url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/main/documentation/rules/no-empty-title.md' }, schema: [optionSchema], defaultOptions: [defaultOption], messages: { emptyTitle: ERROR_MESSAGE }, languages: ['js/js'] }, create(context) { const { message: customMessage } = getRuleOption(context); function isNonEmptyDescription(mochaCallExpression) { const description = mochaCallExpression.arguments[0]; if (!isValidDescriptionArgumentNode(description)) { return false; } const text = getStringIfConstant(description, context.sourceCode.getScope(mochaCallExpression)); return text === null || text.trim().length > 0; } return createMochaVisitors(context, { suiteOrTestCase(visitorContext) { if (isCallExpression(visitorContext.node) && !isNonEmptyDescription(visitorContext.node)) { context.report(customMessage === undefined ? { node: visitorContext.node, messageId: 'emptyTitle' } : { node: visitorContext.node, message: customMessage }); } } }); } }; //# sourceMappingURL=no-empty-title.js.map