Skip to content

Commit 999b69a

Browse files
feat: add onRegExpFlags to RegExpValidator.Options and deprecate onFlags (#50)
Co-authored-by: Michaël De Boey <[email protected]>
1 parent 59f5774 commit 999b69a

File tree

2 files changed

+68
-27
lines changed

2 files changed

+68
-27
lines changed

src/parser.ts

+18-8
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,26 @@ class RegExpParserState {
6262
return this._flags
6363
}
6464

65-
public onFlags(
65+
public onRegExpFlags(
6666
start: number,
6767
end: number,
68-
global: boolean,
69-
ignoreCase: boolean,
70-
multiline: boolean,
71-
unicode: boolean,
72-
sticky: boolean,
73-
dotAll: boolean,
74-
hasIndices: boolean,
68+
{
69+
global,
70+
ignoreCase,
71+
multiline,
72+
unicode,
73+
sticky,
74+
dotAll,
75+
hasIndices,
76+
}: {
77+
global: boolean
78+
ignoreCase: boolean
79+
multiline: boolean
80+
unicode: boolean
81+
sticky: boolean
82+
dotAll: boolean
83+
hasIndices: boolean
84+
},
7585
): void {
7686
this._flags = {
7787
type: "Flags",

src/validator.ts

+50-19
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,31 @@ export namespace RegExpValidator {
149149
*/
150150
onLiteralLeave?: (start: number, end: number) => void
151151

152+
/**
153+
* A function that is called when the validator found flags.
154+
* @param start The 0-based index of the first character.
155+
* @param end The next 0-based index of the last character.
156+
* @param flags.global `g` flag.
157+
* @param flags.ignoreCase `i` flag.
158+
* @param flags.multiline `m` flag.
159+
* @param flags.unicode `u` flag.
160+
* @param flags.sticky `y` flag.
161+
* @param flags.dotAll `s` flag.
162+
* @param flags.hasIndices `d` flag.
163+
*/
164+
onRegExpFlags?: (
165+
start: number,
166+
end: number,
167+
flags: {
168+
global: boolean
169+
ignoreCase: boolean
170+
multiline: boolean
171+
unicode: boolean
172+
sticky: boolean
173+
dotAll: boolean
174+
hasIndices: boolean
175+
},
176+
) => void
152177
/**
153178
* A function that is called when the validator found flags.
154179
* @param start The 0-based index of the first character.
@@ -160,6 +185,8 @@ export namespace RegExpValidator {
160185
* @param sticky `y` flag.
161186
* @param dotAll `s` flag.
162187
* @param hasIndices `d` flag.
188+
*
189+
* @deprecated Use `onRegExpFlags` instead.
163190
*/
164191
onFlags?: (
165192
start: number,
@@ -535,17 +562,15 @@ export class RegExpValidator {
535562
this.raise(`Invalid flag '${source[i]}'`)
536563
}
537564
}
538-
this.onFlags(
539-
start,
540-
end,
565+
this.onRegExpFlags(start, end, {
541566
global,
542567
ignoreCase,
543568
multiline,
544569
unicode,
545570
sticky,
546571
dotAll,
547572
hasIndices,
548-
)
573+
})
549574
}
550575

551576
/**
@@ -599,28 +624,34 @@ export class RegExpValidator {
599624
}
600625
}
601626

602-
private onFlags(
627+
private onRegExpFlags(
603628
start: number,
604629
end: number,
605-
global: boolean,
606-
ignoreCase: boolean,
607-
multiline: boolean,
608-
unicode: boolean,
609-
sticky: boolean,
610-
dotAll: boolean,
611-
hasIndices: boolean,
630+
flags: {
631+
global: boolean
632+
ignoreCase: boolean
633+
multiline: boolean
634+
unicode: boolean
635+
sticky: boolean
636+
dotAll: boolean
637+
hasIndices: boolean
638+
},
612639
): void {
640+
if (this._options.onRegExpFlags) {
641+
this._options.onRegExpFlags(start, end, flags)
642+
}
643+
// Backward compatibility
613644
if (this._options.onFlags) {
614645
this._options.onFlags(
615646
start,
616647
end,
617-
global,
618-
ignoreCase,
619-
multiline,
620-
unicode,
621-
sticky,
622-
dotAll,
623-
hasIndices,
648+
flags.global,
649+
flags.ignoreCase,
650+
flags.multiline,
651+
flags.unicode,
652+
flags.sticky,
653+
flags.dotAll,
654+
flags.hasIndices,
624655
)
625656
}
626657
}

0 commit comments

Comments
 (0)