Skip to content

Fix duplicate identifier reporting in classes #13796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15780,19 +15780,20 @@ namespace ts {
}

function checkClassForDuplicateDeclarations(node: ClassLikeDeclaration) {
const enum Accessor {
const enum Declaration {
Getter = 1,
Setter = 2,
Method = 4,
Property = Getter | Setter
}

const instanceNames = createMap<Accessor>();
const staticNames = createMap<Accessor>();
const instanceNames = createMap<Declaration>();
const staticNames = createMap<Declaration>();
for (const member of node.members) {
if (member.kind === SyntaxKind.Constructor) {
for (const param of (member as ConstructorDeclaration).parameters) {
if (isParameterPropertyDeclaration(param)) {
addName(instanceNames, param.name, (param.name as Identifier).text, Accessor.Property);
addName(instanceNames, param.name, (param.name as Identifier).text, Declaration.Property);
}
}
}
Expand All @@ -15804,25 +15805,34 @@ namespace ts {
if (memberName) {
switch (member.kind) {
case SyntaxKind.GetAccessor:
addName(names, member.name, memberName, Accessor.Getter);
addName(names, member.name, memberName, Declaration.Getter);
break;

case SyntaxKind.SetAccessor:
addName(names, member.name, memberName, Accessor.Setter);
addName(names, member.name, memberName, Declaration.Setter);
break;

case SyntaxKind.PropertyDeclaration:
addName(names, member.name, memberName, Accessor.Property);
addName(names, member.name, memberName, Declaration.Property);
break;

case SyntaxKind.MethodDeclaration:
addName(names, member.name, memberName, Declaration.Method);
break;
}
}
}
}

function addName(names: Map<Accessor>, location: Node, name: string, meaning: Accessor) {
function addName(names: Map<Declaration>, location: Node, name: string, meaning: Declaration) {
const prev = names.get(name);
if (prev) {
if (prev & meaning) {
if (prev & Declaration.Method) {
if (meaning !== Declaration.Method) {
error(location, Diagnostics.Duplicate_identifier_0, getTextOfNode(location));
}
}
else if (prev & meaning) {
error(location, Diagnostics.Duplicate_identifier_0, getTextOfNode(location));
}
else {
Expand Down
31 changes: 31 additions & 0 deletions tests/baselines/reference/classWithDuplicateIdentifier.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
tests/cases/compiler/classWithDuplicateIdentifier.ts(3,5): error TS2300: Duplicate identifier 'a'.
tests/cases/compiler/classWithDuplicateIdentifier.ts(6,5): error TS2300: Duplicate identifier 'b'.
tests/cases/compiler/classWithDuplicateIdentifier.ts(7,5): error TS2300: Duplicate identifier 'b'.
tests/cases/compiler/classWithDuplicateIdentifier.ts(11,5): error TS2300: Duplicate identifier 'c'.
tests/cases/compiler/classWithDuplicateIdentifier.ts(11,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'c' must be of type 'number', but here has type 'string'.


==== tests/cases/compiler/classWithDuplicateIdentifier.ts (5 errors) ====
class C {
a(): number { return 0; } // error: duplicate identifier
a: number;
~
!!! error TS2300: Duplicate identifier 'a'.
}
class K {
b: number; // error: duplicate identifier
~
!!! error TS2300: Duplicate identifier 'b'.
b(): number { return 0; }
~
!!! error TS2300: Duplicate identifier 'b'.
}
class D {
c: number;
c: string;
~
!!! error TS2300: Duplicate identifier 'c'.
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'c' must be of type 'number', but here has type 'string'.
}

33 changes: 33 additions & 0 deletions tests/baselines/reference/classWithDuplicateIdentifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//// [classWithDuplicateIdentifier.ts]
class C {
a(): number { return 0; } // error: duplicate identifier
a: number;
}
class K {
b: number; // error: duplicate identifier
b(): number { return 0; }
}
class D {
c: number;
c: string;
}


//// [classWithDuplicateIdentifier.js]
var C = (function () {
function C() {
}
C.prototype.a = function () { return 0; }; // error: duplicate identifier
return C;
}());
var K = (function () {
function K() {
}
K.prototype.b = function () { return 0; };
return K;
}());
var D = (function () {
function D() {
}
return D;
}());
12 changes: 12 additions & 0 deletions tests/cases/compiler/classWithDuplicateIdentifier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class C {
a(): number { return 0; } // error: duplicate identifier
a: number;
}
class K {
b: number; // error: duplicate identifier
b(): number { return 0; }
}
class D {
c: number;
c: string;
}