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