From 78bc368c2293068197feed9391a4b1af2d67eb29 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Tue, 31 Jan 2017 14:14:48 -0800 Subject: [PATCH] Fix duplicate identifier reporting in classes Previously declarations in the order method-property would not report an error, but the order property-method would. Now both orders report "Duplicate identifier '{0}'." --- src/compiler/checker.ts | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 26c27cb3be2..c1b9a7ac0e3 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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(); - const staticNames = createMap(); + const instanceNames = createMap(); + const staticNames = createMap(); 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); } } } @@ -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, location: Node, name: string, meaning: Accessor) { + function addName(names: Map, 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 {