From 5ca703eeb4315b2ba9deb2dd642f38f84964b915 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 6 Mar 2015 20:53:15 +0100 Subject: [PATCH 1/4] Add new diagnostics message for let/const declarations in a catch clause --- src/compiler/diagnosticInformationMap.generated.ts | 1 + src/compiler/diagnosticMessages.json | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index 477fd5d46af..f616de8bd4b 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -337,6 +337,7 @@ module ts { 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." }, 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." }, 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." }, + Cannot_redeclare_identifier_0_in_catch_clause: { code: 2492, category: DiagnosticCategory.Error, key: "Cannot redeclare identifier '{0}' in catch clause" }, Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." }, 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}'." }, 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}'." }, diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index da153903b8c..ddd3f72aaf0 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1339,6 +1339,10 @@ "category": "Error", "code": 2491 }, + "Cannot redeclare identifier '{0}' in catch clause": { + "category": "Error", + "code": 2492 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", From b4d723217e67c63addfae44b0592b8bcfc28f22a Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 6 Mar 2015 21:42:42 +0100 Subject: [PATCH 2/4] Error on redeclaring a variable with let/const already defined as catch parameter --- src/compiler/checker.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7220182bfe9..4281d8a5bd3 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -9158,6 +9158,15 @@ module ts { grammarErrorOnFirstToken(catchClause.variableDeclaration.initializer, Diagnostics.Catch_clause_variable_cannot_have_an_initializer); } else { + var identifierName = (catchClause.variableDeclaration.name).text; + var locals = catchClause.block.locals; + if (locals && locals[identifierName]) { + var localSymbol = locals[identifierName] + if (localSymbol && (localSymbol.flags & SymbolFlags.BlockScopedVariable) !== 0) { + grammarErrorOnNode(localSymbol.valueDeclaration, Diagnostics.Cannot_redeclare_identifier_0_in_catch_clause, identifierName); + } + } + // It is a SyntaxError if a TryStatement with a Catch occurs within strict code and the Identifier of the // Catch production is eval or arguments checkGrammarEvalOrArgumentsInStrictMode(node, catchClause.variableDeclaration.name); From 129ef7222c00706c2cb3fc31893f309db231f934 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 6 Mar 2015 22:56:59 +0100 Subject: [PATCH 3/4] Use hasProperty instead --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4281d8a5bd3..98cfac70be3 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -9160,7 +9160,7 @@ module ts { else { var identifierName = (catchClause.variableDeclaration.name).text; var locals = catchClause.block.locals; - if (locals && locals[identifierName]) { + if (locals && hasProperty(locals, identifierName)) { var localSymbol = locals[identifierName] if (localSymbol && (localSymbol.flags & SymbolFlags.BlockScopedVariable) !== 0) { grammarErrorOnNode(localSymbol.valueDeclaration, Diagnostics.Cannot_redeclare_identifier_0_in_catch_clause, identifierName); From 2edb5c88d841750e774769f3cbbc7b126de8aeeb Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 6 Mar 2015 23:00:43 +0100 Subject: [PATCH 4/4] Added tests for let/const variable declarations in catch clause (with the same name) --- .../redeclareParameterInCatchBlock.errors.txt | 31 ++++++++++++++ .../redeclareParameterInCatchBlock.js | 42 +++++++++++++++++++ .../redeclareParameterInCatchBlock.ts | 22 ++++++++++ 3 files changed, 95 insertions(+) create mode 100644 tests/baselines/reference/redeclareParameterInCatchBlock.errors.txt create mode 100644 tests/baselines/reference/redeclareParameterInCatchBlock.js create mode 100644 tests/cases/compiler/redeclareParameterInCatchBlock.ts diff --git a/tests/baselines/reference/redeclareParameterInCatchBlock.errors.txt b/tests/baselines/reference/redeclareParameterInCatchBlock.errors.txt new file mode 100644 index 00000000000..07dbbefefe0 --- /dev/null +++ b/tests/baselines/reference/redeclareParameterInCatchBlock.errors.txt @@ -0,0 +1,31 @@ +tests/cases/compiler/redeclareParameterInCatchBlock.ts(5,11): error TS2492: Cannot redeclare identifier 'e' in catch clause +tests/cases/compiler/redeclareParameterInCatchBlock.ts(11,9): error TS2492: Cannot redeclare identifier 'e' in catch clause + + +==== tests/cases/compiler/redeclareParameterInCatchBlock.ts (2 errors) ==== + + try { + + } catch(e) { + const e = null; + ~ +!!! error TS2492: Cannot redeclare identifier 'e' in catch clause + } + + try { + + } catch(e) { + let e; + ~ +!!! error TS2492: Cannot redeclare identifier 'e' in catch clause + } + + try { + + } catch(e) { + function test() { + let e; + } + } + + \ No newline at end of file diff --git a/tests/baselines/reference/redeclareParameterInCatchBlock.js b/tests/baselines/reference/redeclareParameterInCatchBlock.js new file mode 100644 index 00000000000..a187e2fcf0c --- /dev/null +++ b/tests/baselines/reference/redeclareParameterInCatchBlock.js @@ -0,0 +1,42 @@ +//// [redeclareParameterInCatchBlock.ts] + +try { + +} catch(e) { + const e = null; +} + +try { + +} catch(e) { + let e; +} + +try { + +} catch(e) { + function test() { + let e; + } +} + + + +//// [redeclareParameterInCatchBlock.js] +try { +} +catch (e) { + const e = null; +} +try { +} +catch (e) { + let e; +} +try { +} +catch (e) { + function test() { + let e; + } +} diff --git a/tests/cases/compiler/redeclareParameterInCatchBlock.ts b/tests/cases/compiler/redeclareParameterInCatchBlock.ts new file mode 100644 index 00000000000..37ca5fc3a9e --- /dev/null +++ b/tests/cases/compiler/redeclareParameterInCatchBlock.ts @@ -0,0 +1,22 @@ +// @target: es6 + +try { + +} catch(e) { + const e = null; +} + +try { + +} catch(e) { + let e; +} + +try { + +} catch(e) { + function test() { + let e; + } +} +