eslint-plugin-mocha
Version:
Eslint rules for mocha.
63 lines • 2.23 kB
JavaScript
import { hasUnhandledReturnPath } from "../done-callback-paths.js";
import { getRuleOption } from "../rule-options.js";
import { createTrackedCallbackVisitors } from "./callback-tracking.js";
const optionSchema = {
type: 'object',
properties: {
ignorePending: {
type: 'boolean'
}
},
additionalProperties: false
};
const defaultOption = { ignorePending: false };
function reportUnhandledDoneCallback(context, trackedFunction) {
const { callbackName, callbackNode } = trackedFunction;
const hasUnhandledPath = hasUnhandledReturnPath({
callbackBinding: trackedFunction.callbackBinding,
codePath: trackedFunction.codePath,
operationsBySegmentId: trackedFunction.operationsBySegmentId,
sourceCode: context.sourceCode
});
if (!hasUnhandledPath) {
return;
}
context.report({
node: callbackNode,
messageId: 'expectedCallback',
data: { name: callbackName }
});
}
function isDirectTrackedCallbackFunction(trackedFunction) {
return trackedFunction.callbackName !== undefined;
}
export const handleDoneCallbackRule = {
meta: {
type: 'problem',
docs: {
description: 'Enforces handling of callbacks for async tests in every branch',
recommended: true,
url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/main/documentation/rules/handle-done-callback.md'
},
schema: [optionSchema],
defaultOptions: [defaultOption],
messages: {
expectedCallback: 'Expected "{{name}}" callback to be handled.'
},
languages: ['js/js']
},
create(context) {
const { ignorePending } = getRuleOption(context);
return createTrackedCallbackVisitors(context, {
ignorePending,
includeInheritedCallbackBinding: true,
onTrackedFunctionEnd(trackedFunction) {
if (isDirectTrackedCallbackFunction(trackedFunction)) {
reportUnhandledDoneCallback(context, trackedFunction);
}
},
trackHandledCallbackArguments: true
});
}
};
//# sourceMappingURL=handle-done-callback.js.map