Skip to content

Commit

Permalink
Harden against trees without parent pointers for emitting literals; f…
Browse files Browse the repository at this point in the history
…ix lookahead in text for numeric literal indicators.
  • Loading branch information
DanielRosenwasser committed Mar 2, 2015
1 parent 6be13a9 commit 5ec68eb
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -2195,13 +2197,15 @@ module ts {
? getDoubleQuotedStringTextOfLiteral(node)

This comment has been minimized.

Copy link
@CyrusNajmabadi

CyrusNajmabadi Mar 2, 2015

Contributor

This is super confusing. Can you just break it into simple if/else blocks? And comment when each would be hit?

: 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 {
Expand Down

0 comments on commit 5ec68eb

Please sign in to comment.