Skip to content

Commit fe34c05

Browse files
Add support for v flag to regexp/no-misleading-capturing-group (#620)
* Add support for `v` flag to `regexp/no-misleading-capturing-group` * Create itchy-trains-exist.md
1 parent 81f0153 commit fe34c05

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

.changeset/itchy-trains-exist.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-regexp": minor
3+
---
4+
5+
Add support for `v` flag to `regexp/no-misleading-capturing-group`

lib/rules/no-misleading-capturing-group.ts

+24-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import {
1818
isEmpty,
1919
hasSomeDescendant,
2020
Chars,
21-
toCharSet,
2221
followPaths,
22+
toUnicodeSet,
2323
} from "regexp-ast-analysis"
2424
import { canSimplifyQuantifier } from "../utils/regexp-ast/simplify-quantifier"
2525
import { fixSimplifyQuantifier } from "../utils/fix-simplify-quantifier"
@@ -161,10 +161,29 @@ function uncachedGetSingleRepeatedChar(
161161
case "Character":
162162
case "CharacterClass":
163163
case "CharacterSet":
164-
case "ExpressionCharacterClass":
165-
// FIXME: TS Error
166-
// @ts-expect-error -- FIXME
167-
return toCharSet(element, flags)
164+
case "ExpressionCharacterClass": {
165+
const set = toUnicodeSet(element, flags)
166+
if (set.accept.isEmpty) {
167+
return set.chars
168+
}
169+
170+
// this is pretty much the same as a list of alternatives, so we do the
171+
// same thing as for Groups and Alternatives.
172+
return set.wordSets
173+
.map((wordSet): CharSet => {
174+
let total: CharSet | undefined = undefined
175+
for (const c of wordSet) {
176+
if (total === undefined) {
177+
total = c
178+
} else {
179+
total = total.intersect(c)
180+
}
181+
if (total.isEmpty) return total
182+
}
183+
return total ?? Chars.empty(flags)
184+
})
185+
.reduce((a, b) => a.union(b))
186+
}
168187

169188
case "CapturingGroup":
170189
case "Group":

tests/lib/rules/no-misleading-capturing-group.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import rule from "../../../lib/rules/no-misleading-capturing-group"
33

44
const tester = new RuleTester({
55
parserOptions: {
6-
ecmaVersion: 2020,
6+
ecmaVersion: "latest",
77
sourceType: "module",
88
},
99
})
@@ -68,5 +68,11 @@ tester.run("no-misleading-capturing-group", rule as any, {
6868
"The quantifier '~?' can exchange characters (~) with '[\\s\\S]+'. This makes the capturing group misleading, because the quantifier will capture fewer characters than its pattern suggests.",
6969
],
7070
},
71+
{
72+
code: String.raw`/^([a\q{abc}]*).+/v`,
73+
errors: [
74+
"The quantifier '[a\\q{abc}]*' can exchange characters (a) with '.+'. This makes the capturing group misleading, because the quantifier will capture fewer characters than its pattern suggests.",
75+
],
76+
},
7177
],
7278
})

0 commit comments

Comments
 (0)