Only check for collisions with variabels and not properties

This commit is contained in:
Mohamed Hegazy 2014-10-24 10:30:40 -07:00
parent e4a20849bb
commit 67c78a2662
4 changed files with 63 additions and 7 deletions

View File

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

View File

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

View File

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

View File

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