Skip to content

Commit ae1897a

Browse files
authored
Add test cases for v flag (#643)
1 parent f8b871a commit ae1897a

14 files changed

+144
-17
lines changed

tests/lib/rules/no-invisible-character.ts

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

44
const tester = new RuleTester({
55
parserOptions: {
6-
ecmaVersion: 2020,
6+
ecmaVersion: "latest",
77
sourceType: "module",
88
},
99
})
@@ -22,6 +22,7 @@ tester.run("no-invisible-character", rule as any, {
2222
"new RegExp(' ')",
2323
"new RegExp('a')",
2424
"new RegExp('[ ]')",
25+
String.raw`/[\q{\t}]/v`,
2526
],
2627
invalid: [
2728
{
@@ -104,5 +105,10 @@ tester.run("no-invisible-character", rule as any, {
104105
"Unexpected invisible character. Use '\\u200b' instead.",
105106
],
106107
},
108+
{
109+
code: `/[\\q{\t}]/v`,
110+
output: String.raw`/[\q{\t}]/v`,
111+
errors: ["Unexpected invisible character. Use '\\t' instead."],
112+
},
107113
],
108114
})

tests/lib/rules/no-lazy-ends.ts

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

44
const tester = new RuleTester({
55
parserOptions: {
6-
ecmaVersion: 2020,
6+
ecmaVersion: "latest",
77
sourceType: "module",
88
},
99
})
@@ -28,6 +28,8 @@ tester.run("no-lazy-ends", rule as any, {
2828
sourceType: "script",
2929
},
3030
},
31+
32+
String.raw`/[\q{ab}]?/v.test(str)`,
3133
],
3234
invalid: [
3335
{
@@ -200,5 +202,16 @@ tester.run("no-lazy-ends", rule as any, {
200202
},
201203
],
202204
},
205+
{
206+
code: String.raw`/[\q{ab|}]??/v.test(str)`,
207+
errors: [
208+
{
209+
message:
210+
"The quantifier and the quantified element can be removed because the quantifier is lazy and has a minimum of 0.",
211+
line: 1,
212+
column: 2,
213+
},
214+
],
215+
},
203216
],
204217
})

tests/lib/rules/no-legacy-features.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import rule from "../../../lib/rules/no-legacy-features"
33

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

tests/lib/rules/no-missing-g-flag.ts

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

44
const tester = new RuleTester({
55
parserOptions: {
6-
ecmaVersion: 2020,
6+
ecmaVersion: "latest",
77
sourceType: "module",
88
},
99
})
@@ -52,6 +52,11 @@ tester.run("no-missing-g-flag", rule as any, {
5252
const s = 'foo'
5353
const ret = s.replaceAll(new RegExp('foo', unknown), 'bar')
5454
`,
55+
// ES2024
56+
String.raw`
57+
const s = 'foo'
58+
const ret = s.replaceAll(/[\q{foo}]/gv, 'bar')
59+
`,
5560
],
5661
invalid: [
5762
{
@@ -233,5 +238,25 @@ tester.run("no-missing-g-flag", rule as any, {
233238
},
234239
],
235240
},
241+
{
242+
// ES2024
243+
code: String.raw`
244+
const s = 'foo'
245+
const ret = s.replaceAll(/[\q{foo}]/v, 'bar')
246+
`,
247+
output: String.raw`
248+
const s = 'foo'
249+
const ret = s.replaceAll(/[\q{foo}]/vg, 'bar')
250+
`,
251+
options: [{ strictTypes: false }],
252+
errors: [
253+
{
254+
message:
255+
"The pattern given to the argument of `String#replaceAll()` requires the `g` flag, but is missing it.",
256+
line: 3,
257+
column: 38,
258+
},
259+
],
260+
},
236261
],
237262
})

tests/lib/rules/no-obscure-range.ts

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

44
const tester = new RuleTester({
55
parserOptions: {
6-
ecmaVersion: 2020,
6+
ecmaVersion: "latest",
77
sourceType: "module",
88
},
99
})
@@ -23,6 +23,7 @@ tester.run("no-obscure-range", rule as any, {
2323
},
2424
},
2525
},
26+
"/[[0-9]--[6-8]]/v",
2627
],
2728
invalid: [
2829
{
@@ -125,5 +126,16 @@ tester.run("no-obscure-range", rule as any, {
125126
},
126127
],
127128
},
129+
{
130+
code: String.raw`/[[ -\/]--+]/v`,
131+
errors: [
132+
{
133+
message:
134+
"Unexpected obscure character range. The characters of ' -\\/' (U+0020 - U+002f) are not obvious.",
135+
line: 1,
136+
column: 4,
137+
},
138+
],
139+
},
128140
],
129141
})

