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.

This commit is contained in:
Mohamed Hegazy 2014-10-17 09:28:42 -07:00
parent 60bb37be60
commit fd469d63b1
4 changed files with 45 additions and 2 deletions

View file

@ -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));
}

View file

@ -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";
}
}

View file

@ -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
}
}

View file

@ -0,0 +1,9 @@
// @target: ES6
// No errors, const declaration is not shadowed
function outer() {
const x = 0;
function inner() {
var x = "inner";
}
}