Skip to content

Commit

Permalink
fix for es<2024
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Mar 17, 2024
1 parent fb0dade commit b83eaf7
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/ecma-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export type EcmaVersion =
| 2022
| 2023
| 2024
export const latestEcmaVersion = 2024
| 2025
export const latestEcmaVersion = 2025
33 changes: 30 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
ExpressionCharacterClass,
StringAlternative,
Modifiers,
ModifierFlags,
} from "./ast"
import type { EcmaVersion } from "./ecma-versions"
import { latestEcmaVersion } from "./ecma-versions"
Expand Down Expand Up @@ -198,15 +199,41 @@ class RegExpParserState {
throw new Error("UnknownError")
}

this._node = {
const modifiersStart = start + 2 /* offset `(?` */
const addModifiers: ModifierFlags = {
type: "ModifierFlags",
parent: null as never,
start: modifiersStart,
end: modifiersStart,
raw: "",
ignoreCase: false,
multiline: false,
dotAll: false,
}

const modifiers: Modifiers = {
type: "Modifiers",
parent: null as never,
start: modifiersStart,
end: modifiersStart,
raw: "",
add: addModifiers,
remove: null,
}
addModifiers.parent = modifiers

const group: Group = {
type: "Group",
parent,
start,
end: start,
raw: "",
modifiers: null as never, // Set in onModifiersEnter.
modifiers,
alternatives: [],
}
modifiers.parent = group

this._node = group
parent.elements.push(this._node)
}

Expand All @@ -233,7 +260,7 @@ class RegExpParserState {
start,
end: start,
raw: "",
add: null as never, // Set in onAddModifiers.
add: parent.modifiers.add,
remove: null,
}
parent.modifiers = this._node
Expand Down
2 changes: 1 addition & 1 deletion src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1719,7 +1719,7 @@ export class RegExpValidator {
const start = this.index
if (this.eat2(LEFT_PARENTHESIS, QUESTION_MARK)) {
this.onGroupEnter(start)
if (this.ecmaVersion >= 2024) {
if (this.ecmaVersion >= 2025) {
this.consumeModifiers()
}

Expand Down
38 changes: 36 additions & 2 deletions test/fixtures/parser/literal/basic-valid-2015-u.json
Original file line number Diff line number Diff line change
Expand Up @@ -2515,7 +2515,24 @@
"start": 1,
"end": 6,
"raw": "(?:a)",
"modifiers": null,
"modifiers": {
"type": "Modifiers",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"add": {
"type": "ModifierFlags",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"ignoreCase": false,
"multiline": false,
"dotAll": false
},
"remove": null
},
"alternatives": [
{
"type": "Alternative",
Expand Down Expand Up @@ -11274,7 +11291,24 @@
"start": 48,
"end": 67,
"raw": "(?:\\.[a-zA-Z0-9-]+)",
"modifiers": null,
"modifiers": {
"type": "Modifiers",
"parent": "♻️..",
"start": 50,
"end": 50,
"raw": "",
"add": {
"type": "ModifierFlags",
"parent": "♻️..",
"start": 50,
"end": 50,
"raw": "",
"ignoreCase": false,
"multiline": false,
"dotAll": false
},
"remove": null
},
"alternatives": [
{
"type": "Alternative",
Expand Down
76 changes: 72 additions & 4 deletions test/fixtures/parser/literal/basic-valid-2015.json
Original file line number Diff line number Diff line change
Expand Up @@ -3931,7 +3931,24 @@
"start": 1,
"end": 6,
"raw": "(?:a)",
"modifiers": null,
"modifiers": {
"type": "Modifiers",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"add": {
"type": "ModifierFlags",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"ignoreCase": false,
"multiline": false,
"dotAll": false
},
"remove": null
},
"alternatives": [
{
"type": "Alternative",
Expand Down Expand Up @@ -4086,7 +4103,24 @@
"start": 1,
"end": 6,
"raw": "(?:a)",
"modifiers": null,
"modifiers": {
"type": "Modifiers",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"add": {
"type": "ModifierFlags",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"ignoreCase": false,
"multiline": false,
"dotAll": false
},
"remove": null
},
"alternatives": [
{
"type": "Alternative",
Expand Down Expand Up @@ -5187,7 +5221,24 @@
"start": 1,
"end": 6,
"raw": "(?:a)",
"modifiers": null,
"modifiers": {
"type": "Modifiers",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"add": {
"type": "ModifierFlags",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"ignoreCase": false,
"multiline": false,
"dotAll": false
},
"remove": null
},
"alternatives": [
{
"type": "Alternative",
Expand Down Expand Up @@ -17009,7 +17060,24 @@
"start": 48,
"end": 67,
"raw": "(?:\\.[a-zA-Z0-9-]+)",
"modifiers": null,
"modifiers": {
"type": "Modifiers",
"parent": "♻️..",
"start": 50,
"end": 50,
"raw": "",
"add": {
"type": "ModifierFlags",
"parent": "♻️..",
"start": 50,
"end": 50,
"raw": "",
"ignoreCase": false,
"multiline": false,
"dotAll": false
},
"remove": null
},
"alternatives": [
{
"type": "Alternative",
Expand Down
76 changes: 72 additions & 4 deletions test/fixtures/parser/literal/basic-valid.json
Original file line number Diff line number Diff line change
Expand Up @@ -3931,7 +3931,24 @@
"start": 1,
"end": 6,
"raw": "(?:a)",
"modifiers": null,
"modifiers": {
"type": "Modifiers",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"add": {
"type": "ModifierFlags",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"ignoreCase": false,
"multiline": false,
"dotAll": false
},
"remove": null
},
"alternatives": [
{
"type": "Alternative",
Expand Down Expand Up @@ -4086,7 +4103,24 @@
"start": 1,
"end": 6,
"raw": "(?:a)",
"modifiers": null,
"modifiers": {
"type": "Modifiers",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"add": {
"type": "ModifierFlags",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"ignoreCase": false,
"multiline": false,
"dotAll": false
},
"remove": null
},
"alternatives": [
{
"type": "Alternative",
Expand Down Expand Up @@ -5187,7 +5221,24 @@
"start": 1,
"end": 6,
"raw": "(?:a)",
"modifiers": null,
"modifiers": {
"type": "Modifiers",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"add": {
"type": "ModifierFlags",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"ignoreCase": false,
"multiline": false,
"dotAll": false
},
"remove": null
},
"alternatives": [
{
"type": "Alternative",
Expand Down Expand Up @@ -17009,7 +17060,24 @@
"start": 48,
"end": 67,
"raw": "(?:\\.[a-zA-Z0-9-]+)",
"modifiers": null,
"modifiers": {
"type": "Modifiers",
"parent": "♻️..",
"start": 50,
"end": 50,
"raw": "",
"add": {
"type": "ModifierFlags",
"parent": "♻️..",
"start": 50,
"end": 50,
"raw": "",
"ignoreCase": false,
"multiline": false,
"dotAll": false
},
"remove": null
},
"alternatives": [
{
"type": "Alternative",
Expand Down
Loading

0 comments on commit b83eaf7

Please sign in to comment.