From fd469d63b1e0e42a435343152e11d972d17adead Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Fri, 17 Oct 2014 09:28:42 -0700 Subject: [PATCH] Fix search for shadowed const declarations by a var declarations to search for any variable instead of only a blockScoped one to ensure we are not picking it up from a wrong scope. --- src/compiler/checker.ts | 4 ++-- ...onstDeclarationShadowedByVarDeclaration2.js | 18 ++++++++++++++++++ ...tDeclarationShadowedByVarDeclaration2.types | 16 ++++++++++++++++ ...onstDeclarationShadowedByVarDeclaration2.ts | 9 +++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/constDeclarationShadowedByVarDeclaration2.js create mode 100644 tests/baselines/reference/constDeclarationShadowedByVarDeclaration2.types create mode 100644 tests/cases/compiler/constDeclarationShadowedByVarDeclaration2.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5b99e2b2dd..0065d138e9 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6776,8 +6776,8 @@ module ts { // } if (node.initializer && (node.flags & NodeFlags.BlockScoped) === 0) { var symbol = getSymbolOfNode(node); - var localDeclarationSymbol = resolveName(node, node.name.text, SymbolFlags.BlockScoped, /*nodeNotFoundErrorMessage*/ undefined, /*nameArg*/ undefined); - if (localDeclarationSymbol && localDeclarationSymbol !== symbol) { + var localDeclarationSymbol = resolveName(node, node.name.text, SymbolFlags.Variable, /*nodeNotFoundErrorMessage*/ undefined, /*nameArg*/ undefined); + if (localDeclarationSymbol && localDeclarationSymbol !== symbol && localDeclarationSymbol.flags & SymbolFlags.BlockScoped) { if (getDeclarationFlagsFromSymbol(localDeclarationSymbol) & NodeFlags.Const) { error(node, Diagnostics.Cannot_redeclare_constant_0, symbolToString(localDeclarationSymbol)); } diff --git a/tests/baselines/reference/constDeclarationShadowedByVarDeclaration2.js b/tests/baselines/reference/constDeclarationShadowedByVarDeclaration2.js new file mode 100644 index 0000000000..67d3e68807 --- /dev/null +++ b/tests/baselines/reference/constDeclarationShadowedByVarDeclaration2.js @@ -0,0 +1,18 @@ +//// [constDeclarationShadowedByVarDeclaration2.ts] + +// No errors, const declaration is not shadowed +function outer() { + const x = 0; + function inner() { + var x = "inner"; + } +} + +//// [constDeclarationShadowedByVarDeclaration2.js] +// No errors, const declaration is not shadowed +function outer() { + const x = 0; + function inner() { + var x = "inner"; + } +} diff --git a/tests/baselines/reference/constDeclarationShadowedByVarDeclaration2.types b/tests/baselines/reference/constDeclarationShadowedByVarDeclaration2.types new file mode 100644 index 0000000000..4217db4550 --- /dev/null +++ b/tests/baselines/reference/constDeclarationShadowedByVarDeclaration2.types @@ -0,0 +1,16 @@ +=== tests/cases/compiler/constDeclarationShadowedByVarDeclaration2.ts === + +// No errors, const declaration is not shadowed +function outer() { +>outer : () => void + + const x = 0; +>x : number + + function inner() { +>inner : () => void + + var x = "inner"; +>x : string + } +} diff --git a/tests/cases/compiler/constDeclarationShadowedByVarDeclaration2.ts b/tests/cases/compiler/constDeclarationShadowedByVarDeclaration2.ts new file mode 100644 index 0000000000..9893095399 --- /dev/null +++ b/tests/cases/compiler/constDeclarationShadowedByVarDeclaration2.ts @@ -0,0 +1,9 @@ +// @target: ES6 + +// No errors, const declaration is not shadowed +function outer() { + const x = 0; + function inner() { + var x = "inner"; + } +} \ No newline at end of file