-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathno-promise-in-fire-event.ts
74 lines (68 loc) · 2.23 KB
/
no-promise-in-fire-event.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
import { TSESTree, ESLintUtils } from '@typescript-eslint/experimental-utils';
import { getDocsUrl, ASYNC_QUERIES_VARIANTS } from '../utils';
import {
isNewExpression,
isIdentifier,
isImportSpecifier,
isCallExpression,
} from '../node-utils';
export const RULE_NAME = 'no-promise-in-fire-event';
export type MessageIds = 'noPromiseInFireEvent';
type Options = [];
export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description:
'Disallow the use of promises passed to a `fireEvent` method',
category: 'Best Practices',
recommended: false,
},
messages: {
noPromiseInFireEvent:
"A promise shouldn't be passed to a `fireEvent` method, instead pass the DOM element",
},
fixable: 'code',
schema: [],
},
defaultOptions: [],
create(context) {
return {
'ImportDeclaration[source.value=/testing-library/]'(
node: TSESTree.ImportDeclaration
) {
const fireEventImportNode = node.specifiers.find(
specifier =>
isImportSpecifier(specifier) &&
specifier.imported &&
'fireEvent' === specifier.imported.name
) as TSESTree.ImportSpecifier;
const { references } = context.getDeclaredVariables(
fireEventImportNode
)[0];
for (const reference of references) {
const referenceNode = reference.identifier;
const callExpression = referenceNode.parent
.parent as TSESTree.CallExpression;
const [element] = callExpression.arguments as TSESTree.Node[];
if (isCallExpression(element) || isNewExpression(element)) {
const methodName = isIdentifier(element.callee)
? element.callee.name
: ((element.callee as TSESTree.MemberExpression)
.property as TSESTree.Identifier).name;
if (
ASYNC_QUERIES_VARIANTS.some(q => methodName.startsWith(q)) ||
methodName === 'Promise'
) {
context.report({
node: element,
messageId: 'noPromiseInFireEvent',
});
}
}
}
},
};
},
});