no-global-regexp-flag-in-query.js
5.55 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RULE_NAME = void 0;
const utils_1 = require("@typescript-eslint/utils");
const create_testing_library_rule_1 = require("../create-testing-library-rule");
const node_utils_1 = require("../node-utils");
exports.RULE_NAME = 'no-global-regexp-flag-in-query';
exports.default = (0, create_testing_library_rule_1.createTestingLibraryRule)({
name: exports.RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description: 'Disallow the use of the global RegExp flag (/g) in queries',
recommendedConfig: {
dom: false,
angular: false,
react: false,
vue: false,
marko: false,
},
},
messages: {
noGlobalRegExpFlagInQuery: 'Avoid using the global RegExp flag (/g) in queries',
},
fixable: 'code',
schema: [],
},
defaultOptions: [],
create(context, _, helpers) {
function reportLiteralWithRegex(literalNode) {
if ((0, node_utils_1.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) {
if ((0, node_utils_1.isCallExpression)(identifierNode.parent)) {
return identifierNode.parent.arguments;
}
else if ((0, node_utils_1.isMemberExpression)(identifierNode.parent) &&
(0, node_utils_1.isCallExpression)(identifierNode.parent.parent)) {
return identifierNode.parent.parent.arguments;
}
return [];
}
const variableNodesWithRegexs = [];
function hasRegexInVariable(identifier) {
return variableNodesWithRegexs.find((varNode) => {
if (utils_1.ASTUtils.isVariableDeclarator(varNode) &&
utils_1.ASTUtils.isIdentifier(varNode.id)) {
return varNode.id.name === identifier.name;
}
return undefined;
});
}
return {
VariableDeclarator(node) {
if (utils_1.ASTUtils.isVariableDeclarator(node) &&
(0, node_utils_1.isLiteral)(node.init) &&
'regex' in node.init &&
node.init.regex.flags.includes('g')) {
variableNodesWithRegexs.push(node);
}
},
CallExpression(node) {
const identifierNode = (0, node_utils_1.getDeepestIdentifierNode)(node);
if (!identifierNode || !helpers.isQuery(identifierNode)) {
return;
}
const [firstArg, secondArg] = getArguments(identifierNode);
const firstArgumentHasError = reportLiteralWithRegex(firstArg);
if (firstArgumentHasError) {
return;
}
if (utils_1.ASTUtils.isIdentifier(firstArg)) {
const regexVariableNode = hasRegexInVariable(firstArg);
if (regexVariableNode !== undefined) {
context.report({
node: firstArg,
messageId: 'noGlobalRegExpFlagInQuery',
fix(fixer) {
if (utils_1.ASTUtils.isVariableDeclarator(regexVariableNode) &&
(0, node_utils_1.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 ((0, node_utils_1.isObjectExpression)(secondArg)) {
const namePropertyNode = secondArg.properties.find((p) => (0, node_utils_1.isProperty)(p) &&
utils_1.ASTUtils.isIdentifier(p.key) &&
p.key.name === 'name' &&
(0, node_utils_1.isLiteral)(p.value));
if (namePropertyNode) {
reportLiteralWithRegex(namePropertyNode.value);
}
}
},
};
},
});