Skip to content

Commit 19f5830

Browse files
Add support for v flag to regexp/prefer-predefined-assertion (#611)
* Add support for `v` flag to `regexp/prefer-predefined-assertion` * Create young-hairs-bow.md
1 parent fd683c5 commit 19f5830

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

.changeset/young-hairs-bow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-regexp": patch
3+
---
4+
5+
Add support for `v` flag to `regexp/prefer-predefined-assertion`

lib/rules/prefer-predefined-assertion.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
22
import type {
33
CharacterClass,
44
CharacterSet,
5+
ExpressionCharacterClass,
56
LookaroundAssertion,
67
} from "@eslint-community/regexpp/ast"
78
import type { RegExpContext } from "../utils"
@@ -11,7 +12,7 @@ import {
1112
getFirstCharAfter,
1213
getMatchingDirectionFromAssertionKind,
1314
invertMatchingDirection,
14-
toCharSet,
15+
toUnicodeSet,
1516
} from "regexp-ast-analysis"
1617

1718
/**
@@ -20,14 +21,15 @@ import {
2021
*/
2122
function getCharacters(
2223
lookaround: LookaroundAssertion,
23-
): CharacterSet | CharacterClass | null {
24+
): CharacterSet | CharacterClass | ExpressionCharacterClass | null {
2425
if (lookaround.alternatives.length === 1) {
2526
const alt = lookaround.alternatives[0]
2627
if (alt.elements.length === 1) {
2728
const first = alt.elements[0]
2829
if (
2930
first.type === "CharacterSet" ||
30-
first.type === "CharacterClass"
31+
first.type === "CharacterClass" ||
32+
first.type === "ExpressionCharacterClass"
3133
) {
3234
return first
3335
}
@@ -200,9 +202,12 @@ export default createRule("prefer-predefined-assertion", {
200202
}
201203
}
202204

203-
// FIXME: TS Error
204-
// @ts-expect-error -- FIXME
205-
const charSet = toCharSet(chars, flags)
205+
const set = toUnicodeSet(chars, flags)
206+
if (!set.accept.isEmpty) {
207+
// the set contains strings, so it can't be replaced with a predefined assertion
208+
return
209+
}
210+
const charSet = set.chars
206211
if (charSet.isAll) {
207212
replaceEdgeAssertion(aNode, false)
208213
} else if (charSet.equals(word)) {

tests/lib/rules/prefer-predefined-assertion.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import rule from "../../../lib/rules/prefer-predefined-assertion"
33

44
const tester = new RuleTester({
55
parserOptions: {
6-
ecmaVersion: 2020,
6+
ecmaVersion: "latest",
77
sourceType: "module",
88
},
99
})
1010

1111
tester.run("prefer-predefined-assertion", rule as any, {
12-
valid: [String.raw`/a(?=\W)/`],
12+
valid: [String.raw`/a(?=\W)/`, String.raw`/a(?=\W)/v`],
1313
invalid: [
1414
{
1515
code: String.raw`/a(?=\w)/`,
@@ -68,6 +68,11 @@ tester.run("prefer-predefined-assertion", rule as any, {
6868
"This lookaround assertion can be replaced with a negated word boundary assertion ('\\B').",
6969
],
7070
},
71+
{
72+
code: String.raw`/.(?<!\W)a/v`,
73+
output: String.raw`/.\Ba/v`,
74+
errors: 1,
75+
},
7176

7277
{
7378
code: String.raw`/a+(?!\w)(?:\s|bc+)+/`,

0 commit comments

Comments
 (0)