Skip to content

Commit 5ec68eb

Browse files
Harden against trees without parent pointers for emitting literals; fix lookahead in text for numeric literal indicators.
1 parent 6be13a9 commit 5ec68eb

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/compiler/emitter.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -2178,15 +2178,17 @@ module ts {
21782178
}
21792179
}
21802180

2181-
function isBinaryOrOctalIntegerLiteral(text: string): boolean {
2182-
if (text.length <= 0) {
2183-
return false;
2184-
}
2185-
2186-
if (text.charCodeAt(1) === CharacterCodes.B || text.charCodeAt(1) === CharacterCodes.b ||
2187-
text.charCodeAt(1) === CharacterCodes.O || text.charCodeAt(1) === CharacterCodes.o) {
2188-
return true;
2181+
function isBinaryOrOctalIntegerLiteral(node: LiteralExpression, text: string): boolean {
2182+
if (node.kind === SyntaxKind.NumericLiteral && text.length > 1) {
2183+
switch (text.charCodeAt(1)) {
2184+
case CharacterCodes.b:
2185+
case CharacterCodes.B:
2186+
case CharacterCodes.o:
2187+
case CharacterCodes.O:
2188+
return true;
2189+
}
21892190
}
2191+
21902192
return false;
21912193
}
21922194

@@ -2195,13 +2197,15 @@ module ts {
21952197
? getDoubleQuotedStringTextOfLiteral(node)
21962198
: node.parent
21972199
? getSourceTextOfNodeFromSourceFile(currentSourceFile, node)
2198-
: node.text; // TODO(drosen): Is this correct?
2200+
: node.kind === SyntaxKind.NumericLiteral
2201+
? node.text
2202+
: getDoubleQuotedStringTextOfLiteral(node);
21992203

22002204
if (compilerOptions.sourceMap && (node.kind === SyntaxKind.StringLiteral || isTemplateLiteralKind(node.kind))) {
22012205
writer.writeLiteral(text);
22022206
}
2203-
// For version below ES6, emit binary integer literal and octal integer literal in canonical form
2204-
else if (languageVersion < ScriptTarget.ES6 && node.kind === SyntaxKind.NumericLiteral && isBinaryOrOctalIntegerLiteral(text)) {
2207+
// For versions below ES6, emit binary & octal literals in their canonical decimal form.
2208+
else if (languageVersion < ScriptTarget.ES6 && isBinaryOrOctalIntegerLiteral(node, text)) {
22052209
write(node.text);
22062210
}
22072211
else {

0 commit comments

Comments
 (0)