webhook/node_modules/eslint-plugin-jest/lib/rules/no-restricted-matchers.js

63 lines
1.8 KiB
JavaScript
Raw Normal View History

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _utils = require("./utils");
2022-11-10 11:43:16 +01:00
const isChainRestricted = (chain, restriction) => {
if (_utils.ModifierName.hasOwnProperty(restriction) || restriction.endsWith('.not')) {
return chain.startsWith(restriction);
}
return chain === restriction;
};
var _default = (0, _utils.createRule)({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Disallow specific matchers & modifiers',
recommended: false
},
type: 'suggestion',
schema: [{
type: 'object',
additionalProperties: {
type: ['string', 'null']
}
}],
messages: {
2022-11-10 11:43:16 +01:00
restrictedChain: 'Use of `{{ restriction }}` is disallowed',
restrictedChainWithMessage: '{{ message }}'
}
},
defaultOptions: [{}],
create(context, [restrictedChains]) {
return {
CallExpression(node) {
2022-11-10 11:43:16 +01:00
const jestFnCall = (0, _utils.parseJestFnCall)(node, context);
if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'expect') {
return;
}
2022-11-10 11:43:16 +01:00
const chain = jestFnCall.members.map(nod => (0, _utils.getAccessorValue)(nod)).join('.');
for (const [restriction, message] of Object.entries(restrictedChains)) {
if (isChainRestricted(chain, restriction)) {
context.report({
messageId: message ? 'restrictedChainWithMessage' : 'restrictedChain',
data: {
message,
2022-11-10 11:43:16 +01:00
restriction
},
loc: {
2022-11-10 11:43:16 +01:00
start: jestFnCall.members[0].loc.start,
end: jestFnCall.members[jestFnCall.members.length - 1].loc.end
}
});
2022-11-10 11:43:16 +01:00
break;
}
}
}
};
}
});
exports.default = _default;