-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathfake-rule.ts
99 lines (89 loc) · 3.07 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
/**
* @file Fake rule to be able to test createTestingLibraryRule and
* detectTestingLibraryUtils properly
*/
import { TSESTree } from '@typescript-eslint/experimental-utils';
import { createTestingLibraryRule } from '../lib/create-testing-library-rule';
export const RULE_NAME = 'fake-rule';
type Options = [];
type MessageIds =
| 'fakeError'
| 'getByError'
| 'queryByError'
| 'presenceAssertError'
| 'absenceAssertError';
export default createTestingLibraryRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Fake rule to test rule maker and detection helpers',
category: 'Possible Errors',
recommended: false,
},
messages: {
fakeError: 'fake error reported',
getByError: 'some error related to getBy reported',
queryByError: 'some error related to queryBy reported',
presenceAssertError: 'some error related to presence assert reported',
absenceAssertError: 'some error related to absence assert reported',
},
fixable: null,
schema: [],
},
defaultOptions: [],
create(context, _, helpers) {
const reportCallExpressionIdentifier = (node: TSESTree.Identifier) => {
// force "render" to be reported
if (node.name === 'render') {
return context.report({ node, messageId: 'fakeError' });
}
// force queries to be reported
if (helpers.isGetByQuery(node)) {
return context.report({ node, messageId: 'getByError' });
}
if (helpers.isQueryByQuery(node)) {
return context.report({ node, messageId: 'queryByError' });
}
};
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' });
}
};
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,
'CallExpression > MemberExpression'(node: TSESTree.MemberExpression) {
if (!helpers.isNodeComingFromTestingLibrary(node)) {
return;
}
context.report({ node, messageId: 'fakeError' });
},
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',
});
}
},
};
},
});