-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathno-wait-for-snapshot.ts
134 lines (120 loc) · 4.09 KB
/
no-wait-for-snapshot.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
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils';
import { getDocsUrl, ASYNC_UTILS, LIBRARY_MODULES } from '../utils';
import {
findClosestCallExpressionNode,
isMemberExpression,
} from '../node-utils';
export const RULE_NAME = 'no-wait-for-snapshot';
export type MessageIds = 'noWaitForSnapshot';
type Options = [];
const ASYNC_UTILS_REGEXP = new RegExp(`^(${ASYNC_UTILS.join('|')})$`);
const SNAPSHOT_REGEXP = /^(toMatchSnapshot|toMatchInlineSnapshot)$/;
export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description:
'Ensures no snapshot is generated inside of a `waitFor` call',
category: 'Best Practices',
recommended: 'warn',
},
messages: {
noWaitForSnapshot:
"A snapshot can't be generated inside of a `{{ name }}` call",
},
fixable: null,
schema: [],
},
defaultOptions: [],
create(context) {
const asyncUtilsUsage: Array<{
node: TSESTree.Identifier | TSESTree.MemberExpression;
name: string;
}> = [];
const importedAsyncUtils: string[] = [];
const snapshotUsage: TSESTree.Identifier[] = [];
return {
'ImportDeclaration > ImportSpecifier,ImportNamespaceSpecifier'(
node: TSESTree.Node
) {
const parent = node.parent as TSESTree.ImportDeclaration;
if (!LIBRARY_MODULES.includes(parent.source.value.toString())) {
return;
}
let name;
if (node.type === 'ImportSpecifier') {
name = node.imported.name;
}
if (node.type === 'ImportNamespaceSpecifier') {
name = node.local.name;
}
importedAsyncUtils.push(name);
},
[`CallExpression > Identifier[name=${ASYNC_UTILS_REGEXP}]`](
node: TSESTree.Identifier
) {
asyncUtilsUsage.push({ node, name: node.name });
},
[`CallExpression > MemberExpression > Identifier[name=${ASYNC_UTILS_REGEXP}]`](
node: TSESTree.Identifier
) {
const memberExpression = node.parent as TSESTree.MemberExpression;
const identifier = memberExpression.object as TSESTree.Identifier;
const memberExpressionName = identifier.name;
asyncUtilsUsage.push({
node: memberExpression,
name: memberExpressionName,
});
},
[`Identifier[name=${SNAPSHOT_REGEXP}]`](node: TSESTree.Identifier) {
snapshotUsage.push(node);
},
'Program:exit'() {
const testingLibraryUtilUsage = asyncUtilsUsage.filter((usage) => {
if (isMemberExpression(usage.node)) {
const object = usage.node.object as TSESTree.Identifier;
return importedAsyncUtils.includes(object.name);
}
return importedAsyncUtils.includes(usage.name);
});
function getClosestAsyncUtil(
asyncUtilUsage: {
node: TSESTree.Identifier | TSESTree.MemberExpression;
name: string;
},
node: TSESTree.Node
) {
let callExpression = findClosestCallExpressionNode(node);
while (callExpression != null) {
if (callExpression.callee === asyncUtilUsage.node)
return asyncUtilUsage;
callExpression = findClosestCallExpressionNode(
callExpression.parent
);
}
return null;
}
snapshotUsage.forEach((node) => {
testingLibraryUtilUsage.forEach((asyncUtilUsage) => {
const closestAsyncUtil = getClosestAsyncUtil(asyncUtilUsage, node);
if (closestAsyncUtil != null) {
let name;
if (isMemberExpression(closestAsyncUtil.node)) {
name = (closestAsyncUtil.node.property as TSESTree.Identifier)
.name;
} else {
name = closestAsyncUtil.name;
}
context.report({
node,
messageId: 'noWaitForSnapshot',
data: { name },
});
}
});
});
},
};
},
});