Skip to content

Commit 59c3a7d

Browse files
committed
Merge pull request microsoft#2235 from DickvdBrink/letConstInCatchclause
Let const in catchclause
2 parents ffd87b1 + 2edb5c8 commit 59c3a7d

6 files changed

Lines changed: 109 additions & 0 deletions

File tree

src/compiler/checker.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9158,6 +9158,15 @@ module ts {
91589158
grammarErrorOnFirstToken(catchClause.variableDeclaration.initializer, Diagnostics.Catch_clause_variable_cannot_have_an_initializer);
91599159
}
91609160
else {
9161+
var identifierName = (<Identifier>catchClause.variableDeclaration.name).text;
9162+
var locals = catchClause.block.locals;
9163+
if (locals && hasProperty(locals, identifierName)) {
9164+
var localSymbol = locals[identifierName]
9165+
if (localSymbol && (localSymbol.flags & SymbolFlags.BlockScopedVariable) !== 0) {
9166+
grammarErrorOnNode(localSymbol.valueDeclaration, Diagnostics.Cannot_redeclare_identifier_0_in_catch_clause, identifierName);
9167+
}
9168+
}
9169+
91619170
// It is a SyntaxError if a TryStatement with a Catch occurs within strict code and the Identifier of the
91629171
// Catch production is eval or arguments
91639172
checkGrammarEvalOrArgumentsInStrictMode(node, <Identifier>catchClause.variableDeclaration.name);

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ module ts {
337337
The_iterator_returned_by_the_right_hand_side_of_a_for_of_statement_must_have_a_next_method: { code: 2489, category: DiagnosticCategory.Error, key: "The iterator returned by the right-hand side of a 'for...of' statement must have a 'next()' method." },
338338
The_type_returned_by_the_next_method_of_an_iterator_must_have_a_value_property: { code: 2490, category: DiagnosticCategory.Error, key: "The type returned by the 'next()' method of an iterator must have a 'value' property." },
339339
The_left_hand_side_of_a_for_in_statement_cannot_be_a_destructuring_pattern: { code: 2491, category: DiagnosticCategory.Error, key: "The left-hand side of a 'for...in' statement cannot be a destructuring pattern." },
340+
Cannot_redeclare_identifier_0_in_catch_clause: { code: 2492, category: DiagnosticCategory.Error, key: "Cannot redeclare identifier '{0}' in catch clause" },
340341
Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." },
341342
Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." },
342343
Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1: { code: 4004, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported interface has or is using private name '{1}'." },

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,10 @@
13391339
"category": "Error",
13401340
"code": 2491
13411341
},
1342+
"Cannot redeclare identifier '{0}' in catch clause": {
1343+
"category": "Error",
1344+
"code": 2492
1345+
},
13421346

13431347
"Import declaration '{0}' is using private name '{1}'.": {
13441348
"category": "Error",
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
tests/cases/compiler/redeclareParameterInCatchBlock.ts(5,11): error TS2492: Cannot redeclare identifier 'e' in catch clause
2+
tests/cases/compiler/redeclareParameterInCatchBlock.ts(11,9): error TS2492: Cannot redeclare identifier 'e' in catch clause
3+
4+
5+
==== tests/cases/compiler/redeclareParameterInCatchBlock.ts (2 errors) ====
6+
7+
try {
8+
9+
} catch(e) {
10+
const e = null;
11+
~
12+
!!! error TS2492: Cannot redeclare identifier 'e' in catch clause
13+
}
14+
15+
try {
16+
17+
} catch(e) {
18+
let e;
19+
~
20+
!!! error TS2492: Cannot redeclare identifier 'e' in catch clause
21+
}
22+
23+
try {
24+
25+
} catch(e) {
26+
function test() {
27+
let e;
28+
}
29+
}
30+
31+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//// [redeclareParameterInCatchBlock.ts]
2+
3+
try {
4+
5+
} catch(e) {
6+
const e = null;
7+
}
8+
9+
try {
10+
11+
} catch(e) {
12+
let e;
13+
}
14+
15+
try {
16+
17+
} catch(e) {
18+
function test() {
19+
let e;
20+
}
21+
}
22+
23+
24+
25+
//// [redeclareParameterInCatchBlock.js]
26+
try {
27+
}
28+
catch (e) {
29+
const e = null;
30+
}
31+
try {
32+
}
33+
catch (e) {
34+
let e;
35+
}
36+
try {
37+
}
38+
catch (e) {
39+
function test() {
40+
let e;
41+
}
42+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// @target: es6
2+
3+
try {
4+
5+
} catch(e) {
6+
const e = null;
7+
}
8+
9+
try {
10+
11+
} catch(e) {
12+
let e;
13+
}
14+
15+
try {
16+
17+
} catch(e) {
18+
function test() {
19+
let e;
20+
}
21+
}
22+

0 commit comments

Comments
 (0)