forked from testing-library/eslint-plugin-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfake-rule.ts
134 lines (118 loc) · 3.69 KB
/
fake-rule.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
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
/**
* @file Fake rule to be able to test createTestingLibraryRule and
* detectTestingLibraryUtils properly
*/
import { TSESTree } from '@typescript-eslint/utils';
import { createTestingLibraryRule } from '../lib/create-testing-library-rule';
export const RULE_NAME = 'fake-rule';
type Options = [];
type MessageIds =
| 'absenceAssertError'
| 'asyncUtilError'
| 'customQueryError'
| 'fakeError'
| 'findByError'
| 'getByError'
| 'presenceAssertError'
| 'queryByError'
| 'renderError'
| 'userEventError';
export default createTestingLibraryRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Fake rule to test rule maker and detection helpers',
recommendedConfig: {
dom: false,
angular: false,
react: false,
vue: false,
svelte: false,
marko: false,
},
},
messages: {
fakeError: 'fake error reported',
renderError: 'some error related to render util reported',
asyncUtilError:
'some error related to {{ utilName }} async util reported',
getByError: 'some error related to getBy reported',
queryByError: 'some error related to queryBy reported',
findByError: 'some error related to findBy reported',
customQueryError: 'some error related to a customQuery reported',
userEventError: 'some error related to userEvent reported',
presenceAssertError: 'some error related to presence assert reported',
absenceAssertError: 'some error related to absence assert reported',
},
schema: [],
},
defaultOptions: [],
create(context, _, helpers) {
const reportCallExpressionIdentifier = (node: TSESTree.Identifier) => {
// force "render" to be reported
if (helpers.isRenderUtil(node)) {
return context.report({ node, messageId: 'renderError' });
}
// force async utils to be reported
if (helpers.isAsyncUtil(node)) {
return context.report({
node,
messageId: 'asyncUtilError',
data: { utilName: node.name },
});
}
if (helpers.isUserEventMethod(node)) {
return context.report({ node, messageId: 'userEventError' });
}
// force queries to be reported
if (helpers.isCustomQuery(node)) {
return context.report({ node, messageId: 'customQueryError' });
}
if (helpers.isGetQueryVariant(node)) {
return context.report({ node, messageId: 'getByError' });
}
if (helpers.isQueryQueryVariant(node)) {
return context.report({ node, messageId: 'queryByError' });
}
if (helpers.isFindQueryVariant(node)) {
return context.report({ node, messageId: 'findByError' });
}
return undefined;
};
const reportMemberExpression = (node: TSESTree.MemberExpression) => {
if (helpers.isPresenceAssert(node)) {
return context.report({ node, messageId: 'presenceAssertError' });
}
if (helpers.isAbsenceAssert(node)) {
return context.report({ node, messageId: 'absenceAssertError' });
}
return undefined;
};
const reportImportDeclaration = (node: TSESTree.ImportDeclaration) => {
// This is just to check that defining an `ImportDeclaration` doesn't
// override `ImportDeclaration` from `detectTestingLibraryUtils`
if (node.source.value === 'report-me') {
context.report({ node, messageId: 'fakeError' });
}
};
return {
'CallExpression Identifier': reportCallExpressionIdentifier,
MemberExpression: reportMemberExpression,
ImportDeclaration: reportImportDeclaration,
'Program:exit'() {
const importNode = helpers.getCustomModuleImportNode();
const importName = helpers.getCustomModuleImportName();
if (!importNode) {
return;
}
if (importName === 'custom-module-forced-report') {
context.report({
node: importNode,
messageId: 'fakeError',
});
}
},
};
},
});