| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = void 0;
|
|---|
| 7 |
|
|---|
| 8 | var _experimentalUtils = require("@typescript-eslint/experimental-utils");
|
|---|
| 9 |
|
|---|
| 10 | var _utils = require("./utils");
|
|---|
| 11 |
|
|---|
| 12 | const isCatchCall = node => node.callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && (0, _utils.isSupportedAccessor)(node.callee.property, 'catch');
|
|---|
| 13 |
|
|---|
| 14 | var _default = (0, _utils.createRule)({
|
|---|
| 15 | name: __filename,
|
|---|
| 16 | meta: {
|
|---|
| 17 | docs: {
|
|---|
| 18 | description: 'Prevent calling `expect` conditionally',
|
|---|
| 19 | category: 'Best Practices',
|
|---|
| 20 | recommended: 'error'
|
|---|
| 21 | },
|
|---|
| 22 | messages: {
|
|---|
| 23 | conditionalExpect: 'Avoid calling `expect` conditionally`'
|
|---|
| 24 | },
|
|---|
| 25 | type: 'problem',
|
|---|
| 26 | schema: []
|
|---|
| 27 | },
|
|---|
| 28 | defaultOptions: [],
|
|---|
| 29 |
|
|---|
| 30 | create(context) {
|
|---|
| 31 | let conditionalDepth = 0;
|
|---|
| 32 | let inTestCase = false;
|
|---|
| 33 | let inPromiseCatch = false;
|
|---|
| 34 |
|
|---|
| 35 | const increaseConditionalDepth = () => inTestCase && conditionalDepth++;
|
|---|
| 36 |
|
|---|
| 37 | const decreaseConditionalDepth = () => inTestCase && conditionalDepth--;
|
|---|
| 38 |
|
|---|
| 39 | return {
|
|---|
| 40 | FunctionDeclaration(node) {
|
|---|
| 41 | const declaredVariables = context.getDeclaredVariables(node);
|
|---|
| 42 | const testCallExpressions = (0, _utils.getTestCallExpressionsFromDeclaredVariables)(declaredVariables);
|
|---|
| 43 |
|
|---|
| 44 | if (testCallExpressions.length > 0) {
|
|---|
| 45 | inTestCase = true;
|
|---|
| 46 | }
|
|---|
| 47 | },
|
|---|
| 48 |
|
|---|
| 49 | CallExpression(node) {
|
|---|
| 50 | if ((0, _utils.isTestCaseCall)(node)) {
|
|---|
| 51 | inTestCase = true;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | if (isCatchCall(node)) {
|
|---|
| 55 | inPromiseCatch = true;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | if (inTestCase && (0, _utils.isExpectCall)(node) && conditionalDepth > 0) {
|
|---|
| 59 | context.report({
|
|---|
| 60 | messageId: 'conditionalExpect',
|
|---|
| 61 | node
|
|---|
| 62 | });
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | if (inPromiseCatch && (0, _utils.isExpectCall)(node)) {
|
|---|
| 66 | context.report({
|
|---|
| 67 | messageId: 'conditionalExpect',
|
|---|
| 68 | node
|
|---|
| 69 | });
|
|---|
| 70 | }
|
|---|
| 71 | },
|
|---|
| 72 |
|
|---|
| 73 | 'CallExpression:exit'(node) {
|
|---|
| 74 | if ((0, _utils.isTestCaseCall)(node)) {
|
|---|
| 75 | inTestCase = false;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | if (isCatchCall(node)) {
|
|---|
| 79 | inPromiseCatch = false;
|
|---|
| 80 | }
|
|---|
| 81 | },
|
|---|
| 82 |
|
|---|
| 83 | CatchClause: increaseConditionalDepth,
|
|---|
| 84 | 'CatchClause:exit': decreaseConditionalDepth,
|
|---|
| 85 | IfStatement: increaseConditionalDepth,
|
|---|
| 86 | 'IfStatement:exit': decreaseConditionalDepth,
|
|---|
| 87 | SwitchStatement: increaseConditionalDepth,
|
|---|
| 88 | 'SwitchStatement:exit': decreaseConditionalDepth,
|
|---|
| 89 | ConditionalExpression: increaseConditionalDepth,
|
|---|
| 90 | 'ConditionalExpression:exit': decreaseConditionalDepth,
|
|---|
| 91 | LogicalExpression: increaseConditionalDepth,
|
|---|
| 92 | 'LogicalExpression:exit': decreaseConditionalDepth
|
|---|
| 93 | };
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | });
|
|---|
| 97 |
|
|---|
| 98 | exports.default = _default; |
|---|