Skip to content

Add test cases for v flag #643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion tests/lib/rules/no-invisible-character.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import rule from "../../../lib/rules/no-invisible-character"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: "latest",
sourceType: "module",
},
})
Expand All @@ -22,6 +22,7 @@ tester.run("no-invisible-character", rule as any, {
"new RegExp(' ')",
"new RegExp('a')",
"new RegExp('[ ]')",
String.raw`/[\q{\t}]/v`,
],
invalid: [
{
Expand Down Expand Up @@ -104,5 +105,10 @@ tester.run("no-invisible-character", rule as any, {
"Unexpected invisible character. Use '\\u200b' instead.",
],
},
{
code: `/[\\q{\t}]/v`,
output: String.raw`/[\q{\t}]/v`,
errors: ["Unexpected invisible character. Use '\\t' instead."],
},
],
})
15 changes: 14 additions & 1 deletion tests/lib/rules/no-lazy-ends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import rule from "../../../lib/rules/no-lazy-ends"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: "latest",
sourceType: "module",
},
})
Expand All @@ -28,6 +28,8 @@ tester.run("no-lazy-ends", rule as any, {
sourceType: "script",
},
},

String.raw`/[\q{ab}]?/v.test(str)`,
],
invalid: [
{
Expand Down Expand Up @@ -200,5 +202,16 @@ tester.run("no-lazy-ends", rule as any, {
},
],
},
{
code: String.raw`/[\q{ab|}]??/v.test(str)`,
errors: [
{
message:
"The quantifier and the quantified element can be removed because the quantifier is lazy and has a minimum of 0.",
line: 1,
column: 2,
},
],
},
],
})
2 changes: 1 addition & 1 deletion tests/lib/rules/no-legacy-features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import rule from "../../../lib/rules/no-legacy-features"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: "latest",
sourceType: "module",
},
})
Expand Down
27 changes: 26 additions & 1 deletion tests/lib/rules/no-missing-g-flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import rule from "../../../lib/rules/no-missing-g-flag"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: "latest",
sourceType: "module",
},
})
Expand Down Expand Up @@ -52,6 +52,11 @@ tester.run("no-missing-g-flag", rule as any, {
const s = 'foo'
const ret = s.replaceAll(new RegExp('foo', unknown), 'bar')
`,
// ES2024
String.raw`
const s = 'foo'
const ret = s.replaceAll(/[\q{foo}]/gv, 'bar')
`,
],
invalid: [
{
Expand Down Expand Up @@ -233,5 +238,25 @@ tester.run("no-missing-g-flag", rule as any, {
},
],
},
{
// ES2024
code: String.raw`
const s = 'foo'
const ret = s.replaceAll(/[\q{foo}]/v, 'bar')
`,
output: String.raw`
const s = 'foo'
const ret = s.replaceAll(/[\q{foo}]/vg, 'bar')
`,
options: [{ strictTypes: false }],
errors: [
{
message:
"The pattern given to the argument of `String#replaceAll()` requires the `g` flag, but is missing it.",
line: 3,
column: 38,
},
],
},
],
})
14 changes: 13 additions & 1 deletion tests/lib/rules/no-obscure-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import rule from "../../../lib/rules/no-obscure-range"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: "latest",
sourceType: "module",
},
})
Expand All @@ -23,6 +23,7 @@ tester.run("no-obscure-range", rule as any, {
},
},
},
"/[[0-9]--[6-8]]/v",
],
invalid: [
{
Expand Down Expand Up @@ -125,5 +126,16 @@ tester.run("no-obscure-range", rule as any, {
},
],
},
{
code: String.raw`/[[ -\/]--+]/v`,
errors: [
{
message:
"Unexpected obscure character range. The characters of ' -\\/' (U+0020 - U+002f) are not obvious.",
line: 1,
column: 4,
},
],
},
],
})
4 changes: 2 additions & 2 deletions tests/lib/rules/no-octal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import rule from "../../../lib/rules/no-octal"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: "latest",
sourceType: "module",
},
})

