Skip to content

Provide User-Friendly Message for Extended Unicode Escapes in Regular Expressions in Non-Unicode Modes #58981

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

Merged
merged 5 commits into from
Jul 18, 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
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,10 @@
"category": "Error",
"code": 1537
},
"Unicode escape sequences are only available when the Unicode (u) flag or the Unicode Sets (v) flag is set.": {
"category": "Error",
"code": 1538
},

"The types of '{0}' are incompatible between these types.": {
"category": "Error",
Expand Down
16 changes: 10 additions & 6 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ const enum EscapeSequenceScanningFlags {
AtomEscape = 1 << 5,

ReportInvalidEscapeErrors = RegularExpression | ReportErrors,
ScanExtendedUnicodeEscape = String | AnyUnicodeMode,
AllowExtendedUnicodeEscape = String | AnyUnicodeMode,
}

const enum ClassSetExpressionType {
Expand Down Expand Up @@ -1609,13 +1609,17 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
case CharacterCodes.doubleQuote:
return '"';
case CharacterCodes.u:
if (
flags & EscapeSequenceScanningFlags.ScanExtendedUnicodeEscape &&
pos < end && charCodeUnchecked(pos) === CharacterCodes.openBrace
) {
if (pos < end && charCodeUnchecked(pos) === CharacterCodes.openBrace) {
// '\u{DDDDDD}'
pos -= 2;
return scanExtendedUnicodeEscape(!!(flags & EscapeSequenceScanningFlags.ReportInvalidEscapeErrors));
const result = scanExtendedUnicodeEscape(!!(flags & EscapeSequenceScanningFlags.ReportInvalidEscapeErrors));
if (!(flags & EscapeSequenceScanningFlags.AllowExtendedUnicodeEscape)) {
tokenFlags |= TokenFlags.ContainsInvalidEscape;
if (flags & EscapeSequenceScanningFlags.ReportInvalidEscapeErrors) {
error(Diagnostics.Unicode_escape_sequences_are_only_available_when_the_Unicode_u_flag_or_the_Unicode_Sets_v_flag_is_set, start, pos - start);
}
}
return result;
}
// '\uDDDD'
for (; pos < start + 6; pos++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ regularExpressionCharacterClassRangeOrder.ts(7,5): error TS1517: Range out of or
regularExpressionCharacterClassRangeOrder.ts(7,12): error TS1517: Range out of order in character class.
regularExpressionCharacterClassRangeOrder.ts(8,11): error TS1517: Range out of order in character class.
regularExpressionCharacterClassRangeOrder.ts(9,11): error TS1517: Range out of order in character class.
regularExpressionCharacterClassRangeOrder.ts(11,6): error TS1125: Hexadecimal digit expected.
regularExpressionCharacterClassRangeOrder.ts(11,16): error TS1125: Hexadecimal digit expected.
regularExpressionCharacterClassRangeOrder.ts(11,27): error TS1125: Hexadecimal digit expected.
regularExpressionCharacterClassRangeOrder.ts(11,37): error TS1125: Hexadecimal digit expected.
regularExpressionCharacterClassRangeOrder.ts(11,4): error TS1538: Unicode escape sequences are only available when the Unicode (u) flag or the Unicode Sets (v) flag is set.
regularExpressionCharacterClassRangeOrder.ts(11,14): error TS1538: Unicode escape sequences are only available when the Unicode (u) flag or the Unicode Sets (v) flag is set.
regularExpressionCharacterClassRangeOrder.ts(11,25): error TS1538: Unicode escape sequences are only available when the Unicode (u) flag or the Unicode Sets (v) flag is set.
regularExpressionCharacterClassRangeOrder.ts(11,25): error TS1517: Range out of order in character class.
regularExpressionCharacterClassRangeOrder.ts(11,35): error TS1538: Unicode escape sequences are only available when the Unicode (u) flag or the Unicode Sets (v) flag is set.
regularExpressionCharacterClassRangeOrder.ts(12,25): error TS1517: Range out of order in character class.
regularExpressionCharacterClassRangeOrder.ts(13,25): error TS1517: Range out of order in character class.
regularExpressionCharacterClassRangeOrder.ts(15,10): error TS1517: Range out of order in character class.
Expand All @@ -14,7 +15,7 @@ regularExpressionCharacterClassRangeOrder.ts(16,31): error TS1517: Range out of
regularExpressionCharacterClassRangeOrder.ts(17,31): error TS1517: Range out of order in character class.


==== regularExpressionCharacterClassRangeOrder.ts (14 errors) ====
==== regularExpressionCharacterClassRangeOrder.ts (15 errors) ====
// The characters in the following regular expressions are ASCII-lookalike characters found in Unicode, including:
// - 𝘈 (U+1D608 Mathematical Sans-Serif Italic Capital A)
// - 𝘡 (U+1D621 Mathematical Sans-Serif Italic Capital Z)
Expand All @@ -34,14 +35,16 @@ regularExpressionCharacterClassRangeOrder.ts(17,31): error TS1517: Range out of
!!! error TS1517: Range out of order in character class.

/[\u{1D608}-\u{1D621}][\u{1D621}-\u{1D608}]/,

!!! error TS1125: Hexadecimal digit expected.

!!! error TS1125: Hexadecimal digit expected.

!!! error TS1125: Hexadecimal digit expected.

!!! error TS1125: Hexadecimal digit expected.
~~~~~~~~~
!!! error TS1538: Unicode escape sequences are only available when the Unicode (u) flag or the Unicode Sets (v) flag is set.
~~~~~~~~~
!!! error TS1538: Unicode escape sequences are only available when the Unicode (u) flag or the Unicode Sets (v) flag is set.
~~~~~~~~~
!!! error TS1538: Unicode escape sequences are only available when the Unicode (u) flag or the Unicode Sets (v) flag is set.
~~~~~~~~~~~~~~~~~~~
!!! error TS1517: Range out of order in character class.
~~~~~~~~~
!!! error TS1538: Unicode escape sequences are only available when the Unicode (u) flag or the Unicode Sets (v) flag is set.
/[\u{1D608}-\u{1D621}][\u{1D621}-\u{1D608}]/u,
~~~~~~~~~~~~~~~~~~~
!!! error TS1517: Range out of order in character class.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
regularExpressionExtendedUnicodeEscapes.ts(2,3): error TS1538: Unicode escape sequences are only available when the Unicode (u) flag or the Unicode Sets (v) flag is set.
regularExpressionExtendedUnicodeEscapes.ts(2,13): error TS1538: Unicode escape sequences are only available when the Unicode (u) flag or the Unicode Sets (v) flag is set.


==== regularExpressionExtendedUnicodeEscapes.ts (2 errors) ====
const regexes: RegExp[] = [
/\u{10000}[\u{10000}]/,
~~~~~~~~~
!!! error TS1538: Unicode escape sequences are only available when the Unicode (u) flag or the Unicode Sets (v) flag is set.
~~~~~~~~~
!!! error TS1538: Unicode escape sequences are only available when the Unicode (u) flag or the Unicode Sets (v) flag is set.
/\u{10000}[\u{10000}]/u,
/\u{10000}[\u{10000}]/v,
];

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//// [tests/cases/compiler/regularExpressionExtendedUnicodeEscapes.ts] ////

//// [regularExpressionExtendedUnicodeEscapes.ts]
const regexes: RegExp[] = [
/\u{10000}[\u{10000}]/,
/\u{10000}[\u{10000}]/u,
/\u{10000}[\u{10000}]/v,
];


//// [regularExpressionExtendedUnicodeEscapes.js]
const regexes = [
/\u{10000}[\u{10000}]/,
/\u{10000}[\u{10000}]/u,
/\u{10000}[\u{10000}]/v,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//// [tests/cases/compiler/regularExpressionExtendedUnicodeEscapes.ts] ////

=== regularExpressionExtendedUnicodeEscapes.ts ===
const regexes: RegExp[] = [
>regexes : Symbol(regexes, Decl(regularExpressionExtendedUnicodeEscapes.ts, 0, 5))
>RegExp : Symbol(RegExp, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.regexp.d.ts, --, --) ... and 3 more)

/\u{10000}[\u{10000}]/,
/\u{10000}[\u{10000}]/u,
/\u{10000}[\u{10000}]/v,
];

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [tests/cases/compiler/regularExpressionExtendedUnicodeEscapes.ts] ////

=== regularExpressionExtendedUnicodeEscapes.ts ===
const regexes: RegExp[] = [
>regexes : RegExp[]
> : ^^^^^^^^
>[ /\u{10000}[\u{10000}]/, /\u{10000}[\u{10000}]/u, /\u{10000}[\u{10000}]/v,] : RegExp[]
> : ^^^^^^^^

/\u{10000}[\u{10000}]/,
>/\u{10000}[\u{10000}]/ : RegExp
> : ^^^^^^

/\u{10000}[\u{10000}]/u,
>/\u{10000}[\u{10000}]/u : RegExp
> : ^^^^^^

/\u{10000}[\u{10000}]/v,
>/\u{10000}[\u{10000}]/v : RegExp
> : ^^^^^^

];

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @target: esnext
const regexes: RegExp[] = [
/\u{10000}[\u{10000}]/,
/\u{10000}[\u{10000}]/u,
/\u{10000}[\u{10000}]/v,
];