Skip to content

Commit 36ebbb7

Browse files
authored
Add test cases for v flag (#636)
1 parent 9605f0f commit 36ebbb7

7 files changed

+137
-7
lines changed

tests/lib/rules/no-empty-capturing-group.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import rule from "../../../lib/rules/no-empty-capturing-group"
33

44
const tester = new RuleTester({
55
parserOptions: {
6-
ecmaVersion: 2020,
6+
ecmaVersion: "latest",
77
sourceType: "module",
88
},
99
})
1010

1111
tester.run("no-empty-capturing-group", rule as any, {
12-
valid: ["/(a)/", "/a(\\bb)/", "/a(\\b|b)/"],
12+
valid: ["/(a)/", "/a(\\bb)/", "/a(\\b|b)/", String.raw`/a([\q{a}])/v`],
1313
invalid: [
1414
{
1515
code: "/a(\\b)/",
@@ -55,5 +55,14 @@ tester.run("no-empty-capturing-group", rule as any, {
5555
code: "/(\\b\\b|(?:\\B|$))a/",
5656
errors: ["Unexpected capture empty."],
5757
},
58+
{
59+
code: String.raw`/a([\q{}])/v`,
60+
errors: [
61+
{
62+
message: "Unexpected capture empty.",
63+
line: 1,
64+
},
65+
],
66+
},
5867
],
5968
})

tests/lib/rules/no-invalid-regexp.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import rule from "../../../lib/rules/no-invalid-regexp"
33

44
const tester = new RuleTester({
55
parserOptions: {
6-
ecmaVersion: 2020,
6+
ecmaVersion: "latest",
77
sourceType: "module",
88
},
99
})
@@ -63,5 +63,15 @@ tester.run("no-invalid-regexp", rule as any, {
6363
},
6464
],
6565
},
66+
{
67+
code: "new RegExp('[A&&&]', 'v');",
68+
errors: [
69+
{
70+
message:
71+
"Invalid regular expression: /[A&&&]/v: Invalid character in character class",
72+
column: 16,
73+
},
74+
],
75+
},
6676
],
6777
})

tests/lib/rules/no-useless-dollar-replacements.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import rule from "../../../lib/rules/no-useless-dollar-replacements"
33

44
const tester = new RuleTester({
55
parserOptions: {
6-
ecmaVersion: 2020,
6+
ecmaVersion: "latest",
77
sourceType: "module",
88
},
99
})
@@ -91,6 +91,10 @@ tester.run("no-useless-dollar-replacements", rule as any, {
9191
regexp = 'a'
9292
"abc".replaceAll(regexp, 'foo');
9393
`,
94+
String.raw`
95+
const str = 'John Smith';
96+
var newStr = str.replace(/(\w+)\s(\w+)/v, '$2, $1');
97+
`,
9498
],
9599
invalid: [
96100
{
@@ -171,5 +175,19 @@ tester.run("no-useless-dollar-replacements", rule as any, {
171175
"'$<a>' replacement will insert '$<a>' because named capturing group does not found. Use '$$' if you want to escape '$'.",
172176
],
173177
},
178+
{
179+
code: String.raw`
180+
const str = 'John Smith';
181+
var newStr = str.replace(/([[\w]]+)[\s](\w+)/v, '$3, $1 $2');
182+
`,
183+
errors: [
184+
{
185+
message:
186+
"'$3' replacement will insert '$3' because there are less than 3 capturing groups. Use '$$' if you want to escape '$'.",
187+
line: 3,
188+
column: 62,
189+
},
190+
],
191+
},
174192
],
175193
})

tests/lib/rules/prefer-escape-replacement-dollar-char.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ tester.run("prefer-escape-replacement-dollar-char", rule as any, {
1919
`'abc'.replace(/./, "$\`$'");`,
2020
`'abc'.replace(/(.)/, '$1');`,
2121
`'abc'.replace(/(?<foo>.)/, '$<foo>');`,
22+
String.raw`'€1,234'.replace(/[\q{€}]/v, '$$'); // "$1,234"`,
2223
],
2324
invalid: [
2425
{
@@ -67,5 +68,11 @@ tester.run("prefer-escape-replacement-dollar-char", rule as any, {
6768
"Unexpected replacement `$` character without escaping. Use `$$` instead.",
6869
],
6970
},
71+
{
72+
code: String.raw`'€1,234'.replace(/[\q{€}]/v, '$'); // "$1,234"`,
73+
errors: [
74+
"Unexpected replacement `$` character without escaping. Use `$$` instead.",
75+
],
76+
},
7077
],
7178
})

