Skip to content

Commit 91497a8

Browse files
committed
Allow /[\k]/
1 parent 5f254de commit 91497a8

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const hexDigits = '0123456789abcdefABCDEF'.split('');
4545
const decimalDigits = '0123456789'.split('');
4646
const octalDigits = '01234567'.split('');
4747

48-
const INVALID_NAMED_BACKREFERENCE_SENTINEL = { INVALID_NAMED_BACKREFERENCE_SENTINE: true };
48+
const INVALID_NAMED_BACKREFERENCE_SENTINEL = {};
4949

5050
function isIdentifierStart(ch) {
5151
return ch < 128 ? idStartBool[ch] : idStartLargeRegex.test(String.fromCodePoint(ch));
@@ -669,7 +669,14 @@ const acceptCharacterClass = backtrackOnFailure(state => {
669669
return { matched: true, value: character.charCodeAt(0) % 32 };
670670
}),
671671
acceptCharacterClassEscape,
672-
acceptCharacterEscape
672+
acceptCharacterEscape,
673+
// We special-case `\k` because `acceptCharacterEscape` rejects `\k` unconditionally,
674+
// deferring `\k` to acceptGroupNameBackreference, which is not called here.
675+
// See also https://github.com/tc39/ecma262/issues/2037. This code takes the route of
676+
// making it unconditionally legal, rather than legal only in the absence of a group name.
677+
subState => {
678+
return { matched: !subState.unicode && !!subState.eat('k'), value: 107 };
679+
},
673680
);
674681

675682
const acceptClassAtomNoDash = localState => {

test/literal-regexp-expression.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ suite('Parser', () => {
184184
/\k</
185185
/\k<x/
186186
/\k<x>/
187+
/[\k]/
188+
/[\k<]/
189+
/[\k<x]/
190+
/[\k<x>]/
191+
/[\k](?<x>)/
187192
/[${'\\'}1-${'\\'}127]/
188193
/[${'\\'}128-9]/
189194
/[${'\\'}99-${'\\'}100]/
@@ -229,6 +234,7 @@ suite('Parser', () => {
229234
/${'\\u'}{ZZ}/u
230235
/5{5,1G}/u
231236
/\k/u
237+
/[\k]/u
232238
/\k<X>/u
233239
/(?<X>)(?<X>)/
234240
/\p{ASCIIII}/u

0 commit comments

Comments
 (0)