2020-08-25 23:57:08 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
value: true
|
|
|
|
});
|
|
|
|
exports.default = void 0;
|
2022-11-10 10:43:16 +00:00
|
|
|
var _utils = require("@typescript-eslint/utils");
|
|
|
|
var _utils2 = require("./utils");
|
|
|
|
const isCatchCall = node => node.callee.type === _utils.AST_NODE_TYPES.MemberExpression && (0, _utils2.isSupportedAccessor)(node.callee.property, 'catch');
|
|
|
|
var _default = (0, _utils2.createRule)({
|
2020-08-25 23:57:08 +00:00
|
|
|
name: __filename,
|
|
|
|
meta: {
|
|
|
|
docs: {
|
2022-11-10 10:43:16 +00:00
|
|
|
description: 'Disallow calling `expect` conditionally',
|
2020-08-25 23:57:08 +00:00
|
|
|
category: 'Best Practices',
|
2021-02-26 03:58:33 +00:00
|
|
|
recommended: 'error'
|
2020-08-25 23:57:08 +00:00
|
|
|
},
|
|
|
|
messages: {
|
|
|
|
conditionalExpect: 'Avoid calling `expect` conditionally`'
|
|
|
|
},
|
|
|
|
type: 'problem',
|
|
|
|
schema: []
|
|
|
|
},
|
|
|
|
defaultOptions: [],
|
|
|
|
create(context) {
|
|
|
|
let conditionalDepth = 0;
|
|
|
|
let inTestCase = false;
|
2022-11-10 10:43:16 +00:00
|
|
|
let inPromiseCatch = false;
|
2020-08-25 23:57:08 +00:00
|
|
|
const increaseConditionalDepth = () => inTestCase && conditionalDepth++;
|
|
|
|
const decreaseConditionalDepth = () => inTestCase && conditionalDepth--;
|
|
|
|
return {
|
|
|
|
FunctionDeclaration(node) {
|
|
|
|
const declaredVariables = context.getDeclaredVariables(node);
|
2022-11-10 10:43:16 +00:00
|
|
|
const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables, context);
|
2020-08-25 23:57:08 +00:00
|
|
|
if (testCallExpressions.length > 0) {
|
|
|
|
inTestCase = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
CallExpression(node) {
|
2022-11-10 10:43:16 +00:00
|
|
|
const {
|
|
|
|
type: jestFnCallType
|
|
|
|
} = (0, _utils2.parseJestFnCall)(node, context) ?? {};
|
|
|
|
if (jestFnCallType === 'test') {
|
2020-08-25 23:57:08 +00:00
|
|
|
inTestCase = true;
|
|
|
|
}
|
2022-11-10 10:43:16 +00:00
|
|
|
if (isCatchCall(node)) {
|
|
|
|
inPromiseCatch = true;
|
|
|
|
}
|
|
|
|
if (inTestCase && jestFnCallType === 'expect' && conditionalDepth > 0) {
|
|
|
|
context.report({
|
|
|
|
messageId: 'conditionalExpect',
|
|
|
|
node
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (inPromiseCatch && jestFnCallType === 'expect') {
|
2020-08-25 23:57:08 +00:00
|
|
|
context.report({
|
|
|
|
messageId: 'conditionalExpect',
|
|
|
|
node
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'CallExpression:exit'(node) {
|
2022-11-10 10:43:16 +00:00
|
|
|
if ((0, _utils2.isTypeOfJestFnCall)(node, context, ['test'])) {
|
2020-08-25 23:57:08 +00:00
|
|
|
inTestCase = false;
|
|
|
|
}
|
2022-11-10 10:43:16 +00:00
|
|
|
if (isCatchCall(node)) {
|
|
|
|
inPromiseCatch = false;
|
|
|
|
}
|
2020-08-25 23:57:08 +00:00
|
|
|
},
|
|
|
|
CatchClause: increaseConditionalDepth,
|
|
|
|
'CatchClause:exit': decreaseConditionalDepth,
|
|
|
|
IfStatement: increaseConditionalDepth,
|
|
|
|
'IfStatement:exit': decreaseConditionalDepth,
|
|
|
|
SwitchStatement: increaseConditionalDepth,
|
|
|
|
'SwitchStatement:exit': decreaseConditionalDepth,
|
|
|
|
ConditionalExpression: increaseConditionalDepth,
|
|
|
|
'ConditionalExpression:exit': decreaseConditionalDepth,
|
|
|
|
LogicalExpression: increaseConditionalDepth,
|
|
|
|
'LogicalExpression:exit': decreaseConditionalDepth
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
exports.default = _default;
|