Skip to content

Commit a211184

Browse files
author
Josh Goldberg
committed
Clarified error message; extended error to identifier end
Lengthening the reported error length to include all of the identifier necessitates scanning for all of the identifier. I also reset the `pos` after so other identifier scanning still happens.
1 parent 31fca3a commit a211184

13 files changed

+241
-50
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@
10111011
"category": "Message",
10121012
"code": 1350
10131013
},
1014-
"An identifier cannot follow a numeric literal.": {
1014+
"An identifier or keyword cannot immediately follow a numeric literal.": {
10151015
"category": "Error",
10161016
"code": 1351
10171017
},

src/compiler/scanner.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,9 +991,14 @@ namespace ts {
991991
}
992992

993993
function checkForIdentifierStartAfterNumericLiteral() {
994-
if (isIdentifierStart(text.charCodeAt(pos), languageVersion)) {
995-
error(Diagnostics.An_identifier_cannot_follow_a_numeric_literal, pos, 1);
994+
if (!isIdentifierStart(text.charCodeAt(pos), languageVersion)) {
995+
return;
996996
}
997+
998+
const identifierStart = pos;
999+
const { length } = scanIdentifierParts();
1000+
error(Diagnostics.An_identifier_or_keyword_cannot_immediately_follow_a_numeric_literal, identifierStart, length);
1001+
pos = identifierStart;
9971002
}
9981003

9991004
function scanOctalDigits(): number {
Lines changed: 93 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,135 @@
1-
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(1,16): error TS1351: An identifier cannot follow a numeric literal.
2-
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(3,2): error TS1351: An identifier cannot follow a numeric literal.
1+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(1,16): error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
2+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(3,2): error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
33
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(3,2): error TS2304: Cannot find name 'a'.
4-
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(4,3): error TS1124: Digit expected.
5-
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(4,4): error TS2538: Type 'null' cannot be used as an index type.
6-
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(5,2): error TS1351: An identifier cannot follow a numeric literal.
7-
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(6,3): error TS1124: Digit expected.
8-
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(6,3): error TS2304: Cannot find name 'n'.
9-
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(7,2): error TS1351: An identifier cannot follow a numeric literal.
10-
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(7,2): error TS2304: Cannot find name 'a'.
11-
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(8,3): error TS1124: Digit expected.
12-
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(10,3): error TS1124: Digit expected.
13-
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(10,3): error TS2304: Cannot find name 'e'.
14-
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(12,3): error TS1005: ';' expected.
15-
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(13,3): error TS1351: An identifier cannot follow a numeric literal.
16-
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(13,3): error TS2304: Cannot find name 'a'.
4+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(4,4): error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
5+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(4,4): error TS2304: Cannot find name 'a'.
6+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(5,3): error TS1124: Digit expected.
7+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(5,4): error TS2538: Type 'null' cannot be used as an index type.
8+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(6,5): error TS1124: Digit expected.
9+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(6,6): error TS2538: Type 'null' cannot be used as an index type.
10+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(7,2): error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
11+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(8,4): error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
12+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(9,3): error TS1124: Digit expected.
13+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(9,3): error TS2304: Cannot find name 'n'.
14+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(10,5): error TS1124: Digit expected.
15+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(10,5): error TS2304: Cannot find name 'n'.
16+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(11,2): error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
17+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(11,2): error TS2304: Cannot find name 'a'.
18+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(12,4): error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
19+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(12,4): error TS2304: Cannot find name 'a'.
20+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(13,4): error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
21+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(13,4): error TS2304: Cannot find name 'abc'.
22+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(14,3): error TS1124: Digit expected.
23+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(15,5): error TS1124: Digit expected.
24+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(18,3): error TS1124: Digit expected.
25+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(18,3): error TS2304: Cannot find name 'e'.
26+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(19,5): error TS1124: Digit expected.
27+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(19,5): error TS2304: Cannot find name 'e'.
28+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(22,3): error TS1005: ';' expected.
29+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(23,5): error TS1005: ';' expected.
30+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(24,3): error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
31+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(24,3): error TS2304: Cannot find name 'a'.
32+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(25,5): error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
33+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(25,5): error TS2304: Cannot find name 'a'.
34+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(26,5): error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
35+
tests/cases/compiler/identifierStartAfterNumericLiteral.ts(26,5): error TS2304: Cannot find name 'abc'.
1736

1837

19-
==== tests/cases/compiler/identifierStartAfterNumericLiteral.ts (16 errors) ====
38+
==== tests/cases/compiler/identifierStartAfterNumericLiteral.ts (35 errors) ====
2039
let valueIn = 3in[null];
21-
~
22-
!!! error TS1351: An identifier cannot follow a numeric literal.
40+
~~
41+
!!! error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
2342

2443
3a[null]
2544
~
26-
!!! error TS1351: An identifier cannot follow a numeric literal.
45+
!!! error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
2746
~
47+
!!! error TS2304: Cannot find name 'a'.
48+
123a[null]
49+
~
50+
!!! error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
51+
~
2852
!!! error TS2304: Cannot find name 'a'.
2953
3e[null]
3054

3155
!!! error TS1124: Digit expected.
3256
~~~~
57+
!!! error TS2538: Type 'null' cannot be used as an index type.
58+
123e[null]
59+
60+
!!! error TS1124: Digit expected.
61+
~~~~
3362
!!! error TS2538: Type 'null' cannot be used as an index type.
3463
3in[null]
35-
~
36-
!!! error TS1351: An identifier cannot follow a numeric literal.
64+
~~
65+
!!! error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
66+
123in[null]
67+
~~
68+
!!! error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
3769
3en[null]
3870

3971
!!! error TS1124: Digit expected.
4072
~
73+
!!! error TS2304: Cannot find name 'n'.
74+
123en[null]
75+
76+
!!! error TS1124: Digit expected.
77+
~
4178
!!! error TS2304: Cannot find name 'n'.
4279
1a
4380
~
44-
!!! error TS1351: An identifier cannot follow a numeric literal.
81+
!!! error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
4582
~
4683
!!! error TS2304: Cannot find name 'a'.
84+
123a
85+
~
86+
!!! error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
87+
~
88+
!!! error TS2304: Cannot find name 'a'.
89+
123abc
90+
~~~
91+
!!! error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
92+
~~~
93+
!!! error TS2304: Cannot find name 'abc'.
4794
1e
4895

96+
!!! error TS1124: Digit expected.
97+
123e
98+
4999
!!! error TS1124: Digit expected.
50100
1e9
101+
123e9
51102
1ee
52103

53104
!!! error TS1124: Digit expected.
54105
~
106+
!!! error TS2304: Cannot find name 'e'.
107+
123ee
108+
109+
!!! error TS1124: Digit expected.
110+
~
55111
!!! error TS2304: Cannot find name 'e'.
56112
1n
113+
123n
57114
2n2
58115
~
116+
!!! error TS1005: ';' expected.
117+
123n2
118+
~
59119
!!! error TS1005: ';' expected.
60120
2na
61121
~
62-
!!! error TS1351: An identifier cannot follow a numeric literal.
122+
!!! error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
63123
~
64124
!!! error TS2304: Cannot find name 'a'.
125+
123na
126+
~
127+
!!! error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
128+
~
129+
!!! error TS2304: Cannot find name 'a'.
130+
123nabc
131+
~~~
132+
!!! error TS1351: An identifier or keyword cannot immediately follow a numeric literal.
133+
~~~
134+
!!! error TS2304: Cannot find name 'abc'.
65135

tests/baselines/reference/identifierStartAfterNumericLiteral.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,68 @@
22
let valueIn = 3in[null];
33

44
3a[null]
5+
123a[null]
56
3e[null]
7+
123e[null]
68
3in[null]
9+
123in[null]
710
3en[null]
11+
123en[null]
812
1a
13+
123a
14+
123abc
915
1e
16+
123e
1017
1e9
18+
123e9
1119
1ee
20+
123ee
1221
1n
22+
123n
1323
2n2
24+
123n2
1425
2na
26+
123na
27+
123nabc
1528

1629

1730
//// [identifierStartAfterNumericLiteral.js]
1831
var valueIn = 3 in [null];
1932
3;
2033
a[null];
34+
123;
35+
a[null];
2136
3e[null];
37+
123e[null];
2238
3 in [null];
39+
123 in [null];
2340
3e;
2441
n[null];
42+
123e;
43+
n[null];
2544
1;
2645
a;
46+
123;
47+
a;
48+
123;
49+
abc;
2750
1e;
51+
123e;
2852
1e9;
53+
123e9;
2954
1e;
3055
e;
56+
123e;
57+
e;
3158
1n;
59+
123n;
3260
2n;
3361
2;
62+
123n;
63+
2;
3464
2n;
3565
a;
66+
123n;
67+
a;
68+
123n;
69+
abc;

tests/baselines/reference/identifierStartAfterNumericLiteral.symbols

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,27 @@ let valueIn = 3in[null];
33
>valueIn : Symbol(valueIn, Decl(identifierStartAfterNumericLiteral.ts, 0, 3))
44

55
3a[null]
6+
123a[null]
67
3e[null]
8+
123e[null]
79
3in[null]
10+
123in[null]
811
3en[null]
12+
123en[null]
913
1a
14+
123a
15+
123abc
1016
1e
17+
123e
1118
1e9
19+
123e9
1220
1ee
21+
123ee
1322
1n
23+
123n
1424
2n2
25+
123n2
1526
2na
27+
123na
28+
123nabc
1629

tests/baselines/reference/identifierStartAfterNumericLiteral.types

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,101 @@ let valueIn = 3in[null];
1212
>a : any
1313
>null : null
1414

15+
123a[null]
16+
>123 : 123
17+
>a[null] : any
18+
>a : any
19+
>null : null
20+
1521
3e[null]
1622
>3e[null] : any
1723
>3e : 3
1824
>null : null
1925

26+
123e[null]
27+
>123e[null] : any
28+
>123e : 123
29+
>null : null
30+
2031
3in[null]
2132
>3in[null] : boolean
2233
>3 : 3
2334
>[null] : null[]
2435
>null : null
2536

37+
123in[null]
38+
>123in[null] : boolean
39+
>123 : 123
40+
>[null] : null[]
41+
>null : null
42+
2643
3en[null]
2744
>3e : 3
2845
>n[null] : any
2946
>n : any
3047
>null : null
3148

49+
123en[null]
50+
>123e : 123
51+
>n[null] : any
52+
>n : any
53+
>null : null
54+
3255
1a
3356
>1 : 1
3457
>a : any
3558

59+
123a
60+
>123 : 123
61+
>a : any
62+
63+
123abc
64+
>123 : 123
65+
>abc : any
66+
3667
1e
3768
>1e : 1
3869

70+
123e
71+
>123e : 123
72+
3973
1e9
4074
>1e9 : 1000000000
4175

76+
123e9
77+
>123e9 : 123000000000
78+
4279
1ee
4380
>1e : 1
4481
>e : any
4582

83+
123ee
84+
>123e : 123
85+
>e : any
86+
4687
1n
4788
>1n : 1n
4889

90+
123n
91+
>123n : 123n
92+
4993
2n2
5094
>2n : 2n
5195
>2 : 2
5296

97+
123n2
98+
>123n : 123n
99+
>2 : 2
100+
53101
2na
54102
>2n : 2n
55103
>a : any
56104

105+
123na
106+
>123n : 123n
107+
>a : any
108+
109+
123nabc
110+
>123n : 123n
111+
>abc : any
112+

0 commit comments

Comments
 (0)