Skip to content

Commit b754cc1

Browse files
committed
Merge branch 'jbondc-minor-3'
2 parents 84ff2f8 + 14f7dd0 commit b754cc1

File tree

5 files changed

+82
-9
lines changed

5 files changed

+82
-9
lines changed

src/compiler/emitter.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1783,7 +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-
write(".");
1786+
1787+
// 1 .toString is a valid property access, emit a space after the literal
1788+
let shouldEmitSpace: boolean;
1789+
if (!indentedBeforeDot && node.expression.kind === SyntaxKind.NumericLiteral) {
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 {
1798+
write(".");
1799+
}
1800+
17871801
let indentedAfterDot = indentIfOnDifferentLines(node, node.dotToken, node.name);
17881802
emit(node.name);
17891803
decreaseIndentIf(indentedBeforeDot, indentedAfterDot);

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1881,7 +1881,7 @@ namespace ts {
18811881
CarriageReturnLineFeed = 0,
18821882
LineFeed = 1,
18831883
}
1884-
1884+
18851885
export interface LineAndCharacter {
18861886
line: number;
18871887
/*
Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
11
tests/cases/compiler/numLit.ts(3,3): error TS1005: ';' expected.
2-
tests/cases/compiler/numLit.ts(3,3): error TS2304: Cannot find name 'toString'.
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.
35

46

5-
==== tests/cases/compiler/numLit.ts (2 errors) ====
7+
==== tests/cases/compiler/numLit.ts (4 errors) ====
68
1..toString();
79
1.0.toString();
810
1.toString();
911
~~~~~~~~
1012
!!! error TS1005: ';' expected.
11-
~~~~~~~~
12-
!!! error TS2304: Cannot find name 'toString'.
13-
1.+2.0 + 3. ;
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: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,37 @@
22
1..toString();
33
1.0.toString();
44
1.toString();
5-
1.+2.0 + 3. ;
5+
1.+2.0 + 3. ;
6+
7+
// Preserve whitespace where important for JS compatibility
8+
var i: number = 1;
9+
var test1 = i.toString();
10+
var test2 = 2.toString(); // emitted as 2 .toString()
11+
var test3 = 3 .toString();
12+
var test4 = 3 .toString();
13+
var test5 = 3 .toString();
14+
var test6 = 3.['toString']();
15+
var test7 = 3
16+
.toString();
17+
var test8 = new Number(4).toString();
18+
var test9 = 3. + 3.
19+
620

721
//// [numLit.js]
822
1..toString();
923
1.0.toString();
1024
1.;
1125
toString();
1226
1. + 2.0 + 3.;
27+
// Preserve whitespace where important for JS compatibility
28+
var i = 1;
29+
var test1 = i.toString();
30+
var test2 = 2., toString = (); // emitted as 2 .toString()
31+
var test3 = 3 .toString();
32+
var test4 = 3 .toString();
33+
var test5 = 3 .toString();
34+
var test6 = 3.['toString']();
35+
var test7 = 3
36+
.toString();
37+
var test8 = new Number(4).toString();
38+
var test9 = 3. + 3.;

tests/cases/compiler/numLit.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
1..toString();
22
1.0.toString();
33
1.toString();
4-
1.+2.0 + 3. ;
4+
1.+2.0 + 3. ;
5+
6+
// Preserve whitespace where important for JS compatibility
7+
var i: number = 1;
8+
var test1 = i.toString();
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)