1
1
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
2
2
import type { RegExpContext } from "../utils"
3
3
import { isEscapeSequence , createRule , defineRegexpVisitor } from "../utils"
4
- import GraphemeSplitter from "grapheme-splitter"
5
4
import type { ReadonlyFlags } from "regexp-ast-analysis"
6
5
import { mention , mentionChar } from "../utils/mention"
7
6
import type {
@@ -12,7 +11,7 @@ import type {
12
11
import type { PatternRange } from "../utils/ast-utils/pattern-source"
13
12
import type { Rule } from "eslint"
14
13
15
- const splitter = new GraphemeSplitter ( )
14
+ const segmenter = new Intl . Segmenter ( )
16
15
17
16
/** Returns whether the given string starts with a valid surrogate pair. */
18
17
function startsWithSurrogate ( s : string ) : boolean {
@@ -66,10 +65,10 @@ function getGraphemeBeforeQuant(quant: Quantifier): string {
66
65
quant . element . end - alt . start ,
67
66
)
68
67
69
- const graphemes = splitter . splitGraphemes ( before )
70
- const grapheme = graphemes [ graphemes . length - 1 ]
68
+ const segments = [ ... segmenter . segment ( before ) ]
69
+ const segment = segments [ segments . length - 1 ]
71
70
72
- return grapheme
71
+ return segment . segment
73
72
}
74
73
75
74
interface GraphemeProblem {
@@ -86,7 +85,7 @@ function getGraphemeProblems(
86
85
cc : CharacterClass ,
87
86
flags : ReadonlyFlags ,
88
87
) : GraphemeProblem [ ] {
89
- let offset = cc . negate ? 2 : 1
88
+ const offset = cc . negate ? 2 : 1
90
89
91
90
const ignoreElements = cc . elements . filter (
92
91
( element ) =>
@@ -95,14 +94,15 @@ function getGraphemeProblems(
95
94
element . type === "ClassStringDisjunction" ,
96
95
)
97
96
98
- const graphemes = splitter . splitGraphemes ( cc . raw . slice ( offset , - 1 ) )
99
97
const problems : GraphemeProblem [ ] = [ ]
100
98
101
- for ( const grapheme of graphemes ) {
102
- const problem = getProblem ( grapheme , flags )
99
+ for ( const { segment, index } of segmenter . segment (
100
+ cc . raw . slice ( offset , - 1 ) ,
101
+ ) ) {
102
+ const problem = getProblem ( segment , flags )
103
103
if ( problem !== null ) {
104
- const start = offset + cc . start
105
- const end = start + grapheme . length
104
+ const start = offset + index + cc . start
105
+ const end = start + segment . length
106
106
107
107
if (
108
108
ignoreElements . some (
@@ -113,7 +113,7 @@ function getGraphemeProblems(
113
113
}
114
114
115
115
problems . push ( {
116
- grapheme,
116
+ grapheme : segment ,
117
117
problem,
118
118
start,
119
119
end,
@@ -122,7 +122,6 @@ function getGraphemeProblems(
122
122
) ,
123
123
} )
124
124
}
125
- offset += grapheme . length
126
125
}
127
126
128
127
return problems
0 commit comments