Skip to content

Commit

Permalink
fix: set Group#modifiers to null if group has no modifiers, for bac…
Browse files Browse the repository at this point in the history
…kward compatibility
  • Loading branch information
ota-meshi committed Oct 11, 2024
1 parent a2477a1 commit 67a366d
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 510 deletions.
2 changes: 1 addition & 1 deletion src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export interface Alternative extends NodeBase {
export interface Group extends NodeBase {
type: "Group"
parent: Alternative | Quantifier
modifiers: Modifiers
modifiers: Modifiers | null
alternatives: Alternative[]
}

Expand Down
29 changes: 2 additions & 27 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import type {
ExpressionCharacterClass,
StringAlternative,
Modifiers,
ModifierFlags,
} from "./ast"
import type { EcmaVersion } from "./ecma-versions"
import { latestEcmaVersion } from "./ecma-versions"
Expand Down Expand Up @@ -208,39 +207,15 @@ class RegExpParserState {
throw new Error("UnknownError")
}

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,
modifiers: null,
alternatives: [],
}
modifiers.parent = group

this._node = group
parent.elements.push(this._node)
Expand Down Expand Up @@ -269,7 +244,7 @@ class RegExpParserState {
start,
end: start,
raw: "",
add: parent.modifiers.add,
add: null as never, // Set in onAddModifiers.
remove: null,
}
parent.modifiers = this._node
Expand Down
13 changes: 9 additions & 4 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1758,12 +1758,17 @@ export class RegExpValidator {
*/
private consumeModifiers(): boolean {
const start = this.index
this.onModifiersEnter(start)
const hasAddModifiers = this.eatModifiers()
const addModifiers = this.parseModifiers(start, this.index)
this.onAddModifiers(start, this.index, addModifiers)
const addModifiersEnd = this.index
const hasHyphen = this.eat(HYPHEN_MINUS)
if (!hasAddModifiers && !hasHyphen) {
return false
}
this.onModifiersEnter(start)
const addModifiers = this.parseModifiers(start, addModifiersEnd)
this.onAddModifiers(start, addModifiersEnd, addModifiers)

if (this.eat(HYPHEN_MINUS)) {
if (hasHyphen) {
const modifiersStart = this.index
if (!this.eatModifiers() && !hasAddModifiers) {
this.raise("Invalid empty flags")
Expand Down
38 changes: 2 additions & 36 deletions test/fixtures/parser/literal/basic-valid-2015-u.json
Original file line number Diff line number Diff line change
Expand Up @@ -2519,24 +2519,7 @@
"start": 1,
"end": 6,
"raw": "(?:a)",
"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
},
"modifiers": null,
"alternatives": [
{
"type": "Alternative",
Expand Down Expand Up @@ -11295,24 +11278,7 @@
"start": 48,
"end": 67,
"raw": "(?:\\.[a-zA-Z0-9-]+)",
"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
},
"modifiers": null,
"alternatives": [
{
"type": "Alternative",
Expand Down
76 changes: 4 additions & 72 deletions test/fixtures/parser/literal/basic-valid-2015.json
Original file line number Diff line number Diff line change
Expand Up @@ -3933,24 +3933,7 @@
"start": 1,
"end": 6,
"raw": "(?:a)",
"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
},
"modifiers": null,
"alternatives": [
{
"type": "Alternative",
Expand Down Expand Up @@ -4105,24 +4088,7 @@
"start": 1,
"end": 6,
"raw": "(?:a)",
"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
},
"modifiers": null,
"alternatives": [
{
"type": "Alternative",
Expand Down Expand Up @@ -5225,24 +5191,7 @@
"start": 1,
"end": 6,
"raw": "(?:a)",
"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
},
"modifiers": null,
"alternatives": [
{
"type": "Alternative",
Expand Down Expand Up @@ -17064,24 +17013,7 @@
"start": 48,
"end": 67,
"raw": "(?:\\.[a-zA-Z0-9-]+)",
"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
},
"modifiers": null,
"alternatives": [
{
"type": "Alternative",
Expand Down
76 changes: 4 additions & 72 deletions test/fixtures/parser/literal/basic-valid.json
Original file line number Diff line number Diff line change
Expand Up @@ -3933,24 +3933,7 @@
"start": 1,
"end": 6,
"raw": "(?:a)",
"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
},
"modifiers": null,
"alternatives": [
{
"type": "Alternative",
Expand Down Expand Up @@ -4105,24 +4088,7 @@
"start": 1,
"end": 6,
"raw": "(?:a)",
"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
},
"modifiers": null,
"alternatives": [
{
"type": "Alternative",
Expand Down Expand Up @@ -5225,24 +5191,7 @@
"start": 1,
"end": 6,
"raw": "(?:a)",
"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
},
"modifiers": null,
"alternatives": [
{
"type": "Alternative",
Expand Down Expand Up @@ -17064,24 +17013,7 @@
"start": 48,
"end": 67,
"raw": "(?:\\.[a-zA-Z0-9-]+)",
"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
},
"modifiers": null,
"alternatives": [
{
"type": "Alternative",
Expand Down
Loading

0 comments on commit 67a366d

Please sign in to comment.