no-render-in-setup.js
5.27 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
"use strict";
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findClosestBeforeHook = exports.RULE_NAME = void 0;
var experimental_utils_1 = require("@typescript-eslint/experimental-utils");
var utils_1 = require("../utils");
var node_utils_1 = require("../node-utils");
exports.RULE_NAME = 'no-render-in-setup';
function findClosestBeforeHook(node, testingFrameworkSetupHooksToFilter) {
if (node === null)
return null;
if (node_utils_1.isCallExpression(node) &&
node_utils_1.isIdentifier(node.callee) &&
testingFrameworkSetupHooksToFilter.includes(node.callee.name)) {
return node.callee;
}
return findClosestBeforeHook(node.parent, testingFrameworkSetupHooksToFilter);
}
exports.findClosestBeforeHook = findClosestBeforeHook;
exports.default = experimental_utils_1.ESLintUtils.RuleCreator(utils_1.getDocsUrl)({
name: exports.RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Disallow the use of `render` in setup functions',
category: 'Best Practices',
recommended: false,
},
messages: {
noRenderInSetup: 'Move `render` out of `{{name}}` and into individual tests.',
},
fixable: null,
schema: [
{
type: 'object',
properties: {
renderFunctions: {
type: 'array',
},
allowTestingFrameworkSetupHook: {
enum: utils_1.TESTING_FRAMEWORK_SETUP_HOOKS,
},
},
anyOf: [
{
required: ['renderFunctions'],
},
{
required: ['allowTestingFrameworkSetupHook'],
},
],
},
],
},
defaultOptions: [
{
renderFunctions: [],
allowTestingFrameworkSetupHook: '',
},
],
create: function (context, _a) {
var _b;
var _c = _a[0], renderFunctions = _c.renderFunctions, allowTestingFrameworkSetupHook = _c.allowTestingFrameworkSetupHook;
var renderImportedFromTestingLib = false;
var wildcardImportName = null;
return _b = {
'ImportDeclaration[source.value=/testing-library/] ImportNamespaceSpecifier': function (node) {
wildcardImportName = node.local && node.local.name;
},
'ImportDeclaration[source.value=/testing-library/]': function (node) {
renderImportedFromTestingLib = node.specifiers.some(function (specifier) {
return (node_utils_1.isImportSpecifier(specifier) && specifier.local.name === 'render');
});
}
},
_b["VariableDeclarator > CallExpression > Identifier[name=\"require\"]"] = function (node) {
var callExpressionArgs = node.parent.arguments;
var testingLibImport = callExpressionArgs.find(function (args) {
return node_utils_1.isLiteral(args) &&
typeof args.value === 'string' &&
RegExp(/testing-library/, 'g').test(args.value);
});
if (!testingLibImport) {
return;
}
var declaratorNode = node.parent
.parent;
renderImportedFromTestingLib =
node_utils_1.isObjectPattern(declaratorNode.id) &&
declaratorNode.id.properties.some(function (property) {
return node_utils_1.isProperty(property) &&
node_utils_1.isIdentifier(property.key) &&
property.key.name === 'render';
});
},
_b.CallExpression = function (node) {
var testingFrameworkSetupHooksToFilter = utils_1.TESTING_FRAMEWORK_SETUP_HOOKS;
if (allowTestingFrameworkSetupHook.length !== 0) {
testingFrameworkSetupHooksToFilter = utils_1.TESTING_FRAMEWORK_SETUP_HOOKS.filter(function (hook) { return hook !== allowTestingFrameworkSetupHook; });
}
var beforeHook = findClosestBeforeHook(node, testingFrameworkSetupHooksToFilter);
var disallowedRenderFns = renderImportedFromTestingLib || wildcardImportName
? __spreadArrays(['render'], renderFunctions) : renderFunctions;
if (node_utils_1.isRenderFunction(node, disallowedRenderFns) && beforeHook) {
context.report({
node: node,
messageId: 'noRenderInSetup',
data: {
name: beforeHook.name,
},
});
}
},
_b;
},
});