]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/prefer-regex-literals.js
ccd88ae74fd5053033e8127ae32d8c954f1f3339
[pve-eslint.git] / eslint / tests / lib / rules / prefer-regex-literals.js
1 /**
2 * @fileoverview Tests for the prefer-regex-literals rule
3 * @author Milos Djermanovic
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/prefer-regex-literals");
13 const { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2022 } });
20
21 ruleTester.run("prefer-regex-literals", rule, {
22 valid: [
23 "/abc/",
24 "/abc/g",
25
26
27 // considered as dynamic
28 "new RegExp(pattern)",
29 "RegExp(pattern, 'g')",
30 "new RegExp(f('a'))",
31 "RegExp(prefix + 'a')",
32 "new RegExp('a' + suffix)",
33 "RegExp(`a` + suffix);",
34 "new RegExp(String.raw`a` + suffix);",
35 "RegExp('a', flags)",
36 "RegExp('a', 'g' + flags)",
37 "new RegExp(String.raw`a`, flags);",
38 "RegExp(`${prefix}abc`)",
39 "new RegExp(`a${b}c`);",
40 "new RegExp(`a${''}c`);",
41 "new RegExp(String.raw`a${b}c`);",
42 "new RegExp(String.raw`a${''}c`);",
43 "new RegExp('a' + 'b')",
44 "RegExp(1)",
45 "new RegExp(/a/, 'u');",
46 "new RegExp(/a/);",
47 {
48 code: "new RegExp(/a/, flags);",
49 options: [{ disallowRedundantWrapping: true }]
50 },
51 {
52 code: "new RegExp(/a/, `u${flags}`);",
53 options: [{ disallowRedundantWrapping: true }]
54 },
55
56 // redundant wrapping is allowed
57 {
58 code: "new RegExp(/a/);",
59 options: [{}]
60 },
61 {
62 code: "new RegExp(/a/);",
63 options: [{ disallowRedundantWrapping: false }]
64 },
65
66 // invalid number of arguments
67 "new RegExp;",
68 "new RegExp();",
69 "RegExp();",
70 "new RegExp('a', 'g', 'b');",
71 "RegExp('a', 'g', 'b');",
72 "new RegExp(`a`, `g`, `b`);",
73 "RegExp(`a`, `g`, `b`);",
74 "new RegExp(String.raw`a`, String.raw`g`, String.raw`b`);",
75 "RegExp(String.raw`a`, String.raw`g`, String.raw`b`);",
76 {
77 code: "new RegExp(/a/, 'u', 'foo');",
78 options: [{ disallowRedundantWrapping: true }]
79 },
80
81 // not String.raw``
82 "new RegExp(String`a`);",
83 "RegExp(raw`a`);",
84 "new RegExp(f(String.raw)`a`);",
85 "RegExp(string.raw`a`);",
86 "new RegExp(String.Raw`a`);",
87 "new RegExp(String[raw]`a`);",
88 "RegExp(String.raw.foo`a`);",
89 "new RegExp(String.foo.raw`a`);",
90 "RegExp(foo.String.raw`a`);",
91 "new RegExp(String.raw);",
92
93 // not the global String in String.raw``
94 "let String; new RegExp(String.raw`a`);",
95 "function foo() { var String; new RegExp(String.raw`a`); }",
96 "function foo(String) { RegExp(String.raw`a`); }",
97 "if (foo) { const String = bar; RegExp(String.raw`a`); }",
98 "/* globals String:off */ new RegExp(String.raw`a`);",
99 {
100 code: "RegExp('a', String.raw`g`);",
101 globals: { String: "off" }
102 },
103
104 // not RegExp
105 "new Regexp('abc');",
106 "Regexp(`a`);",
107 "new Regexp(String.raw`a`);",
108
109 // not the global RegExp
110 "let RegExp; new RegExp('a');",
111 "function foo() { var RegExp; RegExp('a', 'g'); }",
112 "function foo(RegExp) { new RegExp(String.raw`a`); }",
113 "if (foo) { const RegExp = bar; RegExp('a'); }",
114 "/* globals RegExp:off */ new RegExp('a');",
115 {
116 code: "RegExp('a');",
117 globals: { RegExp: "off" }
118 },
119 "new globalThis.RegExp('a');",
120 {
121 code: "new globalThis.RegExp('a');",
122 env: { es6: true }
123 },
124 {
125 code: "new globalThis.RegExp('a');",
126 env: { es2017: true }
127 },
128 {
129 code: "class C { #RegExp; foo() { globalThis.#RegExp('a'); } }",
130 env: { es2020: true }
131 }
132 ],
133
134 invalid: [
135 {
136 code: "new RegExp('abc');",
137 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
138 },
139 {
140 code: "RegExp('abc');",
141 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
142 },
143 {
144 code: "new RegExp('abc', 'g');",
145 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
146 },
147 {
148 code: "RegExp('abc', 'g');",
149 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
150 },
151 {
152 code: "new RegExp(`abc`);",
153 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
154 },
155 {
156 code: "RegExp(`abc`);",
157 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
158 },
159 {
160 code: "new RegExp(`abc`, `g`);",
161 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
162 },
163 {
164 code: "RegExp(`abc`, `g`);",
165 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
166 },
167 {
168 code: "new RegExp(String.raw`abc`);",
169 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
170 },
171 {
172 code: "RegExp(String.raw`abc`);",
173 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
174 },
175 {
176 code: "new RegExp(String.raw`abc`, String.raw`g`);",
177 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
178 },
179 {
180 code: "RegExp(String.raw`abc`, String.raw`g`);",
181 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
182 },
183 {
184 code: "new RegExp(String['raw']`a`);",
185 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
186 },
187 {
188 code: "new RegExp('');",
189 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
190 },
191 {
192 code: "RegExp('', '');",
193 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
194 },
195 {
196 code: "new RegExp(String.raw``);",
197 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
198 },
199 {
200 code: "new RegExp('a', `g`);",
201 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
202 },
203 {
204 code: "RegExp(`a`, 'g');",
205 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
206 },
207 {
208 code: "RegExp(String.raw`a`, 'g');",
209 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
210 },
211 {
212 code: "new RegExp(String.raw`\\d`, `g`);",
213 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
214 },
215 {
216 code: "RegExp('a', String.raw`g`);",
217 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
218 },
219 {
220 code: "new globalThis.RegExp('a');",
221 env: { es2020: true },
222 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
223 },
224 {
225 code: "globalThis.RegExp('a');",
226 env: { es2020: true },
227 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
228 },
229
230 {
231 code: "new RegExp(/a/);",
232 options: [{ disallowRedundantWrapping: true }],
233 errors: [{ messageId: "unexpectedRedundantRegExp", type: "NewExpression", line: 1, column: 1 }]
234 },
235 {
236 code: "new RegExp(/a/, 'u');",
237 options: [{ disallowRedundantWrapping: true }],
238 errors: [{ messageId: "unexpectedRedundantRegExpWithFlags", type: "NewExpression", line: 1, column: 1 }]
239 },
240 {
241 code: "new RegExp(/a/, `u`);",
242 options: [{ disallowRedundantWrapping: true }],
243 errors: [{ messageId: "unexpectedRedundantRegExpWithFlags", type: "NewExpression", line: 1, column: 1 }]
244 },
245 {
246 code: "new RegExp('a');",
247 options: [{ disallowRedundantWrapping: true }],
248 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression", line: 1, column: 1 }]
249 },
250
251 // Optional chaining
252 {
253 code: "new RegExp((String?.raw)`a`);",
254 errors: [{ messageId: "unexpectedRegExp" }]
255 }
256 ]
257 });