Skip to content

Commit 5712c5f

Browse files
fix: allow named backreferences without u flag in strict (#56)
* fix: allow named backreferences without u flag in strict * Update src/validator.ts Co-authored-by: Michael Schmidt <msrd0000@gmail.com> * chore: format and add comment * test: add test case for invalid --------- Co-authored-by: Michael Schmidt <msrd0000@gmail.com>
1 parent 271cc01 commit 5712c5f

File tree

3 files changed

+188
-1
lines changed

3 files changed

+188
-1
lines changed

src/validator.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,12 @@ export class RegExpValidator {
587587
uFlag = false,
588588
): void {
589589
this._uFlag = uFlag && this.ecmaVersion >= 2015
590-
this._nFlag = uFlag && this.ecmaVersion >= 2018
590+
this._nFlag =
591+
(uFlag && this.ecmaVersion >= 2018) ||
592+
// Introduced as Normative Change in ES2023
593+
// See https://github.com/tc39/ecma262/pull/2436
594+
Boolean(this._options.strict && this.ecmaVersion >= 2023)
595+
591596
this.reset(source, start, end)
592597
this.consumePattern()
593598

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"options": {
3+
"strict": true,
4+
"ecmaVersion": 2022
5+
},
6+
"patterns": {
7+
"/(?<foo>A)\\k<foo>/": {
8+
"error": {
9+
"message": "Invalid regular expression: /(?<foo>A)\\k<foo>/: Invalid escape",
10+
"index": 11
11+
}
12+
}
13+
}
14+
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
{
2+
"options": {
3+
"strict": true,
4+
"ecmaVersion": 2023
5+
},
6+
"patterns": {
7+
"/(?<foo>A)\\k<foo>/": {
8+
"ast": {
9+
"type": "RegExpLiteral",
10+
"parent": null,
11+
"start": 0,
12+
"end": 18,
13+
"raw": "/(?<foo>A)\\k<foo>/",
14+
"pattern": {
15+
"type": "Pattern",
16+
"parent": "♻️..",
17+
"start": 1,
18+
"end": 17,
19+
"raw": "(?<foo>A)\\k<foo>",
20+
"alternatives": [
21+
{
22+
"type": "Alternative",
23+
"parent": "♻️../..",
24+
"start": 1,
25+
"end": 17,
26+
"raw": "(?<foo>A)\\k<foo>",
27+
"elements": [
28+
{
29+
"type": "CapturingGroup",
30+
"parent": "♻️../..",
31+
"start": 1,
32+
"end": 10,
33+
"raw": "(?<foo>A)",
34+
"name": "foo",
35+
"alternatives": [
36+
{
37+
"type": "Alternative",
38+
"parent": "♻️../..",
39+
"start": 8,
40+
"end": 9,
41+
"raw": "A",
42+
"elements": [
43+
{
44+
"type": "Character",
45+
"parent": "♻️../..",
46+
"start": 8,
47+
"end": 9,
48+
"raw": "A",
49+
"value": 65
50+
}
51+
]
52+
}
53+
],
54+
"references": [
55+
"♻️../1"
56+
]
57+
},
58+
{
59+
"type": "Backreference",
60+
"parent": "♻️../..",
61+
"start": 10,
62+
"end": 17,
63+
"raw": "\\k<foo>",
64+
"ref": "foo",
65+
"resolved": "♻️../0"
66+
}
67+
]
68+
}
69+
]
70+
},
71+
"flags": {
72+
"type": "Flags",
73+
"parent": "♻️..",
74+
"start": 18,
75+
"end": 18,
76+
"raw": "",
77+
"global": false,
78+
"ignoreCase": false,
79+
"multiline": false,
80+
"unicode": false,
81+
"sticky": false,
82+
"dotAll": false,
83+
"hasIndices": false
84+
}
85+
}
86+
},
87+
"/(?<foo>A)\\k<foo>/u": {
88+
"ast": {
89+
"type": "RegExpLiteral",
90+
"parent": null,
91+
"start": 0,
92+
"end": 19,
93+
"raw": "/(?<foo>A)\\k<foo>/u",
94+
"pattern": {
95+
"type": "Pattern",
96+
"parent": "♻️..",
97+
"start": 1,
98+
"end": 17,
99+
"raw": "(?<foo>A)\\k<foo>",
100+
"alternatives": [
101+
{
102+
"type": "Alternative",
103+
"parent": "♻️../..",
104+
"start": 1,
105+
"end": 17,
106+
"raw": "(?<foo>A)\\k<foo>",
107+
"elements": [
108+
{
109+
"type": "CapturingGroup",
110+
"parent": "♻️../..",
111+
"start": 1,
112+
"end": 10,
113+
"raw": "(?<foo>A)",
114+
"name": "foo",
115+
"alternatives": [
116+
{
117+
"type": "Alternative",
118+
"parent": "♻️../..",
119+
"start": 8,
120+
"end": 9,
121+
"raw": "A",
122+
"elements": [
123+
{
124+
"type": "Character",
125+
"parent": "♻️../..",
126+
"start": 8,
127+
"end": 9,
128+
"raw": "A",
129+
"value": 65
130+
}
131+
]
132+
}
133+
],
134+
"references": [
135+
"♻️../1"
136+
]
137+
},
138+
{
139+
"type": "Backreference",
140+
"parent": "♻️../..",
141+
"start": 10,
142+
"end": 17,
143+
"raw": "\\k<foo>",
144+
"ref": "foo",
145+
"resolved": "♻️../0"
146+
}
147+
]
148+
}
149+
]
150+
},
151+
"flags": {
152+
"type": "Flags",
153+
"parent": "♻️..",
154+
"start": 18,
155+
"end": 19,
156+
"raw": "u",
157+
"global": false,
158+
"ignoreCase": false,
159+
"multiline": false,
160+
"unicode": true,
161+
"sticky": false,
162+
"dotAll": false,
163+
"hasIndices": false
164+
}
165+
}
166+
}
167+
}
168+
}

0 commit comments

Comments
 (0)