Don't report an error for 1.toString(), just emit a space for JS compat.

This commit is contained in:
jbondc 2015-06-24 10:56:23 -04:00
parent 138970f35f
commit da1bc6bac8
6 changed files with 19 additions and 28 deletions

View File

@ -569,6 +569,5 @@ namespace ts {
decorators_can_only_be_used_in_a_ts_file: { code: 8017, category: DiagnosticCategory.Error, key: "'decorators' can only be used in a .ts file." },
Only_identifiers_Slashqualified_names_with_optional_type_arguments_are_currently_supported_in_a_class_extends_clauses: { code: 9002, category: DiagnosticCategory.Error, key: "Only identifiers/qualified-names with optional type arguments are currently supported in a class 'extends' clauses." },
class_expressions_are_not_currently_supported: { code: 9003, category: DiagnosticCategory.Error, key: "'class' expressions are not currently supported." },
Numeric_literal_0_cannot_be_followed_by_an_expression: { code: 9004, category: DiagnosticCategory.Error, key: "Numeric literal {0} cannot be followed by an expression." },
};
}

View File

@ -2268,9 +2268,5 @@
"'class' expressions are not currently supported.": {
"category": "Error",
"code": 9003
},
"Numeric literal {0} cannot be followed by an expression.": {
"category": "Error",
"code": 9004
}
}

View File

@ -1778,7 +1778,11 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
emit(node.expression);
let indentedBeforeDot = indentIfOnDifferentLines(node, node.expression, node.dotToken);
write(".");
if (!indentedBeforeDot && node.expression.kind === SyntaxKind.NumericLiteral) {
write(" ."); // JS compat with numeric literal
} else {
write(".");
}
let indentedAfterDot = indentIfOnDifferentLines(node, node.dotToken, node.name);
emit(node.name);
decreaseIndentIf(indentedBeforeDot, indentedAfterDot);

View File

@ -100,8 +100,6 @@ namespace ts {
visitNode(cbNode, (<FunctionLikeDeclaration>node).type) ||
visitNode(cbNode, (<ArrowFunction>node).equalsGreaterThanToken) ||
visitNode(cbNode, (<FunctionLikeDeclaration>node).body);
case SyntaxKind.NumericLiteral:
return visitNode(cbNode, (<LiteralExpression>node).invalidDotExpression);
case SyntaxKind.TypeReference:
return visitNode(cbNode, (<TypeReferenceNode>node).typeName) ||
visitNodes(cbNodes, (<TypeReferenceNode>node).typeArguments);
@ -1847,20 +1845,13 @@ namespace ts {
if (sourceText.charCodeAt(tokenPos) === CharacterCodes._0 && isOctalDigit(sourceText.charCodeAt(tokenPos + 1))) {
node.flags |= NodeFlags.OctalLiteral;
}
else if (token === SyntaxKind.DotToken) {
// Issue #2632 Keep space for 3 .toString()
if (isWhiteSpace(sourceText.charCodeAt(node.end))) {
node.end++;
scanner.setTextPos(node.end + 1);
}
}
else if (token === SyntaxKind.Identifier && sourceText.charCodeAt(node.end-1) === CharacterCodes.dot) {
nextCharCode = sourceText.charCodeAt(node.end);
if (!isWhiteSpace(nextCharCode) && !isLineBreak(nextCharCode)) {
// Eat up an invalid expression following '1.' e.g. 1.something()
node.invalidDotExpression = parseLeftHandSideExpressionOrHigher();
parseErrorAtPosition(node.end, node.invalidDotExpression.end - node.end, Diagnostics.Numeric_literal_0_cannot_be_followed_by_an_expression, "'" + sourceText.substring(tokenPos, node.end) + "'");
node.end = node.invalidDotExpression.end;
// If we see 23.Identifier, go back 1 char to scan .Identifier
node.end = node.end - 1;
scanner.setTextPos(node.end);
nextToken()
}
}
}

View File

@ -737,7 +737,6 @@ namespace ts {
text: string;
isUnterminated?: boolean;
hasExtendedUnicodeEscape?: boolean;
invalidDotExpression?: LeftHandSideExpression; // 1.toString() we attach the node but never emit it
}
export interface TemplateExpression extends PrimaryExpression {

View File

@ -1,15 +1,17 @@
1..toString();
1.0.toString();
1.toString(); // error: Numeric literal '1.' cannot be followed by an expression.
1.toString();
1.+2.0 + 3. ;
// Preserve whitespace where important for JS compatibility
var i: number = 1;
var test1 = i.toString();
var test2 = 2.toString(); // error: Numeric literal '2.' cannot be followed by an expression.
var test3 = 3 .toString(); // preserve whitepace
var test4 = 3.['toString']();
var test5 = 3
.toString(); // preserve whitepace
var test6 = new Number(4).toString();
var test7 = 3. + 3.
var test2 = 2.toString(); // emitted as 2 .toString()
var test3 = 3 .toString();
var test4 = 3 .toString();
var test5 = 3 .toString();
var test6 = 3.['toString']();
var test7 = 3
.toString();
var test8 = new Number(4).toString();
var test9 = 3. + 3.