tester.run("no-octal", rule as any, {
valid: ["/\\0/", "/[\\7]/", "/[\\1-\\4]/"],
valid: ["/\\0/", "/[\\7]/", "/[\\1-\\4]/", String.raw`/[\q{\0}]/v`],
invalid: [
{
code: "/\\07/",
Expand Down
14 changes: 13 additions & 1 deletion tests/lib/rules/no-optional-assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import rule from "../../../lib/rules/no-optional-assertion"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: "latest",
sourceType: "module",
},
})
Expand All @@ -15,6 +15,7 @@ tester.run("no-optional-assertion", rule as any, {
String.raw`/(?:a|(?:\b|a)+)?/`,
String.raw`/fo(?:o\b)/`,
String.raw`/fo(?:o\b){1}/`,
String.raw`/(?:(?=[\q{a}]))/v`,
],
invalid: [
{
Expand Down Expand Up @@ -89,5 +90,16 @@ tester.run("no-optional-assertion", rule as any, {
},
],
},
{
code: String.raw`/(?:(?=[\q{a}]))?/v`,
errors: [
{
message:
"This assertion effectively optional and does not change the pattern. Either remove the assertion or change the parent quantifier '?'.",
line: 1,
column: 5,
},
],
},
],
})
14 changes: 13 additions & 1 deletion tests/lib/rules/no-potentially-useless-backreference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import rule from "../../../lib/rules/no-potentially-useless-backreference"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: "latest",
sourceType: "module",
},
})
Expand All @@ -14,6 +14,7 @@ tester.run("no-potentially-useless-backreference", rule as any, {
String.raw`/(a*)(?:a|\1)/`,
String.raw`/(a)+\1/`,
String.raw`/(?=(a))\1/`,
String.raw`/([\q{a}])\1/v`,

// done by regexp/no-useless-backreference
String.raw`/(a+)b|\1/`,
Expand Down Expand Up @@ -84,5 +85,16 @@ tester.run("no-potentially-useless-backreference", rule as any, {
},
],
},
{
code: String.raw`/(?:([\q{a}])|b)\1/v`,
errors: [
{
message:
"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.",
line: 1,
column: 17,
},
],
},
],
})
4 changes: 2 additions & 2 deletions tests/lib/rules/no-standalone-backslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import rule from "../../../lib/rules/no-standalone-backslash"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: "latest",
sourceType: "module",
},
})

tester.run("no-standalone-backslash", rule as any, {
valid: [String.raw`/\cX/`],
valid: [String.raw`/\cX/`, String.raw`/[[\cA-\cZ]--\cX]/v`],
invalid: [
{
code: String.raw`/\c/`,
Expand Down
10 changes: 9 additions & 1 deletion tests/lib/rules/no-super-linear-backtracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import rule from "../../../lib/rules/no-super-linear-backtracking"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: "latest",
sourceType: "module",
},
})
Expand All @@ -13,6 +13,7 @@ tester.run("no-super-linear-backtracking", rule as any, {
String.raw`/regexp/`,
String.raw`/a+b+a+b+/`,
String.raw`/\w+\b[\w-]+/`,
String.raw`/[\q{ab}]*[\q{ab}]*$/v`, // Limitation of scslre
],
invalid: [
// self
Expand Down Expand Up @@ -53,5 +54,12 @@ tester.run("no-super-linear-backtracking", rule as any, {
"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.",
],
},
{
code: String.raw`/[\q{a}]*b?[\q{a}]+$/v`,
output: String.raw`/(?:[\q{a}]+(?:b[\q{a}]+)?|b[\q{a}]+)$/v`,
errors: [
"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.",
],
},
],
})
9 changes: 8 additions & 1 deletion tests/lib/rules/no-super-linear-move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import rule from "../../../lib/rules/no-super-linear-move"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: "latest",
sourceType: "module",
},
})
Expand All @@ -26,6 +26,7 @@ tester.run("no-super-linear-move", rule as any, {
},
{ code: String.raw`/a*b/.source`, options: [{ ignorePartial: true }] },
{ code: String.raw`/a*b/y`, options: [{ ignoreSticky: true }] },
String.raw`/[\q{abc}]+/v`,
],
invalid: [
{
Expand Down Expand Up @@ -80,5 +81,11 @@ tester.run("no-super-linear-move", rule as any, {
"Any attack string /a+/ plus some rejecting suffix will cause quadratic runtime because of this quantifier.",
],
},
{
code: String.raw`/[\q{abc}]+a/v`,
errors: [
"Any attack string /(?:abc)+/ plus some rejecting suffix will cause quadratic runtime because of this quantifier.",
],
},
],
})
5 changes: 4 additions & 1 deletion tests/lib/rules/no-trivially-nested-assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import rule from "../../../lib/rules/no-trivially-nested-assertion"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: "latest",
sourceType: "module",
},
})
Expand All @@ -22,6 +22,9 @@ tester.run("no-trivially-nested-assertion", rule as any, {
// guaranteed to be reset, so we can't transform them into one
// non-negated lookaround
`/(?!(?!(a)))/`,

// ES2024
String.raw`/(?=[\q{$}])/v`,
],
invalid: [
{
Expand Down
17 changes: 15 additions & 2 deletions tests/lib/rules/no-trivially-nested-quantifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ import rule from "../../../lib/rules/no-trivially-nested-quantifier"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: "latest",
sourceType: "module",
},
})

tester.run("no-trivially-nested-quantifier", rule as any, {
valid: [`/(a?)+/`, `/(?:a{2})+/`, `/(?:a{3,4})+/`, `/(?:a+?)+/`],
valid: [
`/(a?)+/`,
`/(?:a{2})+/`,
`/(?:a{3,4})+/`,
`/(?:a+?)+/`,
String.raw`/(?:[\q{a}])+/v`,
],
invalid: [
{
code: String.raw`/(?:a?)+/`,
Expand Down Expand Up @@ -110,5 +116,12 @@ tester.run("no-trivially-nested-quantifier", rule as any, {
{ message: "This nested quantifier can be simplified to '?'." },
],
},
{
code: String.raw`/(?:[\q{a}]+)+/v`,
output: String.raw`/[\q{a}]+/v`,
errors: [
"These two quantifiers are trivially nested and can be replaced with '+'.",
],
},
],
})
Loading