-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathno-render-in-setup.ts
96 lines (91 loc) · 2.47 KB
/
no-render-in-setup.ts
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
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils';
import { getDocsUrl, TESTING_FRAMEWORK_SETUP_HOOKS } from '../utils';
import {
isIdentifier,
isCallExpression,
isRenderFunction,
} from '../node-utils';
export const RULE_NAME = 'no-render-in-setup';
export type MessageIds = 'noRenderInSetup';
export function findClosestBeforeHook(
node: TSESTree.Node,
testingFrameworkSetupHooksToFilter: string[]
): TSESTree.Identifier | null {
if (node === null) return null;
if (
isCallExpression(node) &&
isIdentifier(node.callee) &&
testingFrameworkSetupHooksToFilter.includes(node.callee.name)
) {
return node.callee;
}
return findClosestBeforeHook(node.parent, testingFrameworkSetupHooksToFilter);
}
export default ESLintUtils.RuleCreator(getDocsUrl)({
name: 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: TESTING_FRAMEWORK_SETUP_HOOKS,
},
},
anyOf: [
{
required: ['renderFunctions'],
},
{
required: ['allowTestingFrameworkSetupHook'],
},
],
},
],
},
defaultOptions: [
{
renderFunctions: [],
allowTestingFrameworkSetupHook: '',
},
],
create(context, [{ renderFunctions, allowTestingFrameworkSetupHook }]) {
return {
CallExpression(node) {
let testingFrameworkSetupHooksToFilter = TESTING_FRAMEWORK_SETUP_HOOKS;
if (allowTestingFrameworkSetupHook.length !== 0) {
testingFrameworkSetupHooksToFilter = TESTING_FRAMEWORK_SETUP_HOOKS.filter(
hook => hook !== allowTestingFrameworkSetupHook
);
}
const beforeHook = findClosestBeforeHook(
node,
testingFrameworkSetupHooksToFilter
);
if (isRenderFunction(node, renderFunctions) && beforeHook) {
context.report({
node,
messageId: 'noRenderInSetup',
data: {
name: beforeHook.name,
},
});
}
},
};
},
});