forked from testing-library/eslint-plugin-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-debug.ts
192 lines (175 loc) · 5.53 KB
/
no-debug.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils';
import { getDocsUrl, LIBRARY_MODULES } from '../utils';
import {
isObjectPattern,
isProperty,
isIdentifier,
isCallExpression,
isLiteral,
isMemberExpression,
isImportSpecifier,
isRenderVariableDeclarator,
} from '../node-utils';
export const RULE_NAME = 'no-debug';
function hasTestingLibraryImportModule(
importDeclarationNode: TSESTree.ImportDeclaration
) {
const literal = importDeclarationNode.source;
return LIBRARY_MODULES.some(module => module === literal.value);
}
export default ESLintUtils.RuleCreator(getDocsUrl)({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Disallow unnecessary debug usages in the tests',
category: 'Best Practices',
recommended: 'warn',
},
messages: {
noDebug: 'Unexpected debug statement',
},
fixable: null,
schema: [
{
type: 'object',
properties: {
renderFunctions: {
type: 'array',
},
},
},
],
},
defaultOptions: [
{
renderFunctions: [],
},
],
create(context, [options]) {
let hasDestructuredDebugStatement = false;
const renderVariableDeclarators: TSESTree.VariableDeclarator[] = [];
const { renderFunctions } = options;
let hasImportedScreen = false;
let wildcardImportName: string = null;
return {
VariableDeclarator(node) {
if (isRenderVariableDeclarator(node, renderFunctions)) {
if (
isObjectPattern(node.id) &&
node.id.properties.some(
property =>
isProperty(property) &&
isIdentifier(property.key) &&
property.key.name === 'debug'
)
) {
hasDestructuredDebugStatement = true;
}
if (node.id.type === 'Identifier') {
renderVariableDeclarators.push(node);
}
}
},
[`VariableDeclarator > CallExpression > Identifier[name="require"]`](
node: TSESTree.Identifier
) {
const { arguments: args } = node.parent as TSESTree.CallExpression;
const literalNodeScreenModuleName = args.find(
args =>
isLiteral(args) &&
typeof args.value === 'string' &&
LIBRARY_MODULES.includes(args.value)
);
if (!literalNodeScreenModuleName) {
return;
}
const declaratorNode = node.parent
.parent as TSESTree.VariableDeclarator;
hasImportedScreen =
isObjectPattern(declaratorNode.id) &&
declaratorNode.id.properties.some(
property =>
isProperty(property) &&
isIdentifier(property.key) &&
property.key.name === 'screen'
);
},
// checks if import has shape:
// import { screen } from '@testing-library/dom';
ImportDeclaration(node: TSESTree.ImportDeclaration) {
if (!hasTestingLibraryImportModule(node)) return;
hasImportedScreen = node.specifiers.some(
s => isImportSpecifier(s) && s.imported.name === 'screen'
);
},
// checks if import has shape:
// import * as dtl from '@testing-library/dom';
'ImportDeclaration ImportNamespaceSpecifier'(
node: TSESTree.ImportNamespaceSpecifier
) {
const importDeclarationNode = node.parent as TSESTree.ImportDeclaration;
if (!hasTestingLibraryImportModule(importDeclarationNode)) return;
wildcardImportName = node.local && node.local.name;
},
[`CallExpression > Identifier[name="debug"]`](node: TSESTree.Identifier) {
if (hasDestructuredDebugStatement) {
context.report({
node,
messageId: 'noDebug',
});
}
},
[`CallExpression > MemberExpression > Identifier[name="debug"]`](
node: TSESTree.Identifier
) {
const memberExpression = node.parent as TSESTree.MemberExpression;
const identifier = memberExpression.object as TSESTree.Identifier;
const memberExpressionName = identifier.name;
/*
check if `debug` used following the pattern:
import { screen } from '@testing-library/dom';
...
screen.debug();
*/
const isScreenDebugUsed =
hasImportedScreen && memberExpressionName === 'screen';
/*
check if `debug` used following the pattern:
import * as dtl from '@testing-library/dom';
...
dtl.debug();
*/
const isNamespaceDebugUsed =
wildcardImportName && memberExpressionName === wildcardImportName;
if (isScreenDebugUsed || isNamespaceDebugUsed) {
context.report({
node,
messageId: 'noDebug',
});
}
},
'Program:exit'() {
renderVariableDeclarators.forEach(renderVar => {
const renderVarReferences = context
.getDeclaredVariables(renderVar)[0]
.references.slice(1);
renderVarReferences.forEach(ref => {
const parent = ref.identifier.parent;
if (
isMemberExpression(parent) &&
isIdentifier(parent.property) &&
parent.property.name === 'debug' &&
isCallExpression(parent.parent)
) {
context.report({
node: parent.property,
messageId: 'noDebug',
});
}
});
});
},
};
},
});