Skip to content

Commit 67c78a2

Browse files
committed
Only check for collisions with variabels and not properties
1 parent e4a2084 commit 67c78a2

File tree

4 files changed

+63
-7
lines changed

4 files changed

+63
-7
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6885,7 +6885,7 @@ module ts {
68856885
}
68866886
}
68876887

6888-
function checkCollisionsWithConstDeclarations(node: VariableDeclaration) {
6888+
function checkCollisionWithConstDeclarations(node: VariableDeclaration) {
68896889
// Variable declarations are hoisted to the top of their function scope. They can shadow
68906890
// block scoped declarations, which bind tighter. this will not be flagged as duplicate definition
68916891
// by the binder as the declaration scope is different.
@@ -6906,10 +6906,12 @@ module ts {
69066906
// }
69076907
if (node.initializer && (node.flags & NodeFlags.BlockScoped) === 0) {
69086908
var symbol = getSymbolOfNode(node);
6909-
var localDeclarationSymbol = resolveName(node, node.name.text, SymbolFlags.Variable, /*nodeNotFoundErrorMessage*/ undefined, /*nameArg*/ undefined);
6910-
if (localDeclarationSymbol && localDeclarationSymbol !== symbol && localDeclarationSymbol.flags & SymbolFlags.BlockScopedVariable) {
6911-
if (getDeclarationFlagsFromSymbol(localDeclarationSymbol) & NodeFlags.Const) {
6912-
error(node, Diagnostics.Cannot_redeclare_block_scoped_variable_0, symbolToString(localDeclarationSymbol));
6909+
if (symbol.flags & SymbolFlags.FunctionScopedVariable) {
6910+
var localDeclarationSymbol = resolveName(node, node.name.text, SymbolFlags.Variable, /*nodeNotFoundErrorMessage*/ undefined, /*nameArg*/ undefined);
6911+
if (localDeclarationSymbol && localDeclarationSymbol !== symbol && localDeclarationSymbol.flags & SymbolFlags.BlockScopedVariable) {
6912+
if (getDeclarationFlagsFromSymbol(localDeclarationSymbol) & NodeFlags.Const) {
6913+
error(node, Diagnostics.Cannot_redeclare_block_scoped_variable_0, symbolToString(localDeclarationSymbol));
6914+
}
69136915
}
69146916
}
69156917
}
@@ -6938,8 +6940,7 @@ module ts {
69386940
// Use default messages
69396941
checkTypeAssignableTo(checkAndMarkExpression(node.initializer), type, node, /*chainedMessage*/ undefined, /*terminalMessage*/ undefined);
69406942
}
6941-
6942-
checkCollisionsWithConstDeclarations(node);
6943+
checkCollisionWithConstDeclarations(node);
69436944
}
69446945

69456946
checkCollisionWithCapturedSuperVariable(node, node.name);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [constDeclarationShadowedByVarDeclaration3.ts]
2+
// Ensure only checking for const declarations shadowed by vars
3+
class Rule {
4+
public regex: RegExp = new RegExp('');
5+
public name: string = '';
6+
7+
constructor(name: string) {
8+
this.name = name;
9+
}
10+
}
11+
12+
//// [constDeclarationShadowedByVarDeclaration3.js]
13+
// Ensure only checking for const declarations shadowed by vars
14+
var Rule = (function () {
15+
function Rule(name) {
16+
this.regex = new RegExp('');
17+
this.name = '';
18+
this.name = name;
19+
}
20+
return Rule;
21+
})();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/constDeclarationShadowedByVarDeclaration3.ts ===
2+
// Ensure only checking for const declarations shadowed by vars
3+
class Rule {
4+
>Rule : Rule
5+
6+
public regex: RegExp = new RegExp('');
7+
>regex : RegExp
8+
>RegExp : RegExp
9+
>new RegExp('') : RegExp
10+
>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; }
11+
12+
public name: string = '';
13+
>name : string
14+
15+
constructor(name: string) {
16+
>name : string
17+
18+
this.name = name;
19+
>this.name = name : string
20+
>this.name : string
21+
>this : Rule
22+
>name : string
23+
>name : string
24+
}
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Ensure only checking for const declarations shadowed by vars
2+
class Rule {
3+
public regex: RegExp = new RegExp('');
4+
public name: string = '';
5+
6+
constructor(name: string) {
7+
this.name = name;
8+
}
9+
}

0 commit comments

Comments
 (0)