Skip to content

Commit fb75351

Browse files
authored
Add support for v flag to regexp/negation rule (#560)
1 parent 426f8af commit fb75351

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

.changeset/heavy-rings-reply.md

Lines changed: 5 additions & 0 deletions
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/negation` rule

lib/rules/negation.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { toCharSet } from "regexp-ast-analysis"
1+
import { toCharSet, toUnicodeSet } from "regexp-ast-analysis"
22
import type {
33
EscapeCharacterSet,
44
UnicodePropertyCharacterSet,
@@ -39,8 +39,18 @@ export default createRule("negation", {
3939
if (element.type !== "CharacterSet") {
4040
return
4141
}
42+
if (element.kind === "property" && element.strings) {
43+
// Unicode property escape with property of strings.
44+
// Actually the pattern passing through this branch is an invalid pattern,
45+
// but it has to be checked because of the type guards.
46+
return
47+
}
4248

43-
if (flags.ignoreCase && element.kind === "property") {
49+
if (
50+
flags.ignoreCase &&
51+
!flags.unicodeSets &&
52+
element.kind === "property"
53+
) {
4454
// The ignore case canonicalization affects negated
4555
// Unicode property escapes in a weird way. In short,
4656
// /\p{Foo}/i is not the same as /[^\P{Foo}]/i if
@@ -49,14 +59,9 @@ export default createRule("negation", {
4959
// Note: This only affects Unicode property escapes.
5060
// All other character sets are either case-invariant
5161
// (/./, /\s/, /\d/) or inconsistent (/\w/).
52-
53-
// FIXME: TS Error
54-
// @ts-expect-error -- FIXME
55-
const ccSet = toCharSet(ccNode, flags)
62+
const ccSet = toUnicodeSet(ccNode, flags)
5663

5764
const negatedElementSet = toCharSet(
58-
// FIXME: TS Error
59-
// @ts-expect-error -- FIXME
6065
{
6166
...element,
6267
negate: !element.negate,

tests/lib/rules/negation.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import rule from "../../../lib/rules/negation"
33

44
const tester = new RuleTester({
55
parserOptions: {
6-
ecmaVersion: 2020,
6+
ecmaVersion: "latest",
77
sourceType: "module",
88
},
99
})
@@ -14,6 +14,8 @@ tester.run("negation", rule as any, {
1414
String.raw`/[^\d\s]/`,
1515
String.raw`/[^\p{ASCII}]/iu`,
1616
String.raw`/[^\P{Ll}]/iu`,
17+
String.raw`/[\p{Basic_Emoji}]/v`,
18+
String.raw`/[^\P{Lowercase_Letter}]/iu`,
1719
],
1820
invalid: [
1921
{
@@ -140,5 +142,12 @@ tester.run("negation", rule as any, {
140142
output: null,
141143
errors: ["Unexpected negated character class. Use '\\W' instead."],
142144
},
145+
{
146+
code: String.raw`/[^\P{Lowercase_Letter}]/iv`,
147+
output: String.raw`/\p{Lowercase_Letter}/iv`,
148+
errors: [
149+
"Unexpected negated character class. Use '\\p{Lowercase_Letter}' instead.",
150+
],
151+
},
143152
],
144153
})

0 commit comments

Comments
 (0)