Skip to content

Commit da1bc6b

Browse files
author
jbondc
committed
Don't report an error for 1.toString(), just emit a space for JS compat.
1 parent 138970f commit da1bc6b

File tree

6 files changed

+19
-28
lines changed

6 files changed

+19
-28
lines changed

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,5 @@ namespace ts {
569569
decorators_can_only_be_used_in_a_ts_file: { code: 8017, category: DiagnosticCategory.Error, key: "'decorators' can only be used in a .ts file." },
570570
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." },
571571
class_expressions_are_not_currently_supported: { code: 9003, category: DiagnosticCategory.Error, key: "'class' expressions are not currently supported." },
572-
Numeric_literal_0_cannot_be_followed_by_an_expression: { code: 9004, category: DiagnosticCategory.Error, key: "Numeric literal {0} cannot be followed by an expression." },
573572
};
574573
}

src/compiler/diagnosticMessages.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,9 +2268,5 @@
22682268
"'class' expressions are not currently supported.": {
22692269
"category": "Error",
22702270
"code": 9003
2271-
},
2272-
"Numeric literal {0} cannot be followed by an expression.": {
2273-
"category": "Error",
2274-
"code": 9004
22752271
}
22762272
}

src/compiler/emitter.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1778,7 +1778,11 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
17781778

17791779
emit(node.expression);
17801780
let indentedBeforeDot = indentIfOnDifferentLines(node, node.expression, node.dotToken);
1781-
write(".");
1781+
if (!indentedBeforeDot && node.expression.kind === SyntaxKind.NumericLiteral) {
1782+
write(" ."); // JS compat with numeric literal
1783+
} else {
1784+
write(".");
1785+
}
17821786
let indentedAfterDot = indentIfOnDifferentLines(node, node.dotToken, node.name);
17831787
emit(node.name);
17841788
decreaseIndentIf(indentedBeforeDot, indentedAfterDot);

src/compiler/parser.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ namespace ts {
100100
visitNode(cbNode, (<FunctionLikeDeclaration>node).type) ||
101101
visitNode(cbNode, (<ArrowFunction>node).equalsGreaterThanToken) ||
102102
visitNode(cbNode, (<FunctionLikeDeclaration>node).body);
103-
case SyntaxKind.NumericLiteral:
104-
return visitNode(cbNode, (<LiteralExpression>node).invalidDotExpression);
105103
case SyntaxKind.TypeReference:
106104
return visitNode(cbNode, (<TypeReferenceNode>node).typeName) ||
107105
visitNodes(cbNodes, (<TypeReferenceNode>node).typeArguments);
@@ -1847,20 +1845,13 @@ namespace ts {
18471845
if (sourceText.charCodeAt(tokenPos) === CharacterCodes._0 && isOctalDigit(sourceText.charCodeAt(tokenPos + 1))) {
18481846
node.flags |= NodeFlags.OctalLiteral;
18491847
}
1850-
else if (token === SyntaxKind.DotToken) {
1851-
// Issue #2632 Keep space for 3 .toString()
1852-
if (isWhiteSpace(sourceText.charCodeAt(node.end))) {
1853-
node.end++;
1854-
scanner.setTextPos(node.end + 1);
1855-
}
1856-
}
18571848
else if (token === SyntaxKind.Identifier && sourceText.charCodeAt(node.end-1) === CharacterCodes.dot) {
18581849
nextCharCode = sourceText.charCodeAt(node.end);
18591850
if (!isWhiteSpace(nextCharCode) && !isLineBreak(nextCharCode)) {
1860-
// Eat up an invalid expression following '1.' e.g. 1.something()
1861-
node.invalidDotExpression = parseLeftHandSideExpressionOrHigher();
1862-
parseErrorAtPosition(node.end, node.invalidDotExpression.end - node.end, Diagnostics.Numeric_literal_0_cannot_be_followed_by_an_expression, "'" + sourceText.substring(tokenPos, node.end) + "'");
1863-
node.end = node.invalidDotExpression.end;
1851+
// If we see 23.Identifier, go back 1 char to scan .Identifier
1852+
node.end = node.end - 1;
1853+
scanner.setTextPos(node.end);
1854+
nextToken()
18641855
}
18651856
}
18661857
}

src/compiler/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,6 @@ namespace ts {
737737
text: string;
738738
isUnterminated?: boolean;
739739
hasExtendedUnicodeEscape?: boolean;
740-
invalidDotExpression?: LeftHandSideExpression; // 1.toString() we attach the node but never emit it
741740
}
742741

743742
export interface TemplateExpression extends PrimaryExpression {

tests/cases/compiler/numLit.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
1..toString();
22
1.0.toString();
3-
1.toString(); // error: Numeric literal '1.' cannot be followed by an expression.
3+
1.toString();
44
1.+2.0 + 3. ;
55

66
// Preserve whitespace where important for JS compatibility
77
var i: number = 1;
88
var test1 = i.toString();
9-
var test2 = 2.toString(); // error: Numeric literal '2.' cannot be followed by an expression.
10-
var test3 = 3 .toString(); // preserve whitepace
11-
var test4 = 3.['toString']();
12-
var test5 = 3
13-
.toString(); // preserve whitepace
14-
var test6 = new Number(4).toString();
15-
var test7 = 3. + 3.
9+
var test2 = 2.toString(); // emitted as 2 .toString()
10+
var test3 = 3 .toString();
11+
var test4 = 3 .toString();
12+
var test5 = 3 .toString();
13+
var test6 = 3.['toString']();
14+
var test7 = 3
15+
.toString();
16+
var test8 = new Number(4).toString();
17+
var test9 = 3. + 3.

0 commit comments

Comments
 (0)