Skip to content

Commit

Permalink
Emit all strings with extended escapes using the same scheme as templ…
Browse files Browse the repository at this point in the history
…ates.
  • Loading branch information
DanielRosenwasser committed Feb 27, 2015
1 parent 9d89668 commit 4657c2d
Show file tree
Hide file tree
Showing 37 changed files with 74 additions and 29 deletions.
11 changes: 7 additions & 4 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2187,9 +2187,12 @@ module ts {
}

function emitLiteral(node: LiteralExpression) {
var text = languageVersion < ScriptTarget.ES6 && isTemplateLiteralKind(node.kind) ? getTemplateLiteralAsStringLiteral(node) :
node.parent ? getSourceTextOfNodeFromSourceFile(currentSourceFile, node) :
node.text;
var text = languageVersion < ScriptTarget.ES6 && (isTemplateLiteralKind(node.kind) || node.hasExtendedUnicodeEscape)
? getDoubleQuotedStringTextOfLiteral(node)
: node.parent
? getSourceTextOfNodeFromSourceFile(currentSourceFile, node)
: node.text; // TODO(drosen): Is this correct?

This comment has been minimized.

Copy link
@yuit

yuit Feb 28, 2015

Contributor

What is the TODO comment suppose to mean?


if (compilerOptions.sourceMap && (node.kind === SyntaxKind.StringLiteral || isTemplateLiteralKind(node.kind))) {
writer.writeLiteral(text);
}
Expand All @@ -2202,7 +2205,7 @@ module ts {
}
}

