forked from testing-library/eslint-plugin-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-global-regexp-flag-in-query.ts
179 lines (163 loc) · 4.95 KB
/
no-global-regexp-flag-in-query.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
import { ASTUtils, TSESTree } from '@typescript-eslint/utils';
import { createTestingLibraryRule } from '../create-testing-library-rule';
import {
isMemberExpression,
isCallExpression,
isProperty,
isObjectExpression,
getDeepestIdentifierNode,
isLiteral,
} from '../node-utils';
export const RULE_NAME = 'no-global-regexp-flag-in-query';
export type MessageIds = 'noGlobalRegExpFlagInQuery';
type Options = [];
export default createTestingLibraryRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description: 'Disallow the use of the global RegExp flag (/g) in queries',
recommendedConfig: {
dom: 'error',
angular: 'error',
react: 'error',
vue: 'error',
svelte: 'error',
marko: 'error',
},
},
messages: {
noGlobalRegExpFlagInQuery:
'Avoid using the global RegExp flag (/g) in queries',
},
fixable: 'code',
schema: [],
},
defaultOptions: [],
create(context, _, helpers) {
/**
* Checks if node is reportable (has a regex that contains 'g') and if it is, reports it with `context.report()`.
*
* @param literalNode Literal node under to be
* @returns {Boolean} indicatinf if literal was reported
*/
function reportLiteralWithRegex(literalNode: TSESTree.Node) {
if (
isLiteral(literalNode) &&
'regex' in literalNode &&
literalNode.regex.flags.includes('g')
) {
context.report({
node: literalNode,
messageId: 'noGlobalRegExpFlagInQuery',
fix(fixer) {
const splitter = literalNode.raw.lastIndexOf('/');
const raw = literalNode.raw.substring(0, splitter);
const flags = literalNode.raw.substring(splitter + 1);
const flagsWithoutGlobal = flags.replace('g', '');
return fixer.replaceText(
literalNode,
`${raw}/${flagsWithoutGlobal}`
);
},
});
return true;
}
return false;
}
function getArguments(identifierNode: TSESTree.Identifier) {
if (isCallExpression(identifierNode.parent)) {
return identifierNode.parent.arguments;
} else if (
isMemberExpression(identifierNode.parent) &&
isCallExpression(identifierNode.parent.parent)
) {
return identifierNode.parent.parent.arguments;
}
return [];
}
// Helper array to store variable nodes that have a literal with regex
// e.g. `const countRegExp = /count/gi` will be store here
const variableNodesWithRegexs: TSESTree.VariableDeclarator[] = [];
function hasRegexInVariable(
identifier: TSESTree.Identifier
): TSESTree.VariableDeclarator | undefined {
return variableNodesWithRegexs.find((varNode) => {
if (
ASTUtils.isVariableDeclarator(varNode) &&
ASTUtils.isIdentifier(varNode.id)
) {
return varNode.id.name === identifier.name;
}
return undefined;
});
}
return {
// internal helper function, helps store all variables with regex to `variableNodesWithRegexs`
// could potentially be refactored to using context.getDeclaredVariables()
VariableDeclarator(node: TSESTree.Node) {
if (
ASTUtils.isVariableDeclarator(node) &&
isLiteral(node.init) &&
'regex' in node.init &&
node.init.regex.flags.includes('g')
) {
variableNodesWithRegexs.push(node);
}
},
CallExpression(node) {
const identifierNode = getDeepestIdentifierNode(node);
if (!identifierNode || !helpers.isQuery(identifierNode)) {
return;
}
const [firstArg, secondArg] = getArguments(identifierNode);
const firstArgumentHasError = reportLiteralWithRegex(firstArg);
if (firstArgumentHasError) {
return;
}
// Case issue #592: a variable that has a regex is passed to testing library query
if (ASTUtils.isIdentifier(firstArg)) {
const regexVariableNode = hasRegexInVariable(firstArg);
if (regexVariableNode !== undefined) {
context.report({
node: firstArg,
messageId: 'noGlobalRegExpFlagInQuery',
fix(fixer) {
if (
ASTUtils.isVariableDeclarator(regexVariableNode) &&
isLiteral(regexVariableNode.init) &&
'regex' in regexVariableNode.init &&
regexVariableNode.init.regex.flags.includes('g')
) {
const splitter = regexVariableNode.init.raw.lastIndexOf('/');
const raw = regexVariableNode.init.raw.substring(0, splitter);
const flags = regexVariableNode.init.raw.substring(
splitter + 1
);
const flagsWithoutGlobal = flags.replace('g', '');
return fixer.replaceText(
regexVariableNode.init,
`${raw}/${flagsWithoutGlobal}`
);
}
return null;
},
});
}
}
if (isObjectExpression(secondArg)) {
const namePropertyNode = secondArg.properties.find(
(p) =>
isProperty(p) &&
ASTUtils.isIdentifier(p.key) &&
p.key.name === 'name' &&
isLiteral(p.value)
) as TSESTree.Property | undefined;
if (namePropertyNode) {
reportLiteralWithRegex(namePropertyNode.value);
}
}
},
};
},
});