no-render-in-setup.js
3.5 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.findClosestBeforeHook = 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 = 'no-render-in-setup';
function findClosestBeforeHook(node, testingFrameworkSetupHooksToFilter) {
if (node === null) {
return null;
}
if ((0, node_utils_1.isCallExpression)(node) &&
utils_1.ASTUtils.isIdentifier(node.callee) &&
testingFrameworkSetupHooksToFilter.includes(node.callee.name)) {
return node.callee;
}
if (node.parent) {
return findClosestBeforeHook(node.parent, testingFrameworkSetupHooksToFilter);
}
return null;
}
exports.findClosestBeforeHook = findClosestBeforeHook;
exports.default = (0, create_testing_library_rule_1.createTestingLibraryRule)({
name: exports.RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Disallow the use of `render` in testing frameworks setup functions',
recommendedConfig: {
dom: false,
angular: 'error',
react: 'error',
vue: 'error',
marko: 'error',
},
},
messages: {
noRenderInSetup: 'Forbidden usage of `render` within testing framework `{{ name }}` setup',
},
schema: [
{
type: 'object',
properties: {
allowTestingFrameworkSetupHook: {
enum: utils_2.TESTING_FRAMEWORK_SETUP_HOOKS,
},
},
},
],
},
defaultOptions: [
{
allowTestingFrameworkSetupHook: '',
},
],
create(context, [{ allowTestingFrameworkSetupHook }], helpers) {
const renderWrapperNames = [];
function detectRenderWrapper(node) {
const innerFunction = (0, node_utils_1.getInnermostReturningFunction)(context, node);
if (innerFunction) {
renderWrapperNames.push((0, node_utils_1.getFunctionName)(innerFunction));
}
}
return {
CallExpression(node) {
const testingFrameworkSetupHooksToFilter = utils_2.TESTING_FRAMEWORK_SETUP_HOOKS.filter((hook) => hook !== allowTestingFrameworkSetupHook);
const callExpressionIdentifier = (0, node_utils_1.getDeepestIdentifierNode)(node);
if (!callExpressionIdentifier) {
return;
}
const isRenderIdentifier = helpers.isRenderUtil(callExpressionIdentifier);
if (isRenderIdentifier) {
detectRenderWrapper(callExpressionIdentifier);
}
if (!isRenderIdentifier &&
!renderWrapperNames.includes(callExpressionIdentifier.name)) {
return;
}
const beforeHook = findClosestBeforeHook(node, testingFrameworkSetupHooksToFilter);
if (!beforeHook) {
return;
}
context.report({
node: callExpressionIdentifier,
messageId: 'noRenderInSetup',
data: {
name: beforeHook.name,
},
});
},
};
},
});