Skip to content

Commit ee3186d

Browse files
authored
Improve regexp/require-unicode-regexp rule to allow patterns with v flag (#586)
1 parent 6732d59 commit ee3186d

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

.changeset/loud-flowers-search.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+
Improve `regexp/require-unicode-regexp` rule to allow patterns with v flag

lib/rules/require-unicode-regexp.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import type { ReadonlyFlags } from "regexp-ast-analysis"
1515
import {
1616
hasSomeDescendant,
1717
toCache,
18-
toCharSet,
1918
getFirstCharAfter,
19+
toUnicodeSet,
2020
} from "regexp-ast-analysis"
2121

2222
const UTF16_MAX = 0xffff
@@ -146,22 +146,18 @@ function isCompatibleCharLike(
146146
flags: ReadonlyFlags,
147147
uFlags: ReadonlyFlags,
148148
): boolean {
149-
// FIXME: TS Error
150-
// @ts-expect-error -- FIXME
151-
const cs = toCharSet(char, flags)
149+
const cs = toUnicodeSet(char, flags)
152150
if (!cs.isDisjointWith(SURROGATES)) {
153151
// If the character (class/set) contains high or low
154152
// surrogates, then we won't be able to guarantee that the
155153
// Unicode pattern will behave the same way.
156154
return false
157155
}
158156

159-
// FIXME: TS Error
160-
// @ts-expect-error -- FIXME
161-
const uCs = toCharSet(char, uFlags)
157+
const uCs = toUnicodeSet(char, uFlags)
162158

163159
// Compare the ranges.
164-
return rangeEqual(cs.ranges, uCs.ranges)
160+
return rangeEqual(cs.chars.ranges, uCs.chars.ranges)
165161
}
166162

167163
/**
@@ -203,23 +199,19 @@ function isCompatibleQuantifier(
203199
return undefined
204200
}
205201

206-
// FIXME: TS Error
207-
// @ts-expect-error -- FIXME
208-
const cs = toCharSet(q.element, flags)
202+
const cs = toUnicodeSet(q.element, flags)
209203
if (!cs.isSupersetOf(SURROGATES)) {
210204
// failed condition 1
211205
return false
212206
}
213207

214-
// FIXME: TS Error
215-
// @ts-expect-error -- FIXME
216-
const uCs = toCharSet(q.element, uFlags)
208+
const uCs = toUnicodeSet(q.element, uFlags)
217209
if (!uCs.isSupersetOf(SURROGATES) || !uCs.isSupersetOf(ASTRAL)) {
218210
// failed condition 2
219211
return false
220212
}
221213

222-
if (!rangeEqual(cs.ranges, uCs.without(ASTRAL).ranges)) {
214+
if (!rangeEqual(cs.chars.ranges, uCs.without(ASTRAL).chars.ranges)) {
223215
// failed condition 3
224216
return false
225217
}
@@ -353,7 +345,7 @@ export default createRule("require-unicode-regexp", {
353345
return {}
354346
}
355347

356-
if (!flags.unicode) {
348+
if (!flags.unicode && !flags.unicodeSets) {
357349
context.report({
358350
node,
359351
loc: getFlagsLocation(),

tests/lib/rules/require-unicode-regexp.ts

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

44
const tester = new RuleTester({
55
parserOptions: {
6-
ecmaVersion: 2020,
6+
ecmaVersion: "latest",
77
sourceType: "module",
88
},
99
})
@@ -28,6 +28,8 @@ tester.run("require-unicode-regexp", rule as any, {
2828
String.raw`const flags = 'u'; new globalThis.RegExp('', flags)`,
2929
String.raw`const flags = 'g'; new globalThis.RegExp('', flags + 'u')`,
3030
String.raw`const flags = 'gimu'; new globalThis.RegExp('foo', flags[3])`,
31+
String.raw`/foo/v`,
32+
String.raw`new RegExp('foo', 'v')`,
3133
],
3234
invalid: [
3335
{

0 commit comments

Comments
 (0)