diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6ff0d5f268b..b1e256f1300 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6885,7 +6885,7 @@ module ts { } } - function checkCollisionsWithConstDeclarations(node: VariableDeclaration) { + function checkCollisionWithConstDeclarations(node: VariableDeclaration) { // Variable declarations are hoisted to the top of their function scope. They can shadow // block scoped declarations, which bind tighter. this will not be flagged as duplicate definition // by the binder as the declaration scope is different. @@ -6906,10 +6906,12 @@ module ts { // } if (node.initializer && (node.flags & NodeFlags.BlockScoped) === 0) { var symbol = getSymbolOfNode(node); - var localDeclarationSymbol = resolveName(node, node.name.text, SymbolFlags.Variable, /*nodeNotFoundErrorMessage*/ undefined, /*nameArg*/ undefined); - if (localDeclarationSymbol && localDeclarationSymbol !== symbol && localDeclarationSymbol.flags & SymbolFlags.BlockScopedVariable) { - if (getDeclarationFlagsFromSymbol(localDeclarationSymbol) & NodeFlags.Const) { - error(node, Diagnostics.Cannot_redeclare_block_scoped_variable_0, symbolToString(localDeclarationSymbol)); + if (symbol.flags & SymbolFlags.FunctionScopedVariable) { + var localDeclarationSymbol = resolveName(node, node.name.text, SymbolFlags.Variable, /*nodeNotFoundErrorMessage*/ undefined, /*nameArg*/ undefined); + if (localDeclarationSymbol && localDeclarationSymbol !== symbol && localDeclarationSymbol.flags & SymbolFlags.BlockScopedVariable) { + if (getDeclarationFlagsFromSymbol(localDeclarationSymbol) & NodeFlags.Const) { + error(node, Diagnostics.Cannot_redeclare_block_scoped_variable_0, symbolToString(localDeclarationSymbol)); + } } } } @@ -6938,8 +6940,7 @@ module ts { // Use default messages checkTypeAssignableTo(checkAndMarkExpression(node.initializer), type, node, /*chainedMessage*/ undefined, /*terminalMessage*/ undefined); } - - checkCollisionsWithConstDeclarations(node); + checkCollisionWithConstDeclarations(node); } checkCollisionWithCapturedSuperVariable(node, node.name); diff --git a/tests/baselines/reference/constDeclarationShadowedByVarDeclaration3.js b/tests/baselines/reference/constDeclarationShadowedByVarDeclaration3.js new file mode 100644 index 00000000000..6f5b898c5ff --- /dev/null +++ b/tests/baselines/reference/constDeclarationShadowedByVarDeclaration3.js @@ -0,0 +1,21 @@ +//// [constDeclarationShadowedByVarDeclaration3.ts] +// Ensure only checking for const declarations shadowed by vars +class Rule { + public regex: RegExp = new RegExp(''); + public name: string = ''; + + constructor(name: string) { + this.name = name; + } +} + +//// [constDeclarationShadowedByVarDeclaration3.js] +// Ensure only checking for const declarations shadowed by vars +var Rule = (function () { + function Rule(name) { + this.regex = new RegExp(''); + this.name = ''; + this.name = name; + } + return Rule; +})(); diff --git a/tests/baselines/reference/constDeclarationShadowedByVarDeclaration3.types b/tests/baselines/reference/constDeclarationShadowedByVarDeclaration3.types new file mode 100644 index 00000000000..89490567f14 --- /dev/null +++ b/tests/baselines/reference/constDeclarationShadowedByVarDeclaration3.types @@ -0,0 +1,25 @@ +=== tests/cases/compiler/constDeclarationShadowedByVarDeclaration3.ts === +// Ensure only checking for const declarations shadowed by vars +class Rule { +>Rule : Rule + + public regex: RegExp = new RegExp(''); +>regex : RegExp +>RegExp : RegExp +>new RegExp('') : RegExp +>RegExp : { (pattern: string, flags?: string): RegExp; new (pattern: string, flags?: string): RegExp; $1: string; $2: string; $3: string; $4: string; $5: string; $6: string; $7: string; $8: string; $9: string; lastMatch: string; } + + public name: string = ''; +>name : string + + constructor(name: string) { +>name : string + + this.name = name; +>this.name = name : string +>this.name : string +>this : Rule +>name : string +>name : string + } +} diff --git a/tests/cases/compiler/constDeclarationShadowedByVarDeclaration3.ts b/tests/cases/compiler/constDeclarationShadowedByVarDeclaration3.ts new file mode 100644 index 00000000000..5b4229a8e54 --- /dev/null +++ b/tests/cases/compiler/constDeclarationShadowedByVarDeclaration3.ts @@ -0,0 +1,9 @@ +// Ensure only checking for const declarations shadowed by vars +class Rule { + public regex: RegExp = new RegExp(''); + public name: string = ''; + + constructor(name: string) { + this.name = name; + } +} \ No newline at end of file