From 5ec68eb0e454cc70f7e339e4a3d23911c61d5833 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Mon, 2 Mar 2015 15:51:23 -0800 Subject: [PATCH] Harden against trees without parent pointers for emitting literals; fix lookahead in text for numeric literal indicators. --- src/compiler/emitter.ts | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index ad6a93d2a9623..b4cba8c615d4b 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -2178,15 +2178,17 @@ module ts { } } - function isBinaryOrOctalIntegerLiteral(text: string): boolean { - if (text.length <= 0) { - return false; - } - - if (text.charCodeAt(1) === CharacterCodes.B || text.charCodeAt(1) === CharacterCodes.b || - text.charCodeAt(1) === CharacterCodes.O || text.charCodeAt(1) === CharacterCodes.o) { - return true; + function isBinaryOrOctalIntegerLiteral(node: LiteralExpression, text: string): boolean { + if (node.kind === SyntaxKind.NumericLiteral && text.length > 1) { + switch (text.charCodeAt(1)) { + case CharacterCodes.b: + case CharacterCodes.B: + case CharacterCodes.o: + case CharacterCodes.O: + return true; + } } + return false; } @@ -2195,13 +2197,15 @@ module ts { ? getDoubleQuotedStringTextOfLiteral(node) : node.parent ? getSourceTextOfNodeFromSourceFile(currentSourceFile, node) - : node.text; // TODO(drosen): Is this correct? + : node.kind === SyntaxKind.NumericLiteral + ? node.text + : getDoubleQuotedStringTextOfLiteral(node); if (compilerOptions.sourceMap && (node.kind === SyntaxKind.StringLiteral || isTemplateLiteralKind(node.kind))) { writer.writeLiteral(text); } - // For version below ES6, emit binary integer literal and octal integer literal in canonical form - else if (languageVersion < ScriptTarget.ES6 && node.kind === SyntaxKind.NumericLiteral && isBinaryOrOctalIntegerLiteral(text)) { + // For versions below ES6, emit binary & octal literals in their canonical decimal form. + else if (languageVersion < ScriptTarget.ES6 && isBinaryOrOctalIntegerLiteral(node, text)) { write(node.text); } else {