/
prefer-regex-literals.js
246 lines (231 loc) · 8.28 KB
/
prefer-regex-literals.js
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
* @fileoverview Tests for the prefer-regex-literals rule
* @author Milos Djermanovic
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/prefer-regex-literals");
const { RuleTester } = require("../../../lib/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 } });
ruleTester.run("prefer-regex-literals", rule, {
valid: [
"/abc/",
"/abc/g",
// considered as dynamic
"new RegExp(pattern)",
"RegExp(pattern, 'g')",
"new RegExp(f('a'))",
"RegExp(prefix + 'a')",
"new RegExp('a' + suffix)",
"RegExp(`a` + suffix);",
"new RegExp(String.raw`a` + suffix);",
"RegExp('a', flags)",
"RegExp('a', 'g' + flags)",
"new RegExp(String.raw`a`, flags);",
"RegExp(`${prefix}abc`)",
"new RegExp(`a${b}c`);",
"new RegExp(`a${''}c`);",
"new RegExp(String.raw`a${b}c`);",
"new RegExp(String.raw`a${''}c`);",
"new RegExp('a' + 'b')",
"RegExp(1)",
"new RegExp(/a/, 'u');",
"new RegExp(/a/);",
{
code: "new RegExp(/a/, flags);",
options: [{ disallowRedundantWrapping: true }]
},
{
code: "new RegExp(/a/, `u${flags}`);",
options: [{ disallowRedundantWrapping: true }]
},
// redundant wrapping is allowed
{
code: "new RegExp(/a/);",
options: [{}]
},
{
code: "new RegExp(/a/);",
options: [{ disallowRedundantWrapping: false }]
},
// invalid number of arguments
"new RegExp;",
"new RegExp();",
"RegExp();",
"new RegExp('a', 'g', 'b');",
"RegExp('a', 'g', 'b');",
"new RegExp(`a`, `g`, `b`);",
"RegExp(`a`, `g`, `b`);",
"new RegExp(String.raw`a`, String.raw`g`, String.raw`b`);",
"RegExp(String.raw`a`, String.raw`g`, String.raw`b`);",
{
code: "new RegExp(/a/, 'u', 'foo');",
options: [{ disallowRedundantWrapping: true }]
},
// not String.raw``
"new RegExp(String`a`);",
"RegExp(raw`a`);",
"new RegExp(f(String.raw)`a`);",
"RegExp(string.raw`a`);",
"new RegExp(String.Raw`a`);",
"new RegExp(String[raw]`a`);",
"RegExp(String.raw.foo`a`);",
"new RegExp(String.foo.raw`a`);",
"RegExp(foo.String.raw`a`);",
"new RegExp(String.raw);",
// not the global String in String.raw``
"let String; new RegExp(String.raw`a`);",
"function foo() { var String; new RegExp(String.raw`a`); }",
"function foo(String) { RegExp(String.raw`a`); }",
"if (foo) { const String = bar; RegExp(String.raw`a`); }",
"/* globals String:off */ new RegExp(String.raw`a`);",
{
code: "RegExp('a', String.raw`g`);",
globals: { String: "off" }
},
// not RegExp
"new Regexp('abc');",
"Regexp(`a`);",
"new Regexp(String.raw`a`);",
// not the global RegExp
"let RegExp; new RegExp('a');",
"function foo() { var RegExp; RegExp('a', 'g'); }",
"function foo(RegExp) { new RegExp(String.raw`a`); }",
"if (foo) { const RegExp = bar; RegExp('a'); }",
"/* globals RegExp:off */ new RegExp('a');",
{
code: "RegExp('a');",
globals: { RegExp: "off" }
},
"new globalThis.RegExp('a');",
{
code: "new globalThis.RegExp('a');",
env: { es6: true }
},
{
code: "new globalThis.RegExp('a');",
env: { es2017: true }
}
],
invalid: [
{
code: "new RegExp('abc');",
errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
},
{
code: "RegExp('abc');",
errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
},
{
code: "new RegExp('abc', 'g');",
errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
},
{
code: "RegExp('abc', 'g');",
errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
},
{
code: "new RegExp(`abc`);",
errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
},
{
code: "RegExp(`abc`);",
errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
},
{
code: "new RegExp(`abc`, `g`);",
errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
},
{
code: "RegExp(`abc`, `g`);",
errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
},
{
code: "new RegExp(String.raw`abc`);",
errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
},
{
code: "RegExp(String.raw`abc`);",
errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
},
{
code: "new RegExp(String.raw`abc`, String.raw`g`);",
errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
},
{
code: "RegExp(String.raw`abc`, String.raw`g`);",
errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
},
{
code: "new RegExp(String['raw']`a`);",
errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
},
{
code: "new RegExp('');",
errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
},
{
code: "RegExp('', '');",
errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
},
{
code: "new RegExp(String.raw``);",
errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
},
{
code: "new RegExp('a', `g`);",
errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
},
{
code: "RegExp(`a`, 'g');",
errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
},
{
code: "RegExp(String.raw`a`, 'g');",
errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
},
{
code: "new RegExp(String.raw`\\d`, `g`);",
errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
},
{
code: "RegExp('a', String.raw`g`);",
errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
},
{
code: "new globalThis.RegExp('a');",
env: { es2020: true },
errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
},
{
code: "globalThis.RegExp('a');",
env: { es2020: true },
errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
},
{
code: "new RegExp(/a/);",
options: [{ disallowRedundantWrapping: true }],
errors: [{ messageId: "unexpectedRedundantRegExp", type: "NewExpression", line: 1, column: 1 }]
},
{
code: "new RegExp(/a/, 'u');",
options: [{ disallowRedundantWrapping: true }],
errors: [{ messageId: "unexpectedRedundantRegExpWithFlags", type: "NewExpression", line: 1, column: 1 }]
},
{
code: "new RegExp(/a/, `u`);",
options: [{ disallowRedundantWrapping: true }],
errors: [{ messageId: "unexpectedRedundantRegExpWithFlags", type: "NewExpression", line: 1, column: 1 }]
},
{
code: "new RegExp('a');",
options: [{ disallowRedundantWrapping: true }],
errors: [{ messageId: "unexpectedRegExp", type: "NewExpression", line: 1, column: 1 }]
}
]
});