eslint-plugin-mocha
Version:
Eslint rules for mocha.
58 lines • 2.38 kB
JavaScript
import { createMochaVisitors } from "../ast/mocha-visitors.js";
import { expectNodeRange } from "../ast/node-location.js";
import { expectCallExpression, expectMemberExpression } from "../ast/node-types.js";
import { asRuleNode } from "../ast/rule-node.js";
function fixExclusiveTest(fixer, sourceCode, node) {
const range = expectNodeRange(node.callee);
return fixer.replaceTextRange(range, sourceCode.getText(node.callee.object));
}
function getExclusivePropertyNode(node) {
return node.callee.property;
}
function createExclusiveTestReportDescriptor(exclusivePropertyNode) {
return { node: exclusivePropertyNode, messageId: 'unexpectedExclusiveTest' };
}
export const noExclusiveTestsRule = {
meta: {
type: 'problem',
docs: {
description: 'Disallow exclusive tests',
recommended: true,
url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/main/documentation/rules/no-exclusive-tests.md'
},
hasSuggestions: true,
schema: [],
messages: {
unexpectedExclusiveTest: 'Unexpected exclusive mocha test.',
removeExclusiveModifier: 'Remove the exclusive modifier from this Mocha call.'
},
languages: ['js/js']
},
create(context) {
const { sourceCode } = context;
function checkPresenceOfExclusiveModifier(visitorContext) {
if (visitorContext.modifier !== 'exclusive') {
return;
}
const exclusiveCall = expectCallExpression(visitorContext.node);
const exclusiveNode = {
...exclusiveCall,
callee: expectMemberExpression(asRuleNode(exclusiveCall.callee))
};
const exclusivePropertyNode = getExclusivePropertyNode(exclusiveNode);
context.report({
...createExclusiveTestReportDescriptor(exclusivePropertyNode),
suggest: [{
messageId: 'removeExclusiveModifier',
fix(fixer) {
return fixExclusiveTest(fixer, sourceCode, exclusiveNode);
}
}]
});
}
return createMochaVisitors(context, {
suiteOrTestCase: checkPresenceOfExclusiveModifier
});
}
};
//# sourceMappingURL=no-exclusive-tests.js.map