Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/loud-flowers-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-regexp": minor
---

Improve `regexp/require-unicode-regexp` rule to allow patterns with v flag
24 changes: 8 additions & 16 deletions lib/rules/require-unicode-regexp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import type { ReadonlyFlags } from "regexp-ast-analysis"
import {
hasSomeDescendant,
toCache,
toCharSet,
getFirstCharAfter,
toUnicodeSet,
} from "regexp-ast-analysis"

const UTF16_MAX = 0xffff
Expand Down Expand Up @@ -146,22 +146,18 @@ function isCompatibleCharLike(
flags: ReadonlyFlags,
uFlags: ReadonlyFlags,
): boolean {
// FIXME: TS Error
// @ts-expect-error -- FIXME
const cs = toCharSet(char, flags)
const cs = toUnicodeSet(char, flags)
if (!cs.isDisjointWith(SURROGATES)) {
// If the character (class/set) contains high or low
// surrogates, then we won't be able to guarantee that the
// Unicode pattern will behave the same way.
return false
}

// FIXME: TS Error
// @ts-expect-error -- FIXME
const uCs = toCharSet(char, uFlags)
const uCs = toUnicodeSet(char, uFlags)

// Compare the ranges.
return rangeEqual(cs.ranges, uCs.ranges)
return rangeEqual(cs.chars.ranges, uCs.chars.ranges)
}

/**
Expand Down Expand Up @@ -203,23 +199,19 @@ function isCompatibleQuantifier(
return undefined
}

// FIXME: TS Error
// @ts-expect-error -- FIXME
const cs = toCharSet(q.element, flags)
const cs = toUnicodeSet(q.element, flags)
if (!cs.isSupersetOf(SURROGATES)) {
// failed condition 1
return false
}

// FIXME: TS Error
// @ts-expect-error -- FIXME
const uCs = toCharSet(q.element, uFlags)
const uCs = toUnicodeSet(q.element, uFlags)
if (!uCs.isSupersetOf(SURROGATES) || !uCs.isSupersetOf(ASTRAL)) {
// failed condition 2
return false
}

if (!rangeEqual(cs.ranges, uCs.without(ASTRAL).ranges)) {
if (!rangeEqual(cs.chars.ranges, uCs.without(ASTRAL).chars.ranges)) {
// failed condition 3
return false
}
Expand Down Expand Up @@ -353,7 +345,7 @@ export default createRule("require-unicode-regexp", {
return {}
}

if (!flags.unicode) {
if (!flags.unicode && !flags.unicodeSets) {
context.report({
node,
loc: getFlagsLocation(),
Expand Down
4 changes: 3 additions & 1 deletion tests/lib/rules/require-unicode-regexp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import rule from "../../../lib/rules/require-unicode-regexp"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: "latest",
sourceType: "module",
},
})
Expand All @@ -28,6 +28,8 @@ tester.run("require-unicode-regexp", rule as any, {
String.raw`const flags = 'u'; new globalThis.RegExp('', flags)`,
String.raw`const flags = 'g'; new globalThis.RegExp('', flags + 'u')`,
String.raw`const flags = 'gimu'; new globalThis.RegExp('foo', flags[3])`,
String.raw`/foo/v`,
String.raw`new RegExp('foo', 'v')`,
],
invalid: [
{
Expand Down