]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/prefer-named-capture-group.js
import 8.41.0 source
[pve-eslint.git] / eslint / tests / lib / rules / prefer-named-capture-group.js
1 /**
2 * @fileoverview Tests for prefer-named-capture-group rule.
3 * @author Pig Fang <https://github.com/g-plane>
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/prefer-named-capture-group"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018 } });
20
21 ruleTester.run("prefer-named-capture-group", rule, {
22 valid: [
23 "/normal_regex/",
24 "/(?:[0-9]{4})/",
25 "/(?<year>[0-9]{4})/",
26 "/\\u{1F680}/u",
27 "new RegExp()",
28 "new RegExp(foo)",
29 "new RegExp('')",
30 "new RegExp('(?<year>[0-9]{4})')",
31 "RegExp()",
32 "RegExp(foo)",
33 "RegExp('')",
34 "RegExp('(?<year>[0-9]{4})')",
35 "RegExp('(')", // invalid regexp should be ignored
36 "RegExp('\\\\u{1F680}', 'u')",
37 "new globalThis.RegExp('([0-9]{4})')",
38 {
39 code: "new globalThis.RegExp('([0-9]{4})')",
40 env: { es6: true }
41 },
42 {
43 code: "new globalThis.RegExp('([0-9]{4})')",
44 env: { es2017: true }
45 },
46 {
47 code: "new globalThis.RegExp()",
48 env: { es2020: true }
49 },
50 {
51 code: "new globalThis.RegExp(foo)",
52 env: { es2020: true }
53 },
54 {
55 code: "globalThis.RegExp(foo)",
56 env: { es2020: true }
57 },
58 {
59 code: `
60 var globalThis = bar;
61 globalThis.RegExp(foo);
62 `,
63 env: { es2020: true }
64 },
65 {
66 code: `
67 function foo () {
68 var globalThis = bar;
69 new globalThis.RegExp(baz);
70 }
71 `,
72 env: { es2020: true }
73 }
74 ],
75
76 invalid: [
77 {
78 code: "/([0-9]{4})/",
79 errors: [{
80 messageId: "required",
81 type: "Literal",
82 data: { group: "([0-9]{4})" },
83 line: 1,
84 column: 1,
85 endColumn: 13,
86 suggestions: [
87 {
88 messageId: "addGroupName",
89 output: "/(?<temp1>[0-9]{4})/"
90 },
91 {
92 messageId: "addNonCapture",
93 output: "/(?:[0-9]{4})/"
94 }
95 ]
96 }]
97 },
98 {
99 code: "new RegExp('([0-9]{4})')",
100 errors: [{
101 messageId: "required",
102 type: "NewExpression",
103 data: { group: "([0-9]{4})" },
104 line: 1,
105 column: 1,
106 endColumn: 25,
107 suggestions: [
108 {
109 messageId: "addGroupName",
110 output: "new RegExp('(?<temp1>[0-9]{4})')"
111 },
112 {
113 messageId: "addNonCapture",
114 output: "new RegExp('(?:[0-9]{4})')"
115 }
116 ]
117 }]
118 },
119 {
120 code: "RegExp('([0-9]{4})')",
121 errors: [{
122 messageId: "required",
123 type: "CallExpression",
124 data: { group: "([0-9]{4})" },
125 line: 1,
126 column: 1,
127 endColumn: 21,
128 suggestions: [
129 {
130 messageId: "addGroupName",
131 output: "RegExp('(?<temp1>[0-9]{4})')"
132 },
133 {
134 messageId: "addNonCapture",
135 output: "RegExp('(?:[0-9]{4})')"
136 }
137 ]
138 }]
139 },
140 {
141 code: "new RegExp(`a(bc)d`)",
142 errors: [{
143 messageId: "required",
144 type: "NewExpression",
145 data: { group: "(bc)" },
146 suggestions: [
147 {
148 messageId: "addGroupName",
149 output: "new RegExp(`a(?<temp1>bc)d`)"
150 },
151 {
152 messageId: "addNonCapture",
153 output: "new RegExp(`a(?:bc)d`)"
154 }
155 ]
156 }]
157 },
158 {
159 code: "new RegExp('\u1234\u5678(?:a)(b)');",
160 errors: [{
161 messageId: "required",
162 type: "NewExpression",
163 data: { group: "(b)" },
164 suggestions: [
165 {
166 messageId: "addGroupName",
167 output: "new RegExp('\u1234\u5678(?:a)(?<temp1>b)');"
168 },
169 {
170 messageId: "addNonCapture",
171 output: "new RegExp('\u1234\u5678(?:a)(?:b)');"
172 }
173 ]
174 }]
175 },
176 {
177 code: "new RegExp('\\u1234\\u5678(?:a)(b)');",
178 errors: [{
179 messageId: "required",
180 type: "NewExpression",
181 data: { group: "(b)" },
182 suggestions: null
183 }]
184 },
185 {
186 code: "/([0-9]{4})-(\\w{5})/",
187 errors: [
188 {
189 messageId: "required",
190 type: "Literal",
191 data: { group: "([0-9]{4})" },
192 line: 1,
193 column: 1,
194 endColumn: 21,
195 suggestions: [
196 {
197 messageId: "addGroupName",
198 output: "/(?<temp1>[0-9]{4})-(\\w{5})/"
199 },
200 {
201 messageId: "addNonCapture",
202 output: "/(?:[0-9]{4})-(\\w{5})/"
203 }
204 ]
205 },
206 {
207 messageId: "required",
208 type: "Literal",
209 data: { group: "(\\w{5})" },
210 line: 1,
211 column: 1,
212 endColumn: 21,
213 suggestions: [
214 {
215 messageId: "addGroupName",
216 output: "/([0-9]{4})-(?<temp1>\\w{5})/"
217 },
218 {
219 messageId: "addNonCapture",
220 output: "/([0-9]{4})-(?:\\w{5})/"
221 }
222 ]
223 }
224 ]
225 },
226 {
227 code: "/([0-9]{4})-(5)/",
228 errors: [
229 {
230 messageId: "required",
231 type: "Literal",
232 data: { group: "([0-9]{4})" },
233 line: 1,
234 column: 1,
235 endColumn: 17,
236 suggestions: [
237 {
238 messageId: "addGroupName",
239 output: "/(?<temp1>[0-9]{4})-(5)/"
240 },
241 {
242 messageId: "addNonCapture",
243 output: "/(?:[0-9]{4})-(5)/"
244 }
245 ]
246 },
247 {
248 messageId: "required",
249 type: "Literal",
250 data: { group: "(5)" },
251 line: 1,
252 column: 1,
253 endColumn: 17,
254 suggestions: [
255 {
256 messageId: "addGroupName",
257 output: "/([0-9]{4})-(?<temp1>5)/"
258 },
259 {
260 messageId: "addNonCapture",
261 output: "/([0-9]{4})-(?:5)/"
262 }
263 ]
264 }
265 ]
266 },
267 {
268 code: "/(?<temp2>(a))/",
269 errors: [
270 {
271 messageId: "required",
272 type: "Literal",
273 data: { group: "(a)" },
274 line: 1,
275 column: 1,
276 endColumn: 16,
277 suggestions: [
278 {
279 messageId: "addGroupName",
280 output: "/(?<temp2>(?<temp3>a))/"
281 },
282 {
283 messageId: "addNonCapture",
284 output: "/(?<temp2>(?:a))/"
285 }
286 ]
287 }
288 ]
289 },
290 {
291 code: "/(?<temp2>(a)(?<temp5>b))/",
292 errors: [
293 {
294 messageId: "required",
295 type: "Literal",
296 data: { group: "(a)" },
297 line: 1,
298 column: 1,
299 endColumn: 27,
300 suggestions: [
301 {
302 messageId: "addGroupName",
303 output: "/(?<temp2>(?<temp6>a)(?<temp5>b))/"
304 },
305 {
306 messageId: "addNonCapture",
307 output: "/(?<temp2>(?:a)(?<temp5>b))/"
308 }
309 ]
310 }
311 ]
312 },
313 {
314 code: "/(?<temp1>[0-9]{4})-(\\w{5})/",
315 errors: [
316 {
317 messageId: "required",
318 type: "Literal",
319 data: { group: "(\\w{5})" },
320 line: 1,
321 column: 1,
322 endColumn: 29,
323 suggestions: [
324 {
325 messageId: "addGroupName",
326 output: "/(?<temp1>[0-9]{4})-(?<temp2>\\w{5})/"
327 },
328 {
329 messageId: "addNonCapture",
330 output: "/(?<temp1>[0-9]{4})-(?:\\w{5})/"
331 }
332 ]
333 }
334 ]
335 },
336 {
337 code: "/(?<temp1>[0-9]{4})-(5)/",
338 errors: [
339 {
340 messageId: "required",
341 type: "Literal",
342 data: { group: "(5)" },
343 line: 1,
344 column: 1,
345 endColumn: 25,
346 suggestions: [
347 {
348 messageId: "addGroupName",
349 output: "/(?<temp1>[0-9]{4})-(?<temp2>5)/"
350 },
351 {
352 messageId: "addNonCapture",
353 output: "/(?<temp1>[0-9]{4})-(?:5)/"
354 }
355 ]
356 }
357 ]
358 },
359 {
360 code: "/(?<temp1>a)(?<temp2>a)(a)(?<temp3>a)/",
361 errors: [
362 {
363 messageId: "required",
364 type: "Literal",
365 data: { group: "(a)" },
366 line: 1,
367 column: 1,
368 endColumn: 39,
369 suggestions: [
370 {
371 messageId: "addGroupName",
372 output: "/(?<temp1>a)(?<temp2>a)(?<temp4>a)(?<temp3>a)/"
373 },
374 {
375 messageId: "addNonCapture",
376 output: "/(?<temp1>a)(?<temp2>a)(?:a)(?<temp3>a)/"
377 }
378 ]
379 }
380 ]
381 },
382 {
383 code: "new RegExp('(' + 'a)')",
384 errors: [{
385 messageId: "required",
386 type: "NewExpression",
387 data: { group: "(a)" },
388 suggestions: null
389 }]
390 },
391 {
392 code: "new RegExp('a(bc)d' + 'e')",
393 errors: [{
394 messageId: "required",
395 type: "NewExpression",
396 data: { group: "(bc)" },
397 suggestions: null
398 }]
399 },
400 {
401 code: "new RegExp(\"foo\" + \"(a)\" + \"(b)\");",
402 errors: [
403 {
404 messageId: "required",
405 type: "NewExpression",
406 data: { group: "(a)" },
407 suggestions: null
408 },
409 {
410 messageId: "required",
411 type: "NewExpression",
412 data: { group: "(b)" },
413 suggestions: null
414 }
415 ]
416 },
417 {
418 code: "new RegExp(\"foo\" + \"(?:a)\" + \"(b)\");",
419 errors: [{
420 messageId: "required",
421 type: "NewExpression",
422 data: { group: "(b)" },
423 suggestions: null
424 }]
425 },
426 {
427 code: "RegExp('(a)'+'')",
428 errors: [{
429 messageId: "required",
430 type: "CallExpression",
431 data: { group: "(a)" },
432 suggestions: null
433 }]
434 },
435 {
436 code: "RegExp( '' + '(ab)')",
437 errors: [{
438 messageId: "required",
439 type: "CallExpression",
440 data: { group: "(ab)" },
441 suggestions: null
442 }]
443 },
444 {
445 code: "new RegExp(`(ab)${''}`)",
446 errors: [{
447 messageId: "required",
448 type: "NewExpression",
449 data: { group: "(ab)" },
450 suggestions: null
451 }]
452 },
453 {
454 code: "new RegExp(`(a)\n`)",
455 errors: [{
456 messageId: "required",
457 type: "NewExpression",
458 data: { group: "(a)" },
459 line: 1,
460 column: 1,
461 endLine: 2,
462 endColumn: 3,
463 suggestions: [
464 {
465 messageId: "addGroupName",
466 output: "new RegExp(`(?<temp1>a)\n`)"
467 },
468 {
469 messageId: "addNonCapture",
470 output: "new RegExp(`(?:a)\n`)"
471 }
472 ]
473 }]
474 },
475 {
476 code: "RegExp(`a(b\nc)d`)",
477 errors: [{
478 messageId: "required",
479 type: "CallExpression",
480 data: { group: "(b\nc)" },
481 suggestions: [
482 {
483 messageId: "addGroupName",
484 output: "RegExp(`a(?<temp1>b\nc)d`)"
485 },
486 {
487 messageId: "addNonCapture",
488 output: "RegExp(`a(?:b\nc)d`)"
489 }
490 ]
491 }]
492 },
493 {
494 code: "new RegExp('a(b)\\'')",
495 errors: [{
496 messageId: "required",
497 type: "NewExpression",
498 data: { group: "(b)" },
499 suggestions: null
500 }]
501 },
502 {
503 code: "RegExp('(a)\\\\d')",
504 errors: [{
505 messageId: "required",
506 type: "CallExpression",
507 data: { group: "(a)" },
508 suggestions: null
509 }]
510 },
511 {
512 code: "RegExp(`\\a(b)`)",
513 errors: [{
514 messageId: "required",
515 type: "CallExpression",
516 data: { group: "(b)" },
517 suggestions: null
518 }]
519 },
520 {
521 code: "new globalThis.RegExp('([0-9]{4})')",
522 env: { es2020: true },
523 errors: [{
524 messageId: "required",
525 type: "NewExpression",
526 data: { group: "([0-9]{4})" },
527 line: 1,
528 column: 1,
529 endColumn: 36,
530 suggestions: [
531 {
532 messageId: "addGroupName",
533 output: "new globalThis.RegExp('(?<temp1>[0-9]{4})')"
534 },
535 {
536 messageId: "addNonCapture",
537 output: "new globalThis.RegExp('(?:[0-9]{4})')"
538 }
539 ]
540 }]
541 },
542 {
543 code: "globalThis.RegExp('([0-9]{4})')",
544 env: { es2020: true },
545 errors: [{
546 messageId: "required",
547 type: "CallExpression",
548 data: { group: "([0-9]{4})" },
549 line: 1,
550 column: 1,
551 endColumn: 32,
552 suggestions: [
553 {
554 messageId: "addGroupName",
555 output: "globalThis.RegExp('(?<temp1>[0-9]{4})')"
556 },
557 {
558 messageId: "addNonCapture",
559 output: "globalThis.RegExp('(?:[0-9]{4})')"
560 }
561 ]
562 }]
563 },
564 {
565 code: `
566 function foo() { var globalThis = bar; }
567 new globalThis.RegExp('([0-9]{4})');
568 `,
569 env: { es2020: true },
570 errors: [{
571 messageId: "required",
572 type: "NewExpression",
573 data: { group: "([0-9]{4})" },
574 line: 3,
575 column: 17,
576 endColumn: 52,
577 suggestions: [
578 {
579 messageId: "addGroupName",
580 output: `
581 function foo() { var globalThis = bar; }
582 new globalThis.RegExp('(?<temp1>[0-9]{4})');
583 `
584 },
585 {
586 messageId: "addNonCapture",
587 output: `
588 function foo() { var globalThis = bar; }
589 new globalThis.RegExp('(?:[0-9]{4})');
590 `
591 }
592 ]
593 }]
594 }
595 ]
596 });