Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unicode escape sequence within identifier is always in unicode mode #148

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@
return modifiersGroup;
}

function parseUnicodeSurrogatePairEscape(firstEscape) {
function parseUnicodeSurrogatePairEscape(firstEscape, isUnicodeMode) {
if (isUnicodeMode) {
var first, second;
if (firstEscape.kind == 'unicodeEscape' &&
Expand Down Expand Up @@ -1041,12 +1041,13 @@
}
}

function parseRegExpUnicodeEscapeSequence() {
function parseRegExpUnicodeEscapeSequence(isUnicodeMode) {
var res;
if (res = matchReg(/^u([0-9a-fA-F]{4})/)) {
// UnicodeEscapeSequence
return parseUnicodeSurrogatePairEscape(
createEscaped('unicodeEscape', parseInt(res[1], 16), res[1], 2)
createEscaped('unicodeEscape', parseInt(res[1], 16), res[1], 2),
isUnicodeMode
);
} else if (isUnicodeMode && (res = matchReg(/^u\{([0-9a-fA-F]+)\}/))) {
// RegExpUnicodeEscapeSequence (ES6 Unicode code point escape)
Expand All @@ -1059,8 +1060,8 @@
// ControlEscape
// c ControlLetter
// HexEscapeSequence
// UnicodeEscapeSequence
// IdentityEscape
// UnicodeEscapeSequence[?UnicodeMode]
// IdentityEscape[?UnicodeMode]

var res;
var from = pos;
Expand All @@ -1081,7 +1082,7 @@
} else if (res = matchReg(/^x([0-9a-fA-F]{2})/)) {
// HexEscapeSequence
return createEscaped('hexadecimalEscape', parseInt(res[1], 16), res[1], 2);
} else if (res = parseRegExpUnicodeEscapeSequence()) {
} else if (res = parseRegExpUnicodeEscapeSequence(isUnicodeMode)) {
if (!res || res.codePoint > 0x10FFFF) {
bail('Invalid escape sequence', null, from, pos);
}
Expand All @@ -1093,11 +1094,22 @@
}

function parseIdentifierAtom(check) {
// RegExpIdentifierStart[UnicodeMode] ::
// IdentifierStartChar
// \ RegExpUnicodeEscapeSequence[+UnicodeMode]
// [~UnicodeMode] UnicodeLeadSurrogate UnicodeTrailSurrogate
//
// RegExpIdentifierPart[UnicodeMode] ::
// IdentifierPartChar
// \ RegExpUnicodeEscapeSequence[+UnicodeMode]
// [~UnicodeMode] UnicodeLeadSurrogate UnicodeTrailSurrogate


var ch = lookahead();
var from = pos;
if (ch === '\\') {
incr();
var esc = parseRegExpUnicodeEscapeSequence();
var esc = parseRegExpUnicodeEscapeSequence(true);
if (!esc || !check(esc.codePoint)) {
bail('Invalid escape sequence', null, from, pos);
}
Expand Down Expand Up @@ -1366,7 +1378,7 @@
bail('classEscape');
}

return parseUnicodeSurrogatePairEscape(res);
return parseUnicodeSurrogatePairEscape(res, isUnicodeMode);
}
}

Expand Down
Loading