2020-08-25 23:57:08 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
value: true
|
|
|
|
});
|
|
|
|
exports.default = void 0;
|
|
|
|
var _utils = require("./utils");
|
2022-11-10 10:43:16 +00:00
|
|
|
const isChainRestricted = (chain, restriction) => {
|
|
|
|
if (_utils.ModifierName.hasOwnProperty(restriction) || restriction.endsWith('.not')) {
|
|
|
|
return chain.startsWith(restriction);
|
|
|
|
}
|
|
|
|
return chain === restriction;
|
|
|
|
};
|
2020-08-25 23:57:08 +00:00
|
|
|
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 10:43:16 +00:00
|
|
|
restrictedChain: 'Use of `{{ restriction }}` is disallowed',
|
2020-08-25 23:57:08 +00:00
|
|
|
restrictedChainWithMessage: '{{ message }}'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
defaultOptions: [{}],
|
|
|
|
create(context, [restrictedChains]) {
|
|
|
|
return {
|
|
|
|
CallExpression(node) {
|
2022-11-10 10:43:16 +00:00
|
|
|
const jestFnCall = (0, _utils.parseJestFnCall)(node, context);
|
|
|
|
if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'expect') {
|
2020-08-25 23:57:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-11-10 10:43:16 +00:00
|
|
|
const chain = jestFnCall.members.map(nod => (0, _utils.getAccessorValue)(nod)).join('.');
|
|
|
|
for (const [restriction, message] of Object.entries(restrictedChains)) {
|
|
|
|
if (isChainRestricted(chain, restriction)) {
|
2020-08-25 23:57:08 +00:00
|
|
|
context.report({
|
|
|
|
messageId: message ? 'restrictedChainWithMessage' : 'restrictedChain',
|
|
|
|
data: {
|
|
|
|
message,
|
2022-11-10 10:43:16 +00:00
|
|
|
restriction
|
2020-08-25 23:57:08 +00:00
|
|
|
},
|
|
|
|
loc: {
|
2022-11-10 10:43:16 +00:00
|
|
|
start: jestFnCall.members[0].loc.start,
|
|
|
|
end: jestFnCall.members[jestFnCall.members.length - 1].loc.end
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
});
|
2022-11-10 10:43:16 +00:00
|
|
|
break;
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
exports.default = _default;
|