no-unnecessary-act.js
5.57 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
"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");
exports.RULE_NAME = 'no-unnecessary-act';
exports.default = (0, create_testing_library_rule_1.createTestingLibraryRule)({
name: exports.RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Disallow wrapping Testing Library utils or empty callbacks in `act`',
recommendedConfig: {
dom: false,
angular: false,
react: 'error',
vue: false,
marko: 'error',
},
},
messages: {
noUnnecessaryActTestingLibraryUtil: 'Avoid wrapping Testing Library util calls in `act`',
noUnnecessaryActEmptyFunction: 'Avoid wrapping empty function in `act`',
},
schema: [
{
type: 'object',
properties: {
isStrict: {
type: 'boolean',
},
},
},
],
},
defaultOptions: [
{
isStrict: true,
},
],
create(context, [{ isStrict = true }], helpers) {
function getStatementIdentifier(statement) {
const callExpression = (0, node_utils_1.getStatementCallExpression)(statement);
if (!callExpression &&
!(0, node_utils_1.isExpressionStatement)(statement) &&
!(0, node_utils_1.isReturnStatement)(statement)) {
return null;
}
if (callExpression) {
return (0, node_utils_1.getDeepestIdentifierNode)(callExpression);
}
if ((0, node_utils_1.isExpressionStatement)(statement) &&
utils_1.ASTUtils.isAwaitExpression(statement.expression)) {
return (0, node_utils_1.getPropertyIdentifierNode)(statement.expression.argument);
}
if ((0, node_utils_1.isReturnStatement)(statement) && statement.argument) {
return (0, node_utils_1.getPropertyIdentifierNode)(statement.argument);
}
return null;
}
function hasSomeNonTestingLibraryCall(statements) {
return statements.some((statement) => {
const identifier = getStatementIdentifier(statement);
if (!identifier) {
return false;
}
return !helpers.isTestingLibraryUtil(identifier);
});
}
function hasTestingLibraryCall(statements) {
return statements.some((statement) => {
const identifier = getStatementIdentifier(statement);
if (!identifier) {
return false;
}
return helpers.isTestingLibraryUtil(identifier);
});
}
function checkNoUnnecessaryActFromBlockStatement(blockStatementNode) {
const functionNode = blockStatementNode.parent;
const callExpressionNode = functionNode === null || functionNode === void 0 ? void 0 : functionNode.parent;
if (!callExpressionNode || !functionNode) {
return;
}
const identifierNode = (0, node_utils_1.getDeepestIdentifierNode)(callExpressionNode);
if (!identifierNode) {
return;
}
if (!helpers.isActUtil(identifierNode)) {
return;
}
if ((0, node_utils_1.isEmptyFunction)(functionNode)) {
context.report({
node: identifierNode,
messageId: 'noUnnecessaryActEmptyFunction',
});
return;
}
const shouldBeReported = isStrict
? hasTestingLibraryCall(blockStatementNode.body)
: !hasSomeNonTestingLibraryCall(blockStatementNode.body);
if (shouldBeReported) {
context.report({
node: identifierNode,
messageId: 'noUnnecessaryActTestingLibraryUtil',
});
}
}
function checkNoUnnecessaryActFromImplicitReturn(node) {
var _a;
const nodeIdentifier = (0, node_utils_1.getDeepestIdentifierNode)(node);
if (!nodeIdentifier) {
return;
}
const parentCallExpression = (_a = node.parent) === null || _a === void 0 ? void 0 : _a.parent;
if (!parentCallExpression) {
return;
}
const identifierNode = (0, node_utils_1.getDeepestIdentifierNode)(parentCallExpression);
if (!identifierNode) {
return;
}
if (!helpers.isActUtil(identifierNode)) {
return;
}
if (!helpers.isTestingLibraryUtil(nodeIdentifier)) {
return;
}
context.report({
node: identifierNode,
messageId: 'noUnnecessaryActTestingLibraryUtil',
});
}
return {
'CallExpression > ArrowFunctionExpression > BlockStatement': checkNoUnnecessaryActFromBlockStatement,
'CallExpression > FunctionExpression > BlockStatement': checkNoUnnecessaryActFromBlockStatement,
'CallExpression > ArrowFunctionExpression > CallExpression': checkNoUnnecessaryActFromImplicitReturn,
};
},
});