tests/lib/rules/prefer-regexp-exec.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import rule from "../../../lib/rules/prefer-regexp-exec"
33

44
const tester = new RuleTester({
55
parserOptions: {
6-
ecmaVersion: 2020,
6+
ecmaVersion: "latest",
77
sourceType: "module",
88
},
99
})
@@ -19,6 +19,9 @@ tester.run("prefer-regexp-exec", rule as any, {
1919
const search = /thing/;
2020
search.exec(text);
2121
`,
22+
`
23+
/thin[[g]]/v.exec('something');
24+
`,
2225
],
2326
invalid: [
2427
{
@@ -72,5 +75,17 @@ tester.run("prefer-regexp-exec", rule as any, {
7275
},
7376
],
7477
},
78+
{
79+
code: `
80+
'something'.match(/thin[[g]]/v);
81+
`,
82+
errors: [
83+
{
84+
message: "Use the `RegExp#exec()` method instead.",
85+
line: 2,
86+
column: 13,
87+
},
88+
],
89+
},
7590
],
7691
})

tests/lib/rules/prefer-regexp-test.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import rule from "../../../lib/rules/prefer-regexp-test"
33

44
const tester = new RuleTester({
55
parserOptions: {
6-
ecmaVersion: 2020,
6+
ecmaVersion: "latest",
77
sourceType: "module",
88
},
99
})
@@ -33,6 +33,11 @@ tester.run("prefer-regexp-test", rule as any, {
3333
const a = pattern.exec(text)
3434
const b = text.match(pattern)
3535
`,
36+
`
37+
const text = 'something';
38+
const pattern = /thin[[g]]/v;
39+
if (pattern.test(text)) {}
40+
`,
3641
],
3742
invalid: [
3843
{
@@ -186,5 +191,37 @@ tester.run("prefer-regexp-test", rule as any, {
186191
"Use the `RegExp#test()` method instead of `String#match`, if you need a boolean.",
187192
],
188193
},
194+
{
195+
code: `
196+
const text = 'something';
197+
const pattern = /thin[[g]]/v;
198+
if (pattern.exec(text)) {}
199+
if (text.match(pattern)) {}
200+
`,
201+
output: `
202+
const text = 'something';
203+
const pattern = /thin[[g]]/v;
204+
if (pattern.test(text)) {}
205+
if (pattern.test(text)) {}
206+
`,
207+
errors: [
208+
{
209+
message:
210+
"Use the `RegExp#test()` method instead of `RegExp#exec`, if you need a boolean.",
211+
line: 4,
212+
column: 25,
213+
endLine: 4,
214+
endColumn: 29,
215+
},
216+
{
217+
message:
218+
"Use the `RegExp#test()` method instead of `String#match`, if you need a boolean.",
219+
line: 5,
220+
column: 17,
221+
endLine: 5,
222+
endColumn: 36,
223+
},
224+
],
225+
},
189226
],
190227
})

tests/lib/rules/prefer-result-array-groups.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import rule from "../../../lib/rules/prefer-result-array-groups"
44

55
const tester = new RuleTester({
66
parserOptions: {
7-
ecmaVersion: 2020,
7+
ecmaVersion: "latest",
88
sourceType: "module",
99
},
1010
})
@@ -51,6 +51,14 @@ tester.run("prefer-result-array-groups", rule as any, {
5151
// ...
5252
}
5353
`,
54+
`
55+
const regex = /reg[[exp]]/v
56+
let match
57+
while (match = regex.exec(foo)) {
58+
const match = match[0]
59+
// ...
60+
}
61+
`,
5462
// String.prototype.match()
5563
`
5664
const arr = "str".match(/regexp/)
@@ -231,6 +239,32 @@ tester.run("prefer-result-array-groups", rule as any, {
231239
},
232240
],
233241
},
242+
{
243+
code: `
244+
const regex = /a(?<foo>[[b]])c/v
245+
let match
246+
while (match = regex.exec(foo)) {
247+
const p1 = match[1]
248+
// ...
249+
}
250+
`,
251+
output: `
252+
const regex = /a(?<foo>[[b]])c/v
253+
let match
254+
while (match = regex.exec(foo)) {
255+
const p1 = match.groups.foo
256+
// ...
257+
}
258+
`,
259+
errors: [
260+
{
261+
message:
262+
"Unexpected indexed access for the named capturing group 'foo' from regexp result array.",
263+
line: 5,
264+
column: 28,
265+
},
266+
],
267+
},
234268
// String.prototype.match()
235269
{
236270
code: `

0 commit comments

Comments
 (0)