Skip to content

Commit 024a64a

Browse files
authored
✨ Supports ES2022 RegExp Match Indices (#22)
1 parent 20e0388 commit 024a64a

19 files changed

+1415
-652
lines changed

src/ast.ts

+1
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ export interface Flags extends NodeBase {
288288
parent: RegExpLiteral | null
289289
dotAll: boolean
290290
global: boolean
291+
hasIndices: boolean
291292
ignoreCase: boolean
292293
multiline: boolean
293294
sticky: boolean

src/ecma-versions.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
export type EcmaVersion = 5 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021
1+
export type EcmaVersion =
2+
| 5
3+
| 2015
4+
| 2016
5+
| 2017
6+
| 2018
7+
| 2019
8+
| 2020
9+
| 2021
10+
| 2022

src/parser.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class RegExpParserState {
4040

4141
public constructor(options?: RegExpParser.Options) {
4242
this.strict = Boolean(options && options.strict)
43-
this.ecmaVersion = (options && options.ecmaVersion) || 2020
43+
this.ecmaVersion = (options && options.ecmaVersion) || 2022
4444
}
4545

4646
public get pattern(): Pattern {
@@ -66,6 +66,7 @@ class RegExpParserState {
6666
unicode: boolean,
6767
sticky: boolean,
6868
dotAll: boolean,
69+
hasIndices: boolean,
6970
): void {
7071
this._flags = {
7172
type: "Flags",
@@ -79,6 +80,7 @@ class RegExpParserState {
7980
unicode,
8081
sticky,
8182
dotAll,
83+
hasIndices,
8284
}
8385
}
8486

@@ -502,11 +504,12 @@ export namespace RegExpParser {
502504
strict?: boolean
503505

504506
/**
505-
* ECMAScript version. Default is `2020`.
507+
* ECMAScript version. Default is `2022`.
506508
* - `2015` added `u` and `y` flags.
507509
* - `2018` added `s` flag, Named Capturing Group, Lookbehind Assertion,
508510
* and Unicode Property Escape.
509511
* - `2019`, `2020`, and `2021` added more valid Unicode Property Escapes.
512+
* - `2022` added `d` flag.
510513
*/
511514
ecmaVersion?: EcmaVersion
512515
}

src/validator.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,12 @@ export namespace RegExpValidator {
127127
strict?: boolean
128128

129129
/**
130-
* ECMAScript version. Default is `2020`.
130+
* ECMAScript version. Default is `2022`.
131131
* - `2015` added `u` and `y` flags.
132132
* - `2018` added `s` flag, Named Capturing Group, Lookbehind Assertion,
133133
* and Unicode Property Escape.
134134
* - `2019`, `2020`, and `2021` added more valid Unicode Property Escapes.
135+
* - `2022` added `d` flag.
135136
*/
136137
ecmaVersion?: EcmaVersion
137138

@@ -158,6 +159,7 @@ export namespace RegExpValidator {
158159
* @param unicode `u` flag.
159160
* @param sticky `y` flag.
160161
* @param dotAll `s` flag.
162+
* @param hasIndices `d` flag.
161163
*/
162164
onFlags?(
163165
start: number,
@@ -168,6 +170,7 @@ export namespace RegExpValidator {
168170
unicode: boolean,
169171
sticky: boolean,
170172
dotAll: boolean,
173+
hasIndices: boolean,
171174
): void
172175

173176
/**
@@ -476,6 +479,7 @@ export class RegExpValidator {
476479
let sticky = false
477480
let unicode = false
478481
let dotAll = false
482+
let hasIndices = false
479483
for (let i = start; i < end; ++i) {
480484
const flag = source.charCodeAt(i)
481485

@@ -496,6 +500,8 @@ export class RegExpValidator {
496500
sticky = true
497501
} else if (flag === LatinSmallLetterS && this.ecmaVersion >= 2018) {
498502
dotAll = true
503+
} else if (flag === LatinSmallLetterD && this.ecmaVersion >= 2022) {
504+
hasIndices = true
499505
} else {
500506
this.raise(`Invalid flag '${source[i]}'`)
501507
}
@@ -509,6 +515,7 @@ export class RegExpValidator {
509515
unicode,
510516
sticky,
511517
dotAll,
518+
hasIndices,
512519
)
513520
}
514521

@@ -548,7 +555,7 @@ export class RegExpValidator {
548555
}
549556

550557
private get ecmaVersion() {
551-
return this._options.ecmaVersion || 2020
558+
return this._options.ecmaVersion || 2022
552559
}
553560

554561
private onLiteralEnter(start: number): void {
@@ -572,6 +579,7 @@ export class RegExpValidator {
572579
unicode: boolean,
573580
sticky: boolean,
574581
dotAll: boolean,
582+
hasIndices: boolean,
575583
): void {
576584
if (this._options.onFlags) {
577585
this._options.onFlags(
@@ -583,6 +591,7 @@ export class RegExpValidator {
583591
unicode,
584592
sticky,
585593
dotAll,
594+
hasIndices,
586595
)
587596
}
588597
}

test/fixtures/parser/literal.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,19 @@ import path from "path"
33

44
type FixtureData = {
55
[filename: string]: {
6-
options: { strict: boolean; ecmaVersion: 5 | 2015 | 2016 | 2017 | 2018 }
6+
options: {
7+
strict: boolean
8+
ecmaVersion:
9+
| 5
10+
| 2015
11+
| 2016
12+
| 2017
13+
| 2018
14+
| 2019
15+
| 2020
16+
| 2021
17+
| 2022
18+
}
719
patterns: {
820
[source: string]:
921
| { ast: object }

0 commit comments

Comments
 (0)