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

Co-authored-by: Ron Buckton <ron.buckton@microsoft.com>
This commit is contained in:
graphemecluster 2024-07-18 10:08:54 +08:00 committed by GitHub
parent afa03f0948
commit 165350dc8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 102 additions and 19 deletions

View File

@ -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",

View File

@ -1006,7 +1006,7 @@ const enum EscapeSequenceScanningFlags {
AtomEscape = 1 << 5,
ReportInvalidEscapeErrors = RegularExpression | ReportErrors,
ScanExtendedUnicodeEscape = String | AnyUnicodeMode,
AllowExtendedUnicodeEscape = String | AnyUnicodeMode,
}
const enum ClassSetExpressionType {
@ -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++) {

View File

@ -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.
@ -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)
@ -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.

View File

@ -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,
];

View File

@ -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,
];

View File

@ -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,
];

View File

@ -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
> : ^^^^^^
];

View File

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