forked from testing-library/eslint-plugin-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-side-effects-wait-for.ts
70 lines (62 loc) · 2.26 KB
/
no-side-effects-wait-for.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
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils'
import { getDocsUrl, hasTestingLibraryImportModule } from '../utils'
import { isBlockStatement, findClosestCallNode, isMemberExpression, isCallExpression, isIdentifier } from '../node-utils'
export const RULE_NAME = 'no-side-effects-wait-for';
const WAIT_EXPRESSION_QUERY =
'CallExpression[callee.name=/^(waitFor)$/]';
const SIDE_EFFECTS: Array<string> = ['fireEvent', 'userEvent']
export type MessageIds = 'noSideEffectsWaitFor';
type Options = [];
export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description:
"It's preferred to avoid side effects in `waitFor`",
category: 'Best Practices',
recommended: false,
},
messages: {
noSideEffectsWaitFor: 'Avoid using side effects within `waitFor` callback',
},
fixable: null,
schema: [],
},
defaultOptions: [],
create: function(context) {
let isImportingTestingLibrary = false;
function reportSideEffects(
node: TSESTree.BlockStatement
) {
const hasSideEffects = (body: Array<TSESTree.Node>): boolean =>
body.some((node: TSESTree.ExpressionStatement) => {
if (
isCallExpression(node.expression) &&
isMemberExpression(node.expression.callee) &&
isIdentifier(node.expression.callee.object)
) {
const object: TSESTree.Identifier = node.expression.callee.object
const identifierName: string = object.name
return SIDE_EFFECTS.includes(identifierName)
} else {
return false
}
})
if (isImportingTestingLibrary && isBlockStatement(node) && hasSideEffects(node.body)) {
context.report({
node,
loc: node.loc.start,
messageId: 'noSideEffectsWaitFor',
});
}
}
return {
[`${WAIT_EXPRESSION_QUERY} > ArrowFunctionExpression > BlockStatement`]: reportSideEffects,
[`${WAIT_EXPRESSION_QUERY} > FunctionExpression > BlockStatement`]: reportSideEffects,
ImportDeclaration(node: TSESTree.ImportDeclaration) {
isImportingTestingLibrary = hasTestingLibraryImportModule(node);
}
};
}
})