Skip to content

Commit

Permalink
Add support for v flag to regexp/no-misleading-capturing-group (#620
Browse files Browse the repository at this point in the history
)

* Add support for `v` flag to `regexp/no-misleading-capturing-group`

* Create itchy-trains-exist.md
  • Loading branch information
RunDevelopment authored Oct 1, 2023
1 parent 81f0153 commit fe34c05
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-trains-exist.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/no-misleading-capturing-group`
29 changes: 24 additions & 5 deletions lib/rules/no-misleading-capturing-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
isEmpty,
hasSomeDescendant,
Chars,
toCharSet,
followPaths,
toUnicodeSet,
} from "regexp-ast-analysis"
import { canSimplifyQuantifier } from "../utils/regexp-ast/simplify-quantifier"
import { fixSimplifyQuantifier } from "../utils/fix-simplify-quantifier"
Expand Down Expand Up @@ -161,10 +161,29 @@ function uncachedGetSingleRepeatedChar(
case "Character":
case "CharacterClass":
case "CharacterSet":
case "ExpressionCharacterClass":
// FIXME: TS Error
// @ts-expect-error -- FIXME
return toCharSet(element, flags)
case "ExpressionCharacterClass": {
const set = toUnicodeSet(element, flags)
if (set.accept.isEmpty) {
return set.chars
}

// this is pretty much the same as a list of alternatives, so we do the
// same thing as for Groups and Alternatives.
return set.wordSets
.map((wordSet): CharSet => {
let total: CharSet | undefined = undefined
for (const c of wordSet) {
if (total === undefined) {
total = c
} else {
total = total.intersect(c)
}
if (total.isEmpty) return total
}
return total ?? Chars.empty(flags)
})
.reduce((a, b) => a.union(b))
}

case "CapturingGroup":
case "Group":
Expand Down
8 changes: 7 additions & 1 deletion tests/lib/rules/no-misleading-capturing-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import rule from "../../../lib/rules/no-misleading-capturing-group"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: "latest",
sourceType: "module",
},
})
Expand Down Expand Up @@ -68,5 +68,11 @@ tester.run("no-misleading-capturing-group", rule as any, {
"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.",
],
},
{
code: String.raw`/^([a\q{abc}]*).+/v`,
errors: [
"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.",
],
},
],
})

0 comments on commit fe34c05

Please sign in to comment.