Skip to content

Commit 3e45601

Browse files
committed
Allow const in for statements
1 parent 82f5fb4 commit 3e45601

12 files changed

+156
-50
lines changed

src/compiler/diagnosticInformationMap.generated.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ module ts {
116116
new_T_cannot_be_used_to_create_an_array_Use_new_Array_T_instead: { code: 1150, category: DiagnosticCategory.Error, key: "'new T[]' cannot be used to create an array. Use 'new Array<T>()' instead." },
117117
An_enum_member_cannot_have_a_numeric_name: { code: 1151, category: DiagnosticCategory.Error, key: "An enum member cannot have a numeric name." },
118118
var_let_or_const_expected: { code: 1152, category: DiagnosticCategory.Error, key: "'var', 'let' or 'const' expected." },
119-
let_variable_declarations_are_only_available_when_targeting_ECMAScript_6_and_higher: { code: 1153, category: DiagnosticCategory.Error, key: "'let' variable declarations are only available when targeting ECMAScript 6 and higher." },
120-
const_variable_declarations_are_only_available_when_targeting_ECMAScript_6_and_higher: { code: 1154, category: DiagnosticCategory.Error, key: "'const' variable declarations are only available when targeting ECMAScript 6 and higher." },
119+
let_declarations_are_only_available_when_targeting_ECMAScript_6_and_higher: { code: 1153, category: DiagnosticCategory.Error, key: "'let' declarations are only available when targeting ECMAScript 6 and higher." },
120+
const_declarations_are_only_available_when_targeting_ECMAScript_6_and_higher: { code: 1154, category: DiagnosticCategory.Error, key: "'const' declarations are only available when targeting ECMAScript 6 and higher." },
121121
const_must_be_intialized: { code: 1155, category: DiagnosticCategory.Error, key: "const must be intialized." },
122122
const_must_be_declared_inside_a_block: { code: 1156, category: DiagnosticCategory.Error, key: "const must be declared inside a block." },
123123
let_must_be_declared_inside_a_block: { code: 1157, category: DiagnosticCategory.Error, key: "let must be declared inside a block." },

src/compiler/diagnosticMessages.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,11 @@
455455
"category": "Error",
456456
"code": 1152
457457
},
458-
"'let' variable declarations are only available when targeting ECMAScript 6 and higher.": {
458+
"'let' declarations are only available when targeting ECMAScript 6 and higher.": {
459459
"category": "Error",
460460
"code": 1153
461461
},
462-
"'const' variable declarations are only available when targeting ECMAScript 6 and higher.": {
462+
"'const' declarations are only available when targeting ECMAScript 6 and higher.": {
463463
"category": "Error",
464464
"code": 1154
465465
},

src/compiler/parser.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -2654,7 +2654,16 @@ module ts {
26542654
error(Diagnostics.Variable_declaration_list_cannot_be_empty);
26552655
}
26562656
if (languageVersion < ScriptTarget.ES6) {
2657-
grammarErrorAtPos(declarations.pos, declarations.end - declarations.pos, Diagnostics.let_variable_declarations_are_only_available_when_targeting_ECMAScript_6_and_higher);
2657+
grammarErrorAtPos(declarations.pos, declarations.end - declarations.pos, Diagnostics.let_declarations_are_only_available_when_targeting_ECMAScript_6_and_higher);
2658+
}
2659+
}
2660+
else if (parseOptional(SyntaxKind.ConstKeyword)) {
2661+
var declarations = parseVariableDeclarationList(NodeFlags.Const, true);
2662+
if (!declarations.length) {
2663+
error(Diagnostics.Variable_declaration_list_cannot_be_empty);
2664+
}
2665+
if (languageVersion < ScriptTarget.ES6) {
2666+
grammarErrorAtPos(declarations.pos, declarations.end - declarations.pos, Diagnostics.const_declarations_are_only_available_when_targeting_ECMAScript_6_and_higher);
26582667
}
26592668
}
26602669
else {
@@ -3145,10 +3154,10 @@ module ts {
31453154
}
31463155
if (languageVersion < ScriptTarget.ES6) {
31473156
if (node.flags & NodeFlags.Let) {
3148-
grammarErrorOnNode(node, Diagnostics.let_variable_declarations_are_only_available_when_targeting_ECMAScript_6_and_higher);
3157+
grammarErrorOnNode(node, Diagnostics.let_declarations_are_only_available_when_targeting_ECMAScript_6_and_higher);
31493158
}
31503159
else if (node.flags & NodeFlags.Const) {
3151-
grammarErrorOnNode(node, Diagnostics.const_variable_declarations_are_only_available_when_targeting_ECMAScript_6_and_higher);
3160+
grammarErrorOnNode(node, Diagnostics.const_declarations_are_only_available_when_targeting_ECMAScript_6_and_higher);
31523161
}
31533162
}
31543163
else if (!allowLetDeclarations){

tests/baselines/reference/constDeclarations-errors.errors.txt

+19-34
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,13 @@ tests/cases/compiler/constDeclarations-errors.ts(5,7): error TS1155: const must
44
tests/cases/compiler/constDeclarations-errors.ts(5,11): error TS1155: const must be intialized.
55
tests/cases/compiler/constDeclarations-errors.ts(5,15): error TS1155: const must be intialized.
66
tests/cases/compiler/constDeclarations-errors.ts(5,27): error TS1155: const must be intialized.
7-
tests/cases/compiler/constDeclarations-errors.ts(8,5): error TS1109: Expression expected.
8-
tests/cases/compiler/constDeclarations-errors.ts(8,5): error TS1156: const must be declared inside a block.
97
tests/cases/compiler/constDeclarations-errors.ts(8,11): error TS1155: const must be intialized.
10-
tests/cases/compiler/constDeclarations-errors.ts(8,13): error TS1005: ';' expected.
11-
tests/cases/compiler/constDeclarations-errors.ts(8,13): error TS1128: Declaration or statement expected.
12-
tests/cases/compiler/constDeclarations-errors.ts(8,18): error TS1128: Declaration or statement expected.
13-
tests/cases/compiler/constDeclarations-errors.ts(10,5): error TS1109: Expression expected.
14-
tests/cases/compiler/constDeclarations-errors.ts(10,5): error TS1156: const must be declared inside a block.
15-
tests/cases/compiler/constDeclarations-errors.ts(10,28): error TS1005: ';' expected.
16-
tests/cases/compiler/constDeclarations-errors.ts(10,18): error TS2304: Cannot find name 'c'.
17-
tests/cases/compiler/constDeclarations-errors.ts(10,25): error TS2304: Cannot find name 'c'.
8+
tests/cases/compiler/constDeclarations-errors.ts(14,11): error TS1155: const must be intialized.
9+
tests/cases/compiler/constDeclarations-errors.ts(17,20): error TS1155: const must be intialized.
10+
tests/cases/compiler/constDeclarations-errors.ts(11,27): error TS2449: The operand of an increment or decrement operator cannot be a constant.
1811

1912

20-
==== tests/cases/compiler/constDeclarations-errors.ts (17 errors) ====
13+
==== tests/cases/compiler/constDeclarations-errors.ts (10 errors) ====
2114

2215
// error, missing intialicer
2316
const c1;
@@ -36,30 +29,22 @@ tests/cases/compiler/constDeclarations-errors.ts(10,25): error TS2304: Cannot fi
3629
~~
3730
!!! error TS1155: const must be intialized.
3831

39-
// error, wrong context
32+
// error, can not be unintalized
4033
for(const c in {}) { }
41-
~~~~~
42-
!!! error TS1109: Expression expected.
43-
~~~~~~~
44-
!!! error TS1156: const must be declared inside a block.
4534
~
4635
!!! error TS1155: const must be intialized.
47-
~~
48-
!!! error TS1005: ';' expected.
49-
~~
50-
!!! error TS1128: Declaration or statement expected.
51-
~
52-
!!! error TS1128: Declaration or statement expected.
5336

54-
for(const c = 0; c < 9; c++) { }
55-
~~~~~
56-
!!! error TS1109: Expression expected.
57-
~~~~~~~~~~~~
58-
!!! error TS1156: const must be declared inside a block.
59-
~
60-
!!! error TS1005: ';' expected.
61-
~
62-
!!! error TS2304: Cannot find name 'c'.
63-
~
64-
!!! error TS2304: Cannot find name 'c'.
65-
37+
// error, assigning to a const
38+
for(const c8 = 0; c8 < 1; c8++) { }
39+
~~
40+
!!! error TS2449: The operand of an increment or decrement operator cannot be a constant.
41+
42+
// error, can not be unintalized
43+
for(const c9; c9 < 1;) { }
44+
~~
45+
!!! error TS1155: const must be intialized.
46+
47+
// error, can not be unintalized
48+
for(const c10 = 0, c11; c10 < 1;) { }
49+
~~~
50+
!!! error TS1155: const must be intialized.
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
tests/cases/compiler/constDeclarations-es5.ts(2,1): error TS1154: 'const' variable declarations are only available when targeting ECMAScript 6 and higher.
2-
tests/cases/compiler/constDeclarations-es5.ts(3,1): error TS1154: 'const' variable declarations are only available when targeting ECMAScript 6 and higher.
3-
tests/cases/compiler/constDeclarations-es5.ts(4,1): error TS1154: 'const' variable declarations are only available when targeting ECMAScript 6 and higher.
1+
tests/cases/compiler/constDeclarations-es5.ts(2,1): error TS1154: 'const' declarations are only available when targeting ECMAScript 6 and higher.
2+
tests/cases/compiler/constDeclarations-es5.ts(3,1): error TS1154: 'const' declarations are only available when targeting ECMAScript 6 and higher.
3+
tests/cases/compiler/constDeclarations-es5.ts(4,1): error TS1154: 'const' declarations are only available when targeting ECMAScript 6 and higher.
44

55

66
==== tests/cases/compiler/constDeclarations-es5.ts (3 errors) ====
77

88
const z7 = false;
99
~~~~~~~~~~~~~~~~~
10-
!!! error TS1154: 'const' variable declarations are only available when targeting ECMAScript 6 and higher.
10+
!!! error TS1154: 'const' declarations are only available when targeting ECMAScript 6 and higher.
1111
const z8: number = 23;
1212
~~~~~~~~~~~~~~~~~~~~~~
13-
!!! error TS1154: 'const' variable declarations are only available when targeting ECMAScript 6 and higher.
13+
!!! error TS1154: 'const' declarations are only available when targeting ECMAScript 6 and higher.
1414
const z9 = 0, z10 :string = "", z11 = null;
1515
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16-
!!! error TS1154: 'const' variable declarations are only available when targeting ECMAScript 6 and higher.
16+
!!! error TS1154: 'const' declarations are only available when targeting ECMAScript 6 and higher.
1717

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [constDeclarations-scopes2.ts]
2+
3+
// global
4+
const c = "string";
5+
6+
var n: number;
7+
var b: boolean;
8+
9+
// for scope
10+
for (const c = 0; c < 10; n = c ) {
11+
// for block
12+
const c = false;
13+
b = c;
14+
}
15+
16+
17+
18+
//// [constDeclarations-scopes2.js]
19+
// global
20+
const c = "string";
21+
var n;
22+
var b;
23+
for (var c = 0; c < 10; n = c) {
24+
// for block
25+
const c = false;
26+
b = c;
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== tests/cases/compiler/constDeclarations-scopes2.ts ===
2+
3+
// global
4+
const c = "string";
5+
>c : string
6+
7+
var n: number;
8+
>n : number
9+
10+
var b: boolean;
11+
>b : boolean
12+
13+
// for scope
14+
for (const c = 0; c < 10; n = c ) {
15+
>c : number
16+
>c < 10 : boolean
17+
>c : number
18+
>n = c : number
19+
>n : number
20+
>c : number
21+
22+
// for block
23+
const c = false;
24+
>c : boolean
25+
26+
b = c;
27+
>b = c : boolean
28+
>b : boolean
29+
>c : boolean
30+
}
31+
32+

tests/baselines/reference/constDeclarations.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,24 @@
44
const c1 = false;
55
const c2: number = 23;
66
const c3 = 0, c4 :string = "", c5 = null;
7-
7+
8+
9+
for(const c4 = 0; c4 < 9; ) { break; }
10+
11+
12+
for(const c5 = 0, c6 = 0; c5 < c6; ) { break; }
813

914
//// [constDeclarations.js]
1015
// No error
1116
const c1 = false;
1217
const c2 = 23;
1318
const c3 = 0, c4 = "", c5 = null;
19+
for (var c4 = 0; c4 < 9;) {
20+
break;
21+
}
22+
for (var c5 = 0, c6 = 0; c5 < c6;) {
23+
break;
24+
}
1425

1526

1627
//// [constDeclarations.d.ts]

tests/baselines/reference/constDeclarations.types

+14
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,17 @@ const c3 = 0, c4 :string = "", c5 = null;
1212
>c4 : string
1313
>c5 : any
1414

15+
16+
for(const c4 = 0; c4 < 9; ) { break; }
17+
>c4 : number
18+
>c4 < 9 : boolean
19+
>c4 : number
20+
21+
22+
for(const c5 = 0, c6 = 0; c5 < c6; ) { break; }
23+
>c5 : number
24+
>c6 : number
25+
>c5 < c6 : boolean
26+
>c5 : number
27+
>c6 : number
28+

tests/cases/compiler/constDeclarations-errors.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ const c1;
55
const c2: number;
66
const c3, c4, c5 :string, c6; // error, missing initialicer
77

8-
// error, wrong context
8+
// error, can not be unintalized
99
for(const c in {}) { }
1010

11-
for(const c = 0; c < 9; c++) { }
11+
// error, assigning to a const
12+
for(const c8 = 0; c8 < 1; c8++) { }
13+
14+
// error, can not be unintalized
15+
for(const c9; c9 < 1;) { }
16+
17+
// error, can not be unintalized
18+
for(const c10 = 0, c11; c10 < 1;) { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @target: ES6
2+
3+
// global
4+
const c = "string";
5+
6+
var n: number;
7+
var b: boolean;
8+
9+
// for scope
10+
for (const c = 0; c < 10; n = c ) {
11+
// for block
12+
const c = false;
13+
b = c;
14+
}
15+

tests/cases/compiler/constDeclarations.ts

+6
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@
55
const c1 = false;
66
const c2: number = 23;
77
const c3 = 0, c4 :string = "", c5 = null;
8+
9+
10+
for(const c4 = 0; c4 < 9; ) { break; }
11+
12+
13+
for(const c5 = 0, c6 = 0; c5 < c6; ) { break; }

0 commit comments

Comments
 (0)