Merge pull request #13661 from Microsoft/fix13646

More exhaustive needsDotDotForPropertyAccess for integer literals
This commit is contained in:
Ron Buckton
2017-01-26 12:40:21 -08:00
committed by GitHub
10 changed files with 199 additions and 5 deletions

View File

@@ -1054,9 +1054,11 @@ namespace ts {
// Also emit a dot if expression is a integer const enum value - it will appear in generated code as numeric literal
function needsDotDotForPropertyAccess(expression: Expression) {
if (expression.kind === SyntaxKind.NumericLiteral) {
// check if numeric literal was originally written with a dot
// check if numeric literal is a decimal literal that was originally written with a dot
const text = getLiteralTextOfNode(<LiteralExpression>expression);
return text.indexOf(tokenToString(SyntaxKind.DotToken)) < 0;
return getNumericLiteralFlags(text, /*hint*/ NumericLiteralFlags.All) === NumericLiteralFlags.None
&& !(<LiteralExpression>expression).isOctalLiteral
&& text.indexOf(tokenToString(SyntaxKind.DotToken)) < 0;
}
else if (isPropertyAccessExpression(expression) || isElementAccessExpression(expression)) {
// check if constant enum value is integer

View File

@@ -341,16 +341,52 @@ namespace ts {
}
export function isBinaryOrOctalIntegerLiteral(node: LiteralLikeNode, text: string) {
if (node.kind === SyntaxKind.NumericLiteral && text.length > 1) {
return node.kind === SyntaxKind.NumericLiteral
&& (getNumericLiteralFlags(text, /*hint*/ NumericLiteralFlags.BinaryOrOctal) & NumericLiteralFlags.BinaryOrOctal) !== 0;
}
export const enum NumericLiteralFlags {
None = 0,
Hexadecimal = 1 << 0,
Binary = 1 << 1,
Octal = 1 << 2,
Scientific = 1 << 3,
BinaryOrOctal = Binary | Octal,
BinaryOrOctalOrHexadecimal = BinaryOrOctal | Hexadecimal,
All = Hexadecimal | Binary | Octal | Scientific,
}
/**
* Scans a numeric literal string to determine the form of the number.
* @param text Numeric literal text
* @param hint If `Scientific` or `All` is specified, performs a more expensive check to scan for scientific notation.
*/
export function getNumericLiteralFlags(text: string, hint?: NumericLiteralFlags) {
if (text.length > 1) {
switch (text.charCodeAt(1)) {
case CharacterCodes.b:
case CharacterCodes.B:
return NumericLiteralFlags.Binary;
case CharacterCodes.o:
case CharacterCodes.O:
return true;
return NumericLiteralFlags.Octal;
case CharacterCodes.x:
case CharacterCodes.X:
return NumericLiteralFlags.Hexadecimal;
}
if (hint & NumericLiteralFlags.Scientific) {
for (let i = text.length - 1; i >= 0; i--) {
switch (text.charCodeAt(i)) {
case CharacterCodes.e:
case CharacterCodes.E:
return NumericLiteralFlags.Scientific;
}
}
}
}
return false;
return NumericLiteralFlags.None;
}
function getQuotedEscapedLiteralText(leftQuote: string, text: string, rightQuote: string) {