function getTemplateLiteralAsStringLiteral(node: LiteralExpression): string {
function getDoubleQuotedStringTextOfLiteral(node: LiteralExpression): string {
var result = escapeString(node.text);
result = replaceNonAsciiCharacters(result);

Expand Down
4 changes: 4 additions & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2164,6 +2164,10 @@ module ts {
var text = scanner.getTokenValue();
node.text = internName ? internIdentifier(text) : text;

if (scanner.hasExtendedUnicodeEscape()) {
node.hasExtendedUnicodeEscape = true;
}

if (scanner.isUnterminated()) {
node.isUnterminated = true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module ts {
getTokenPos(): number;
getTokenText(): string;
getTokenValue(): string;
hasExtendedUnicodeEscape(): boolean;
hasPrecedingLineBreak(): boolean;
isIdentifier(): boolean;
isReservedWord(): boolean;
Expand Down Expand Up @@ -556,6 +557,7 @@ module ts {
var token: SyntaxKind;
var tokenValue: string;
var precedingLineBreak: boolean;
var hasExtendedUnicodeEscape: boolean;
var tokenIsUnterminated: boolean;

function error(message: DiagnosticMessage, length?: number): void {
Expand Down Expand Up @@ -776,6 +778,7 @@ module ts {
return "\"";
case CharacterCodes.u:
if (text.charCodeAt(pos) === CharacterCodes.openBrace) {
hasExtendedUnicodeEscape = true;
pos++;
var escapedValue = scanMinimumNumberOfHexDigits(1);

Expand Down Expand Up @@ -926,6 +929,7 @@ module ts {

function scan(): SyntaxKind {
startPos = pos;
hasExtendedUnicodeEscape = false;
precedingLineBreak = false;
tokenIsUnterminated = false;
while (true) {
Expand Down Expand Up @@ -1393,6 +1397,7 @@ module ts {
getTokenPos: () => tokenPos,
getTokenText: () => text.substring(tokenPos, pos),
getTokenValue: () => tokenValue,
hasExtendedUnicodeEscape: () => hasExtendedUnicodeEscape,
hasPrecedingLineBreak: () => precedingLineBreak,
isIdentifier: () => token === SyntaxKind.Identifier || token > SyntaxKind.LastReservedWord,
isReservedWord: () => token >= SyntaxKind.FirstReservedWord && token <= SyntaxKind.LastReservedWord,
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ module ts {
export interface LiteralExpression extends PrimaryExpression {
text: string;
isUnterminated?: boolean;
hasExtendedUnicodeEscape?: boolean;
}

export interface StringLiteralExpression extends LiteralExpression {
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/APISample_compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ declare module "typescript" {
interface LiteralExpression extends PrimaryExpression {
text: string;
isUnterminated?: boolean;
hasExtendedUnicodeEscape?: boolean;
}
interface StringLiteralExpression extends LiteralExpression {
_stringLiteralExpressionBrand: any;
Expand Down Expand Up @@ -1421,6 +1422,7 @@ declare module "typescript" {
getTokenPos(): number;
getTokenText(): string;
getTokenValue(): string;
hasExtendedUnicodeEscape(): boolean;
hasPrecedingLineBreak(): boolean;
isIdentifier(): boolean;
isReservedWord(): boolean;
Expand Down
6 changes: 6 additions & 0 deletions tests/baselines/reference/APISample_compile.types
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,9 @@ declare module "typescript" {

isUnterminated?: boolean;
>isUnterminated : boolean

hasExtendedUnicodeEscape?: boolean;
>hasExtendedUnicodeEscape : boolean
}
interface StringLiteralExpression extends LiteralExpression {
>StringLiteralExpression : StringLiteralExpression
Expand Down Expand Up @@ -4481,6 +4484,9 @@ declare module "typescript" {
getTokenValue(): string;
>getTokenValue : () => string

hasExtendedUnicodeEscape(): boolean;
>hasExtendedUnicodeEscape : () => boolean

hasPrecedingLineBreak(): boolean;
>hasPrecedingLineBreak : () => boolean

Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/APISample_linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ declare module "typescript" {
interface LiteralExpression extends PrimaryExpression {
text: string;
isUnterminated?: boolean;
hasExtendedUnicodeEscape?: boolean;
}
interface StringLiteralExpression extends LiteralExpression {
_stringLiteralExpressionBrand: any;
Expand Down Expand Up @@ -1452,6 +1453,7 @@ declare module "typescript" {
getTokenPos(): number;
getTokenText(): string;
getTokenValue(): string;
hasExtendedUnicodeEscape(): boolean;
hasPrecedingLineBreak(): boolean;
isIdentifier(): boolean;
isReservedWord(): boolean;
Expand Down
6 changes: 6 additions & 0 deletions tests/baselines/reference/APISample_linter.types
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,9 @@ declare module "typescript" {

isUnterminated?: boolean;
>isUnterminated : boolean

hasExtendedUnicodeEscape?: boolean;
>hasExtendedUnicodeEscape : boolean
}
interface StringLiteralExpression extends LiteralExpression {
>StringLiteralExpression : StringLiteralExpression
Expand Down Expand Up @@ -4627,6 +4630,9 @@ declare module "typescript" {
getTokenValue(): string;
>getTokenValue : () => string

hasExtendedUnicodeEscape(): boolean;
>hasExtendedUnicodeEscape : () => boolean

hasPrecedingLineBreak(): boolean;
>hasPrecedingLineBreak : () => boolean

Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/APISample_transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ declare module "typescript" {
interface LiteralExpression extends PrimaryExpression {
text: string;
isUnterminated?: boolean;
hasExtendedUnicodeEscape?: boolean;
}
interface StringLiteralExpression extends LiteralExpression {
_stringLiteralExpressionBrand: any;
Expand Down Expand Up @@ -1453,6 +1454,7 @@ declare module "typescript" {
getTokenPos(): number;
getTokenText(): string;
getTokenValue(): string;
hasExtendedUnicodeEscape(): boolean;
hasPrecedingLineBreak(): boolean;
isIdentifier(): boolean;
isReservedWord(): boolean;
Expand Down
6 changes: 6 additions & 0 deletions tests/baselines/reference/APISample_transform.types
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,9 @@ declare module "typescript" {

isUnterminated?: boolean;
>isUnterminated : boolean

hasExtendedUnicodeEscape?: boolean;
>hasExtendedUnicodeEscape : boolean
}
interface StringLiteralExpression extends LiteralExpression {
>StringLiteralExpression : StringLiteralExpression
Expand Down Expand Up @@ -4577,6 +4580,9 @@ declare module "typescript" {
getTokenValue(): string;
>getTokenValue : () => string

hasExtendedUnicodeEscape(): boolean;
>hasExtendedUnicodeEscape : () => boolean

hasPrecedingLineBreak(): boolean;
>hasPrecedingLineBreak : () => boolean

Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/APISample_watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ declare module "typescript" {
interface LiteralExpression extends PrimaryExpression {
text: string;
isUnterminated?: boolean;
hasExtendedUnicodeEscape?: boolean;
}
interface StringLiteralExpression extends LiteralExpression {
_stringLiteralExpressionBrand: any;
Expand Down Expand Up @@ -1490,6 +1491,7 @@ declare module "typescript" {
getTokenPos(): number;
getTokenText(): string;
getTokenValue(): string;
hasExtendedUnicodeEscape(): boolean;
hasPrecedingLineBreak(): boolean;
isIdentifier(): boolean;
isReservedWord(): boolean;
Expand Down
6 changes: 6 additions & 0 deletions tests/baselines/reference/APISample_watcher.types
Original file line number Diff line number Diff line change
Expand Up @@ -1933,6 +1933,9 @@ declare module "typescript" {

isUnterminated?: boolean;
>isUnterminated : boolean

hasExtendedUnicodeEscape?: boolean;
>hasExtendedUnicodeEscape : boolean
}
interface StringLiteralExpression extends LiteralExpression {
>StringLiteralExpression : StringLiteralExpression
Expand Down Expand Up @@ -4750,6 +4753,9 @@ declare module "typescript" {
getTokenValue(): string;
>getTokenValue : () => string

hasExtendedUnicodeEscape(): boolean;
>hasExtendedUnicodeEscape : () => boolean

hasPrecedingLineBreak(): boolean;
>hasPrecedingLineBreak : () => boolean

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ var x = "\u{0}";


//// [unicodeExtendedEscapesInStrings01_ES5.js]
var x = "\u{0}";
var x = "\0";
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ var x = "\u{00}";


//// [unicodeExtendedEscapesInStrings02_ES5.js]
var x = "\u{00}";
var x = "\0";
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ var x = "\u{0000}";


//// [unicodeExtendedEscapesInStrings03_ES5.js]
var x = "\u{0000}";
var x = "\0";
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ var x = "\u{00000000}";


//// [unicodeExtendedEscapesInStrings04_ES5.js]
var x = "\u{00000000}";
var x = "\0";
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ var x = "\u{48}\u{65}\u{6c}\u{6c}\u{6f}\u{20}\u{77}\u{6f}\u{72}\u{6c}\u{64}";


//// [unicodeExtendedEscapesInStrings05_ES5.js]
var x = "\u{48}\u{65}\u{6c}\u{6c}\u{6f}\u{20}\u{77}\u{6f}\u{72}\u{6c}\u{64}";
var x = "Hello world";
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ var x = "\u{10FFFF}";
//// [unicodeExtendedEscapesInStrings06_ES5.js]
// ES6 Spec - 10.1.1 Static Semantics: UTF16Encoding (cp)
// 1. Assert: 0 ≤ cp ≤ 0x10FFFF.
var x = "\u{10FFFF}";
var x = "\uDBFF\uDFFF";
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ var x = "\u{110000}";
//// [unicodeExtendedEscapesInStrings07_ES5.js]
// ES6 Spec - 10.1.1 Static Semantics: UTF16Encoding (cp)
// 1. Assert: 0 ≤ cp ≤ 0x10FFFF.
var x = "\u{110000}";
var x = "}";
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ var x = "\u{FFFF}";
// ES6 Spec - 10.1.1 Static Semantics: UTF16Encoding (cp)
// 2. If cp ≤ 65535, return cp.
// (FFFF == 65535)
var x = "\u{FFFF}";
var x = "\uFFFF";
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ var x = "\u{10000}";
// ES6 Spec - 10.1.1 Static Semantics: UTF16Encoding (cp)
// 2. If cp ≤ 65535, return cp.
// (10000 == 65536)
var x = "\u{10000}";
var x = "\uD800\uDC00";
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ var x = "\u{D800}";
// 2. Let cu1 be floor((cp – 65536) / 1024) + 0xD800.
// Although we should just get back a single code point value of 0xD800,
// this is a useful edge-case test.
var x = "\u{D800}";
var x = "\uD800";
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ var x = "\u{DC00}";
// 2. Let cu2 be ((cp – 65536) modulo 1024) + 0xDC00.
// Although we should just get back a single code point value of 0xDC00,
// this is a useful edge-case test.
var x = "\u{DC00}";
var x = "\uDC00";
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ var x = "\u{FFFFFFFF}";


//// [unicodeExtendedEscapesInStrings12_ES5.js]
var x = "\u{FFFFFFFF}";
var x = "}";
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ var x = "\u{DDDDD}";


//// [unicodeExtendedEscapesInStrings13_ES5.js]
var x = "\u{DDDDD}";
var x = "\uDB37\uDDDD";
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ var x = "\u{-DDDD}";

//// [unicodeExtendedEscapesInStrings14_ES5.js]
// Shouldn't work, negatives are not allowed.
var x = "\u{-DDDD}";
var x = "-DDDD}";
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ var x = "\u{abcd}\u{ef12}\u{3456}\u{7890}";


//// [unicodeExtendedEscapesInStrings15_ES5.js]
var x = "\u{abcd}\u{ef12}\u{3456}\u{7890}";
var x = "\uABCD\uEF12\u3456\u7890";
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ var x = "\u{ABCD}\u{EF12}\u{3456}\u{7890}";


//// [unicodeExtendedEscapesInStrings16_ES5.js]
var x = "\u{ABCD}\u{EF12}\u{3456}\u{7890}";
var x = "\uABCD\uEF12\u3456\u7890";
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ var x = "\u{r}\u{n}\u{t}";


//// [unicodeExtendedEscapesInStrings17_ES5.js]
var x = "\u{r}\u{n}\u{t}";
var x = "r}n}t}";
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ var x = "\u{65}\u{65}";


//// [unicodeExtendedEscapesInStrings18_ES5.js]
var x = "\u{65}\u{65}";
var x = "ee";
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ var x = "\u{}";


//// [unicodeExtendedEscapesInStrings19_ES5.js]
var x = "\u{}";
var x = "}";
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ var x = "\u{";


//// [unicodeExtendedEscapesInStrings20_ES5.js]
var x = "\u{";
var x = "";
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ var x = "\u{67";


//// [unicodeExtendedEscapesInStrings21_ES5.js]
var x = "\u{67";
var x = "g";
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ var x = "\u{00000000000067";


//// [unicodeExtendedEscapesInStrings22_ES5.js]
var x = "\u{00000000000067";
var x = "g";
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ var x = "\u{00000000000067}";


//// [unicodeExtendedEscapesInStrings23_ES5.js]
var x = "\u{00000000000067}";
var x = "g";
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ var x = "\u{00000000000067


//// [unicodeExtendedEscapesInStrings24_ES5.js]
var x = "\u{00000000000067;
var x = "g";
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ var x = "\u{00000000000067}


//// [unicodeExtendedEscapesInStrings25_ES5.js]
var x = "\u{00000000000067};
var x = "g";

0 comments on commit 4657c2d

Please sign in to comment.