Skip to content

Commit 14f7dd0

Browse files
committed
Revert changes to the parser and augment check to the emitter to handel '1..toString' case
1 parent 464f655 commit 14f7dd0

File tree

7 files changed

+62
-190
lines changed

7 files changed

+62
-190
lines changed

src/compiler/emitter.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,11 +1783,21 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
17831783

17841784
emit(node.expression);
17851785
let indentedBeforeDot = indentIfOnDifferentLines(node, node.expression, node.dotToken);
1786+
1787+
// 1 .toString is a valid property access, emit a space after the literal
1788+
let shouldEmitSpace: boolean;
17861789
if (!indentedBeforeDot && node.expression.kind === SyntaxKind.NumericLiteral) {
1787-
write(" ."); // JS compat with numeric literal
1788-
} else {
1790+
let text = getSourceTextOfNodeFromSourceFile(currentSourceFile, node.expression);
1791+
shouldEmitSpace = text.indexOf(tokenToString(SyntaxKind.DotToken)) < 0;
1792+
}
1793+
1794+
if (shouldEmitSpace) {
1795+
write(" .");
1796+
}
1797+
else {
17891798
write(".");
17901799
}
1800+
17911801
let indentedAfterDot = indentIfOnDifferentLines(node, node.dotToken, node.name);
17921802
emit(node.name);
17931803
decreaseIndentIf(indentedBeforeDot, indentedAfterDot);

src/compiler/parser.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,30 +1830,20 @@ namespace ts {
18301830
}
18311831

18321832
let tokenPos = scanner.getTokenPos();
1833-
let nextCharCode: number;
1834-
18351833
nextToken();
18361834
finishNode(node);
18371835

1838-
if (node.kind === SyntaxKind.NumericLiteral) {
1839-
// Octal literals are not allowed in strict mode or ES5
1840-
// Note that theoretically the following condition would hold true literals like 009,
1841-
// which is not octal.But because of how the scanner separates the tokens, we would
1842-
// never get a token like this. Instead, we would get 00 and 9 as two separate tokens.
1843-
// We also do not need to check for negatives because any prefix operator would be part of a
1844-
// parent unary expression.
1845-
if (sourceText.charCodeAt(tokenPos) === CharacterCodes._0 && isOctalDigit(sourceText.charCodeAt(tokenPos + 1))) {
1846-
node.flags |= NodeFlags.OctalLiteral;
1847-
}
1848-
else if (token === SyntaxKind.Identifier && sourceText.charCodeAt(node.end-1) === CharacterCodes.dot) {
1849-
nextCharCode = sourceText.charCodeAt(node.end);
1850-
if (!isWhiteSpace(nextCharCode) && !isLineBreak(nextCharCode)) {
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()
1855-
}
1856-
}
1836+
// Octal literals are not allowed in strict mode or ES5
1837+
// Note that theoretically the following condition would hold true literals like 009,
1838+
// which is not octal.But because of how the scanner separates the tokens, we would
1839+
// never get a token like this. Instead, we would get 00 and 9 as two separate tokens.
1840+
// We also do not need to check for negatives because any prefix operator would be part of a
1841+
// parent unary expression.
1842+
if (node.kind === SyntaxKind.NumericLiteral
1843+
&& sourceText.charCodeAt(tokenPos) === CharacterCodes._0
1844+
&& isOctalDigit(sourceText.charCodeAt(tokenPos + 1))) {
1845+
1846+
node.flags |= NodeFlags.OctalLiteral;
18571847
}
18581848

18591849
return node;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
tests/cases/compiler/numLit.ts(3,3): error TS1005: ';' expected.
2+
tests/cases/compiler/numLit.ts(9,15): error TS1005: ',' expected.
3+
tests/cases/compiler/numLit.ts(9,23): error TS1005: '=' expected.
4+
tests/cases/compiler/numLit.ts(9,24): error TS1109: Expression expected.
5+
6+
7+
==== tests/cases/compiler/numLit.ts (4 errors) ====
8+
1..toString();
9+
1.0.toString();
10+
1.toString();
11+
~~~~~~~~
12+
!!! error TS1005: ';' expected.
13+
1.+2.0 + 3. ;
14+
15+
// Preserve whitespace where important for JS compatibility
16+
var i: number = 1;
17+
var test1 = i.toString();
18+
var test2 = 2.toString(); // emitted as 2 .toString()
19+
~~~~~~~~
20+
!!! error TS1005: ',' expected.
21+
~
22+
!!! error TS1005: '=' expected.
23+
~
24+
!!! error TS1109: Expression expected.
25+
var test3 = 3 .toString();
26+
var test4 = 3 .toString();
27+
var test5 = 3 .toString();
28+
var test6 = 3.['toString']();
29+
var test7 = 3
30+
.toString();
31+
var test8 = new Number(4).toString();
32+
var test9 = 3. + 3.
33+

tests/baselines/reference/numLit.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ var test9 = 3. + 3.
1919

2020

2121
//// [numLit.js]
22-
1. .toString();
23-
1.0 .toString();
24-
1 .toString();
22+
1..toString();
23+
1.0.toString();
24+
1.;
25+
toString();
2526
1. + 2.0 + 3.;
2627
// Preserve whitespace where important for JS compatibility
2728
var i = 1;
2829
var test1 = i.toString();
29-
var test2 = 2 .toString(); // emitted as 2 .toString()
30+
var test2 = 2., toString = (); // emitted as 2 .toString()
3031
var test3 = 3 .toString();
3132
var test4 = 3 .toString();
3233
var test5 = 3 .toString();

tests/baselines/reference/numLit.symbols

Lines changed: 0 additions & 65 deletions
This file was deleted.

tests/baselines/reference/numLit.types

Lines changed: 0 additions & 97 deletions
This file was deleted.

tests/baselines/reference/toStringOnPrimitives.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ aBool.toString();
88
true.toString();
99
var aBool = false;
1010
aBool.toString();
11-
1. .toString();
11+
1..toString();

0 commit comments

Comments
 (0)