Skip to content
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

Add support for v flag to regexp/no-contradiction-with-assertion #606

Merged
merged 2 commits into from
Sep 14, 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
5 changes: 5 additions & 0 deletions .changeset/neat-rats-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-regexp": patch
---

Add support for `v` flag to `regexp/no-contradiction-with-assertion`
10 changes: 6 additions & 4 deletions lib/rules/no-contradiction-with-assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ function* getNextElements(

if (
parent.type === "CharacterClass" ||
parent.type === "CharacterClassRange"
parent.type === "CharacterClassRange" ||
parent.type === "ExpressionCharacterClass" ||
parent.type === "ClassIntersection" ||
parent.type === "ClassSubtraction" ||
parent.type === "StringAlternative"
) {
return
}
Expand All @@ -103,10 +107,8 @@ function* getNextElements(
}
}

// FIXME: TS Error
// @ts-expect-error -- FIXME
const elements = parent.elements
const index = elements.indexOf(element) as number
const index = elements.indexOf(element)
const inc = dir === "ltr" ? 1 : -1
for (let i = index + inc; i >= 0 && i < elements.length; i += inc) {
const e = elements[i]
Expand Down
13 changes: 12 additions & 1 deletion tests/lib/rules/no-contradiction-with-assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import rule from "../../../lib/rules/no-contradiction-with-assertion"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: "latest",
sourceType: "module",
},
})
Expand All @@ -15,10 +15,12 @@ tester.run("no-contradiction-with-assertion", rule as any, {
String.raw`/(?!)a/`,
String.raw`/(?=)a/`,
String.raw`/$a/`,
String.raw`/$a/v`,

// Other valid regexes
String.raw`/(^|[\s\S])\bfoo/`,
String.raw`/(?:aa|a\b)-?a/`,
String.raw`/(?:aa|a\b)-?a/v`,
],
invalid: [
{
Expand Down Expand Up @@ -48,6 +50,15 @@ tester.run("no-contradiction-with-assertion", rule as any, {
},
],
},
{
code: String.raw`/a\b[a\q{foo|bar}]*-/v`,
errors: [
{
messageId: "cannotEnterQuantifier",
suggestions: [{ output: String.raw`/a\b-/v` }],
},
],
},

{
code: String.raw`/(^[\t ]*)#(?:comments-start|cs)[\s\S]*?^[ \t]*#(?:comments-end|ce)/m`,
Expand Down
Loading