tests/lib/rules/no-octal.ts

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

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

1111
tester.run("no-octal", rule as any, {
12-
valid: ["/\\0/", "/[\\7]/", "/[\\1-\\4]/"],
12+
valid: ["/\\0/", "/[\\7]/", "/[\\1-\\4]/", String.raw`/[\q{\0}]/v`],
1313
invalid: [
1414
{
1515
code: "/\\07/",

tests/lib/rules/no-optional-assertion.ts

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

44
const tester = new RuleTester({
55
parserOptions: {
6-
ecmaVersion: 2020,
6+
ecmaVersion: "latest",
77
sourceType: "module",
88
},
99
})
@@ -15,6 +15,7 @@ tester.run("no-optional-assertion", rule as any, {
1515
String.raw`/(?:a|(?:\b|a)+)?/`,
1616
String.raw`/fo(?:o\b)/`,
1717
String.raw`/fo(?:o\b){1}/`,
18+
String.raw`/(?:(?=[\q{a}]))/v`,
1819
],
1920
invalid: [
2021
{
@@ -89,5 +90,16 @@ tester.run("no-optional-assertion", rule as any, {
8990
},
9091
],
9192
},
93+
{
94+
code: String.raw`/(?:(?=[\q{a}]))?/v`,
95+
errors: [
96+
{
97+
message:
98+
"This assertion effectively optional and does not change the pattern. Either remove the assertion or change the parent quantifier '?'.",
99+
line: 1,
100+
column: 5,
101+
},
102+
],
103+
},
92104
],
93105
})

tests/lib/rules/no-potentially-useless-backreference.ts

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

44
const tester = new RuleTester({
55
parserOptions: {
6-
ecmaVersion: 2020,
6+
ecmaVersion: "latest",
77
sourceType: "module",
88
},
99
})
@@ -14,6 +14,7 @@ tester.run("no-potentially-useless-backreference", rule as any, {
1414
String.raw`/(a*)(?:a|\1)/`,
1515
String.raw`/(a)+\1/`,
1616
String.raw`/(?=(a))\1/`,
17+
String.raw`/([\q{a}])\1/v`,
1718

1819
// done by regexp/no-useless-backreference
1920
String.raw`/(a+)b|\1/`,
@@ -84,5 +85,16 @@ tester.run("no-potentially-useless-backreference", rule as any, {
8485
},
8586
],
8687
},
88+
{
89+
code: String.raw`/(?:([\q{a}])|b)\1/v`,
90+
errors: [
91+
{
92+
message:
93+
"Some paths leading to the backreference do not go through the referenced capturing group or the captured text might be reset before reaching the backreference.",
94+
line: 1,
95+
column: 17,
96+
},
97+
],
98+
},
8799
],
88100
})

tests/lib/rules/no-standalone-backslash.ts

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

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

1111
tester.run("no-standalone-backslash", rule as any, {
12-
valid: [String.raw`/\cX/`],
12+
valid: [String.raw`/\cX/`, String.raw`/[[\cA-\cZ]--\cX]/v`],
1313
invalid: [
1414
{
1515
code: String.raw`/\c/`,

tests/lib/rules/no-super-linear-backtracking.ts

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

44
const tester = new RuleTester({
55
parserOptions: {
6-
ecmaVersion: 2020,
6+
ecmaVersion: "latest",
77
sourceType: "module",
88
},
99
})
@@ -13,6 +13,7 @@ tester.run("no-super-linear-backtracking", rule as any, {
1313
String.raw`/regexp/`,
1414
String.raw`/a+b+a+b+/`,
1515
String.raw`/\w+\b[\w-]+/`,
16+
String.raw`/[\q{ab}]*[\q{ab}]*$/v`, // Limitation of scslre
1617
],
1718
invalid: [
1819
// self
@@ -53,5 +54,12 @@ tester.run("no-super-linear-backtracking", rule as any, {
5354
"The quantifier '\\w+' can exchange characters with '\\w+'. Using any string accepted by /b+/, this can be exploited to cause at least polynomial backtracking. This might cause exponential backtracking.",
5455
],
5556
},
57+
{
58+
code: String.raw`/[\q{a}]*b?[\q{a}]+$/v`,
59+
output: String.raw`/(?:[\q{a}]+(?:b[\q{a}]+)?|b[\q{a}]+)$/v`,
60+
errors: [
61+
"The quantifier '[\\q{a}]*' can exchange characters with '[\\q{a}]+'. Using any string accepted by /a+/, this can be exploited to cause at least polynomial backtracking. This might cause exponential backtracking.",
62+
],
63+
},
5664
],
5765
})

0 commit comments

Comments
 (0)