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/optimal-quantifier-concatenation #618

Merged
merged 2 commits into from
Oct 1, 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/beige-suns-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-regexp": minor
---

Add support for `v` flag to `regexp/optimal-quantifier-concatenation`
12 changes: 6 additions & 6 deletions lib/rules/optimal-quantifier-concatenation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import type { Ancestor, ReadonlyFlags } from "regexp-ast-analysis"
import {
Chars,
hasSomeDescendant,
toCharSet,
getConsumedChars,
toUnicodeSet,
} from "regexp-ast-analysis"
import { getParser } from "../utils/regexp-ast"
import type { CharSet } from "refa"
Expand Down Expand Up @@ -76,13 +76,13 @@ function getSingleConsumedChar(
case "Character":
case "CharacterSet":
case "CharacterClass":
case "ExpressionCharacterClass":
case "ExpressionCharacterClass": {
const set = toUnicodeSet(element, flags)
return {
// FIXME: TS Error
// @ts-expect-error -- FIXME
char: toCharSet(element, flags),
complete: true,
char: set.chars,
complete: set.accept.isEmpty,
}
}

case "Group":
case "CapturingGroup": {
Expand Down
23 changes: 22 additions & 1 deletion tests/lib/rules/optimal-quantifier-concatenation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import rule from "../../../lib/rules/optimal-quantifier-concatenation"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: "latest",
sourceType: "module",
},
})
Expand Down Expand Up @@ -291,5 +291,26 @@ tester.run("optimal-quantifier-concatenation", rule as any, {
"'\\d*' can be removed because it is already included by '\\d+'. This cannot be fixed automatically because it involves a capturing group.",
],
},
{
code: String.raw`/\d+(\d*)/v`,
output: null,
errors: [
"'\\d*' can be removed because it is already included by '\\d+'. This cannot be fixed automatically because it involves a capturing group.",
],
},
{
code: String.raw`/a+[a\q{}]+/v`,
output: String.raw`/a+[a\q{}]/v`,
errors: [
"'[a\\q{}]+' can be replaced with '[a\\q{}]' because of 'a+'.",
],
},
{
code: String.raw`/[ab]*[\q{a|bb}]+/v`,
output: String.raw`/[ab]*[\q{a|bb}]/v`,
errors: [
"'[\\q{a|bb}]+' can be replaced with '[\\q{a|bb}]' because of '[ab]*'.",
],
},
],
})