prefer-explicit-assert.js
6.64 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RULE_NAME = void 0;
const utils_1 = require("@typescript-eslint/utils");
const create_testing_library_rule_1 = require("../create-testing-library-rule");
const node_utils_1 = require("../node-utils");
const utils_2 = require("../utils");
exports.RULE_NAME = 'prefer-explicit-assert';
const isAtTopLevel = (node) => {
var _a, _b, _c;
return (!!((_a = node.parent) === null || _a === void 0 ? void 0 : _a.parent) &&
node.parent.parent.type === 'ExpressionStatement') ||
(((_c = (_b = node.parent) === null || _b === void 0 ? void 0 : _b.parent) === null || _c === void 0 ? void 0 : _c.type) === 'AwaitExpression' &&
!!node.parent.parent.parent &&
node.parent.parent.parent.type === 'ExpressionStatement');
};
const isVariableDeclaration = (node) => {
if ((0, node_utils_1.isCallExpression)(node.parent) &&
utils_1.ASTUtils.isAwaitExpression(node.parent.parent) &&
utils_1.ASTUtils.isVariableDeclarator(node.parent.parent.parent)) {
return true;
}
if ((0, node_utils_1.isCallExpression)(node.parent) &&
utils_1.ASTUtils.isVariableDeclarator(node.parent.parent)) {
return true;
}
if ((0, node_utils_1.isMemberExpression)(node.parent) &&
(0, node_utils_1.isCallExpression)(node.parent.parent) &&
utils_1.ASTUtils.isAwaitExpression(node.parent.parent.parent) &&
utils_1.ASTUtils.isVariableDeclarator(node.parent.parent.parent.parent)) {
return true;
}
if ((0, node_utils_1.isMemberExpression)(node.parent) &&
(0, node_utils_1.isCallExpression)(node.parent.parent) &&
utils_1.ASTUtils.isVariableDeclarator(node.parent.parent.parent)) {
return true;
}
return false;
};
exports.default = (0, create_testing_library_rule_1.createTestingLibraryRule)({
name: exports.RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description: 'Suggest using explicit assertions rather than standalone queries',
recommendedConfig: {
dom: false,
angular: false,
react: false,
vue: false,
marko: false,
},
},
messages: {
preferExplicitAssert: 'Wrap stand-alone `{{queryType}}` query with `expect` function for better explicit assertion',
preferExplicitAssertAssertion: '`getBy*` queries must be asserted with `{{assertion}}`',
},
schema: [
{
type: 'object',
additionalProperties: false,
properties: {
assertion: {
type: 'string',
enum: utils_2.PRESENCE_MATCHERS,
},
includeFindQueries: { type: 'boolean' },
},
},
],
},
defaultOptions: [{ includeFindQueries: true }],
create(context, [options], helpers) {
const { assertion, includeFindQueries } = options;
const getQueryCalls = [];
const findQueryCalls = [];
return {
'CallExpression Identifier'(node) {
if (helpers.isGetQueryVariant(node)) {
getQueryCalls.push(node);
}
if (helpers.isFindQueryVariant(node)) {
findQueryCalls.push(node);
}
},
'Program:exit'() {
if (includeFindQueries) {
findQueryCalls.forEach((queryCall) => {
const memberExpression = (0, node_utils_1.isMemberExpression)(queryCall.parent)
? queryCall.parent
: queryCall;
if (isVariableDeclaration(queryCall) ||
!isAtTopLevel(memberExpression)) {
return;
}
context.report({
node: queryCall,
messageId: 'preferExplicitAssert',
data: {
queryType: 'findBy*',
},
});
});
}
getQueryCalls.forEach((queryCall) => {
const node = (0, node_utils_1.isMemberExpression)(queryCall.parent)
? queryCall.parent
: queryCall;
if (isAtTopLevel(node)) {
context.report({
node: queryCall,
messageId: 'preferExplicitAssert',
data: {
queryType: 'getBy*',
},
});
}
if (assertion) {
const expectCallNode = (0, node_utils_1.findClosestCallNode)(node, 'expect');
if (!expectCallNode)
return;
const expectStatement = expectCallNode.parent;
if (!(0, node_utils_1.isMemberExpression)(expectStatement)) {
return;
}
const property = expectStatement.property;
if (!utils_1.ASTUtils.isIdentifier(property)) {
return;
}
let matcher = property.name;
let isNegatedMatcher = false;
if (matcher === 'not' &&
(0, node_utils_1.isMemberExpression)(expectStatement.parent) &&
utils_1.ASTUtils.isIdentifier(expectStatement.parent.property)) {
isNegatedMatcher = true;
matcher = expectStatement.parent.property.name;
}
const shouldEnforceAssertion = (!isNegatedMatcher && utils_2.PRESENCE_MATCHERS.includes(matcher)) ||
(isNegatedMatcher && utils_2.ABSENCE_MATCHERS.includes(matcher));
if (shouldEnforceAssertion && matcher !== assertion) {
context.report({
node: property,
messageId: 'preferExplicitAssertAssertion',
data: {
assertion,
},
});
}
}
});
},
};
},
});