From f017051e434cdce83719fb12a53a67a319e554be Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Tue, 26 Sep 2023 13:21:30 +0200 Subject: [PATCH 1/2] Add support for `v` flag to `regexp/optimal-quantifier-concatenation` --- lib/rules/optimal-quantifier-concatenation.ts | 12 +++++----- .../rules/optimal-quantifier-concatenation.ts | 23 ++++++++++++++++++- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/rules/optimal-quantifier-concatenation.ts b/lib/rules/optimal-quantifier-concatenation.ts index 0288b8bfe..a1b8b6dfd 100644 --- a/lib/rules/optimal-quantifier-concatenation.ts +++ b/lib/rules/optimal-quantifier-concatenation.ts @@ -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" @@ -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": { diff --git a/tests/lib/rules/optimal-quantifier-concatenation.ts b/tests/lib/rules/optimal-quantifier-concatenation.ts index e6a181d34..aa75171b2 100644 --- a/tests/lib/rules/optimal-quantifier-concatenation.ts +++ b/tests/lib/rules/optimal-quantifier-concatenation.ts @@ -3,7 +3,7 @@ import rule from "../../../lib/rules/optimal-quantifier-concatenation" const tester = new RuleTester({ parserOptions: { - ecmaVersion: 2020, + ecmaVersion: "latest", sourceType: "module", }, }) @@ -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]*'.", + ], + }, ], }) From cc406147cf4e6a572675d843f66046e0628c7cc0 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Tue, 26 Sep 2023 13:22:23 +0200 Subject: [PATCH 2/2] Create beige-suns-clap.md --- .changeset/beige-suns-clap.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/beige-suns-clap.md diff --git a/.changeset/beige-suns-clap.md b/.changeset/beige-suns-clap.md new file mode 100644 index 000000000..e914aac99 --- /dev/null +++ b/.changeset/beige-suns-clap.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-regexp": minor +--- + +Add support for `v` flag to `regexp/optimal-quantifier-concatenation`