Skip to content

Commit

Permalink
✨ Supports ES2022 RegExp Match Indices (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Jun 14, 2021
1 parent 20e0388 commit 024a64a
Show file tree
Hide file tree
Showing 19 changed files with 1,415 additions and 652 deletions.
1 change: 1 addition & 0 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ export interface Flags extends NodeBase {
parent: RegExpLiteral | null
dotAll: boolean
global: boolean
hasIndices: boolean
ignoreCase: boolean
multiline: boolean
sticky: boolean
Expand Down
11 changes: 10 additions & 1 deletion src/ecma-versions.ts
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
export type EcmaVersion = 5 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021
export type EcmaVersion =
| 5
| 2015
| 2016
| 2017
| 2018
| 2019
| 2020
| 2021
| 2022
7 changes: 5 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class RegExpParserState {

public constructor(options?: RegExpParser.Options) {
this.strict = Boolean(options && options.strict)
this.ecmaVersion = (options && options.ecmaVersion) || 2020
this.ecmaVersion = (options && options.ecmaVersion) || 2022
}

public get pattern(): Pattern {
Expand All @@ -66,6 +66,7 @@ class RegExpParserState {
unicode: boolean,
sticky: boolean,
dotAll: boolean,
hasIndices: boolean,
): void {
this._flags = {
type: "Flags",
Expand All @@ -79,6 +80,7 @@ class RegExpParserState {
unicode,
sticky,
dotAll,
hasIndices,
}
}

Expand Down Expand Up @@ -502,11 +504,12 @@ export namespace RegExpParser {
strict?: boolean

/**
* ECMAScript version. Default is `2020`.
* ECMAScript version. Default is `2022`.
* - `2015` added `u` and `y` flags.
* - `2018` added `s` flag, Named Capturing Group, Lookbehind Assertion,
* and Unicode Property Escape.
* - `2019`, `2020`, and `2021` added more valid Unicode Property Escapes.
* - `2022` added `d` flag.
*/
ecmaVersion?: EcmaVersion
}
Expand Down
13 changes: 11 additions & 2 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,12 @@ export namespace RegExpValidator {
strict?: boolean

/**
* ECMAScript version. Default is `2020`.
* ECMAScript version. Default is `2022`.
* - `2015` added `u` and `y` flags.
* - `2018` added `s` flag, Named Capturing Group, Lookbehind Assertion,
* and Unicode Property Escape.
* - `2019`, `2020`, and `2021` added more valid Unicode Property Escapes.
* - `2022` added `d` flag.
*/
ecmaVersion?: EcmaVersion

Expand All @@ -158,6 +159,7 @@ export namespace RegExpValidator {
* @param unicode `u` flag.
* @param sticky `y` flag.
* @param dotAll `s` flag.
* @param hasIndices `d` flag.
*/
onFlags?(
start: number,
Expand All @@ -168,6 +170,7 @@ export namespace RegExpValidator {
unicode: boolean,
sticky: boolean,
dotAll: boolean,
hasIndices: boolean,
): void

/**
Expand Down Expand Up @@ -476,6 +479,7 @@ export class RegExpValidator {
let sticky = false
let unicode = false
let dotAll = false
let hasIndices = false
for (let i = start; i < end; ++i) {
const flag = source.charCodeAt(i)

Expand All @@ -496,6 +500,8 @@ export class RegExpValidator {
sticky = true
} else if (flag === LatinSmallLetterS && this.ecmaVersion >= 2018) {
dotAll = true
} else if (flag === LatinSmallLetterD && this.ecmaVersion >= 2022) {
hasIndices = true
} else {
this.raise(`Invalid flag '${source[i]}'`)
}
Expand All @@ -509,6 +515,7 @@ export class RegExpValidator {
unicode,
sticky,
dotAll,
hasIndices,
)
}

Expand Down Expand Up @@ -548,7 +555,7 @@ export class RegExpValidator {
}

private get ecmaVersion() {
return this._options.ecmaVersion || 2020
return this._options.ecmaVersion || 2022
}

private onLiteralEnter(start: number): void {
Expand All @@ -572,6 +579,7 @@ export class RegExpValidator {
unicode: boolean,
sticky: boolean,
dotAll: boolean,
hasIndices: boolean,
): void {
if (this._options.onFlags) {
this._options.onFlags(
Expand All @@ -583,6 +591,7 @@ export class RegExpValidator {
unicode,
sticky,
dotAll,
hasIndices,
)
}
}
Expand Down
14 changes: 13 additions & 1 deletion test/fixtures/parser/literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@ import path from "path"

type FixtureData = {
[filename: string]: {
options: { strict: boolean; ecmaVersion: 5 | 2015 | 2016 | 2017 | 2018 }
options: {
strict: boolean
ecmaVersion:
| 5
| 2015
| 2016
| 2017
| 2018
| 2019
| 2020
| 2021
| 2022
}
patterns: {
[source: string]:
| { ast: object }
Expand Down
Loading

0 comments on commit 024a64a

Please sign in to comment.