diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index 46976813120..09b009711fe 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -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." }, }; } \ No newline at end of file diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 4b705396fe5..fee46a84c1e 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -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 } } diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 3fbe640254f..363acf7b651 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -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); diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 55fdcd91d34..14c7d6da455 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -100,8 +100,6 @@ namespace ts { visitNode(cbNode, (node).type) || visitNode(cbNode, (node).equalsGreaterThanToken) || visitNode(cbNode, (node).body); - case SyntaxKind.NumericLiteral: - return visitNode(cbNode, (node).invalidDotExpression); case SyntaxKind.TypeReference: return visitNode(cbNode, (node).typeName) || visitNodes(cbNodes, (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() } } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 187cead4842..40135fe22e4 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -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 { diff --git a/tests/cases/compiler/numLit.ts b/tests/cases/compiler/numLit.ts index d9d42f9018c..abe3505703d 100644 --- a/tests/cases/compiler/numLit.ts +++ b/tests/cases/compiler/numLit.ts @@ -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. \ No newline at end of file +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.