Skip to content

Commit bbf9579

Browse files
Added scanning support for extended escapes.
1 parent 6ad1780 commit bbf9579

File tree

178 files changed

+553
-1033
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

178 files changed

+553
-1033
lines changed

src/compiler/diagnosticInformationMap.generated.ts

+2
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ module ts {
153153
External_module_0_has_no_default_export_or_export_assignment: { code: 1192, category: DiagnosticCategory.Error, key: "External module '{0}' has no default export or export assignment." },
154154
An_export_declaration_cannot_have_modifiers: { code: 1193, category: DiagnosticCategory.Error, key: "An export declaration cannot have modifiers." },
155155
Export_declarations_are_not_permitted_in_an_internal_module: { code: 1194, category: DiagnosticCategory.Error, key: "Export declarations are not permitted in an internal module." },
156+
An_extended_Unicode_escape_value_must_be_between_0x0_and_0x10FFFF_inclusive: { code: 1195, category: DiagnosticCategory.Error, key: "An extended Unicode escape value must be between 0x0 and 0x10FFFF inclusive." },
157+
expected: { code: 1196, category: DiagnosticCategory.Error, key: "'}' expected." },
156158
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
157159
Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." },
158160
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },

src/compiler/diagnosticMessages.json

+9-2
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,14 @@
603603
"category": "Error",
604604
"code": 1194
605605
},
606-
606+
"An extended Unicode escape value must be between 0x0 and 0x10FFFF inclusive.": {
607+
"category": "Error",
608+
"code": 1195
609+
},
610+
"'}' expected.": {
611+
"category": "Error",
612+
"code": 1196
613+
},
607614
"Duplicate identifier '{0}'.": {
608615
"category": "Error",
609616
"code": 2300
@@ -1572,7 +1579,7 @@
15721579
"Exported type alias '{0}' has or is using private name '{1}'.": {
15731580
"category": "Error",
15741581
"code": 4081
1575-
},
1582+
},
15761583
"The current host does not support the '{0}' option.": {
15771584
"category": "Error",
15781585
"code": 5001

src/compiler/scanner.ts

+51-4
Original file line numberDiff line numberDiff line change
@@ -774,11 +774,44 @@ module ts {
774774
return "\'";
775775
case CharacterCodes.doubleQuote:
776776
return "\"";
777-
case CharacterCodes.x:
778777
case CharacterCodes.u:
779-
var ch = scanExactNumberOfHexDigits(ch === CharacterCodes.x ? 2 : 4);
780-
if (ch >= 0) {
781-
return String.fromCharCode(ch);
778+
if (text.charCodeAt(pos) === CharacterCodes.openBrace) {
779+
pos++;
780+
var escapedValue = scanMinimumNumberOfHexDigits(1);
781+
782+
if (escapedValue < 0) {
783+
// TODO(drosen): give a proper error message for escaped values that are too large.
784+
error(Diagnostics.Hexadecimal_digit_expected)
785+
return "";
786+
}
787+
788+
if (escapedValue > 0x10FFFF) {
789+
error(Diagnostics.An_extended_Unicode_escape_value_must_be_between_0x0_and_0x10FFFF_inclusive);
790+
return "";
791+
}
792+
793+
if (pos >= len) {
794+
error(Diagnostics.Unexpected_end_of_text);
795+
return "";
796+
}
797+
798+
// Only swallow the following character up if it's a '}'.
799+
var escapeTerminator = text.charCodeAt(pos);
800+
if (escapeTerminator == CharacterCodes.closeBrace) {
801+
pos++;
802+
}
803+
else {
804+
// '}' expected.
805+
error(Diagnostics.expected);
806+
}
807+
808+
return utf16EncodeAsString(escapedValue);
809+
}
810+
// fall through
811+
case CharacterCodes.x:
812+
var escapedValue = scanExactNumberOfHexDigits(ch === CharacterCodes.x ? 2 : 4);
813+
if (escapedValue >= 0) {
814+
return String.fromCharCode(escapedValue);
782815
}
783816
else {
784817
error(Diagnostics.Hexadecimal_digit_expected);
@@ -800,6 +833,20 @@ module ts {
800833
return String.fromCharCode(ch);
801834
}
802835
}
836+
837+
// Derived from the 10.1.1 UTF16Encoding of the ES6 Spec.
838+
function utf16EncodeAsString(codePoint: number): string {
839+
Debug.assert(0x0 <= codePoint && codePoint <= 0x10FFFF);
840+
841+
if (codePoint <= 65535) {
842+
return String.fromCharCode(codePoint);
843+
}
844+
845+
var codeUnit1 = Math.floor((codePoint - 65536) / 1024) + 0xD800;
846+
var codeUnit2 = ((codePoint - 65536) % 1024) + 0xDC00;
847+
848+
return String.fromCharCode(codeUnit1, codeUnit2);
849+
}
803850

804851
// Current character is known to be a backslash. Check for Unicode escape of the form '\uXXXX'
805852
// and return code point value if valid Unicode escape is found. Otherwise return -1.

tests/baselines/reference/taggedTemplateStringsWithUnicodeEscapes.errors.txt

-10
This file was deleted.

tests/baselines/reference/taggedTemplateStringsWithUnicodeEscapes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ function f() {
1111
args[_i - 0] = arguments[_i];
1212
}
1313
}
14-
(_a = ["'{1f4a9}'", "'💩'"], _a.raw = ["'\\u{1f4a9}'", "'\\uD83D\\uDCA9'"], f(_a, " should be converted to "));
14+
(_a = ["'💩'", "'💩'"], _a.raw = ["'\\u{1f4a9}'", "'\\uD83D\\uDCA9'"], f(_a, " should be converted to "));
1515
var _a;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/compiler/taggedTemplateStringsWithUnicodeEscapes.ts ===
2+
function f(...args: any[]) {
3+
>f : (...args: any[]) => void
4+
>args : any[]
5+
}
6+
7+
f `'\u{1f4a9}'${ " should be converted to " }'\uD83D\uDCA9'`;
8+
>f : (...args: any[]) => void
9+

tests/baselines/reference/taggedTemplateStringsWithUnicodeEscapesES6.errors.txt

-10
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/compiler/taggedTemplateStringsWithUnicodeEscapesES6.ts ===
2+
function f(...args: any[]) {
3+
>f : (...args: any[]) => void
4+
>args : any[]
5+
}
6+
7+
f `'\u{1f4a9}'${ " should be converted to " }'\uD83D\uDCA9'`;
8+
>f : (...args: any[]) => void
9+

tests/baselines/reference/unicodeExtendedEscapesInStrings01_ES5.errors.txt

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/conformance/es6/unicodeExtendedEscapes/unicodeExtendedEscapesInStrings01_ES5.ts ===
2+
3+
var x = "\u{0}";
4+
>x : string
5+

tests/baselines/reference/unicodeExtendedEscapesInStrings01_ES6.errors.txt

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/conformance/es6/unicodeExtendedEscapes/unicodeExtendedEscapesInStrings01_ES6.ts ===
2+
3+
var x = "\u{0}";
4+
>x : string
5+

tests/baselines/reference/unicodeExtendedEscapesInStrings02_ES5.errors.txt

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/conformance/es6/unicodeExtendedEscapes/unicodeExtendedEscapesInStrings02_ES5.ts ===
2+
3+
var x = "\u{00}";
4+
>x : string
5+

tests/baselines/reference/unicodeExtendedEscapesInStrings02_ES6.errors.txt

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/conformance/es6/unicodeExtendedEscapes/unicodeExtendedEscapesInStrings02_ES6.ts ===
2+
3+
var x = "\u{00}";
4+
>x : string
5+

tests/baselines/reference/unicodeExtendedEscapesInStrings03_ES5.errors.txt

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/conformance/es6/unicodeExtendedEscapes/unicodeExtendedEscapesInStrings03_ES5.ts ===
2+
3+
var x = "\u{0000}";
4+
>x : string
5+

tests/baselines/reference/unicodeExtendedEscapesInStrings03_ES6.errors.txt

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/conformance/es6/unicodeExtendedEscapes/unicodeExtendedEscapesInStrings03_ES6.ts ===
2+
3+
var x = "\u{0000}";
4+
>x : string
5+

tests/baselines/reference/unicodeExtendedEscapesInStrings04_ES5.errors.txt

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/conformance/es6/unicodeExtendedEscapes/unicodeExtendedEscapesInStrings04_ES5.ts ===
2+
3+
var x = "\u{00000000}";
4+
>x : string
5+

tests/baselines/reference/unicodeExtendedEscapesInStrings04_ES6.errors.txt

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/conformance/es6/unicodeExtendedEscapes/unicodeExtendedEscapesInStrings04_ES6.ts ===
2+
3+
var x = "\u{00000000}";
4+
>x : string
5+

tests/baselines/reference/unicodeExtendedEscapesInStrings05_ES5.errors.txt

-39
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/conformance/es6/unicodeExtendedEscapes/unicodeExtendedEscapesInStrings05_ES5.ts ===
2+
3+
var x = "\u{48}\u{65}\u{6c}\u{6c}\u{6f}\u{20}\u{77}\u{6f}\u{72}\u{6c}\u{64}";
4+
>x : string
5+

tests/baselines/reference/unicodeExtendedEscapesInStrings05_ES6.errors.txt

-39
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/conformance/es6/unicodeExtendedEscapes/unicodeExtendedEscapesInStrings05_ES6.ts ===
2+
3+
var x = "\u{48}\u{65}\u{6c}\u{6c}\u{6f}\u{20}\u{77}\u{6f}\u{72}\u{6c}\u{64}";
4+
>x : string
5+

tests/baselines/reference/unicodeExtendedEscapesInStrings06_ES5.errors.txt

-11
This file was deleted.

0 commit comments

Comments
 (0)