diff --git a/src/services/services.ts b/src/services/services.ts index 568b92f5c06..80cb181a643 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -6408,20 +6408,31 @@ namespace ts { if (containingTypeReference) { const parent = containingTypeReference.parent; if (isVariableLike(parent) && parent.type === containingTypeReference && parent.initializer && isImplementationExpression(parent.initializer)) { - result.push(getReferenceEntryFromNode(parent.initializer)); + maybeAdd(getReferenceEntryFromNode(parent.initializer)); } else if (isFunctionLike(parent) && parent.type === containingTypeReference && parent.body && parent.body.kind === SyntaxKind.Block) { forEachReturnStatement(parent.body, (returnStatement) => { if (returnStatement.expression && isImplementationExpression(returnStatement.expression)) { - result.push(getReferenceEntryFromNode(returnStatement.expression)); + maybeAdd(getReferenceEntryFromNode(returnStatement.expression)); } }); } else if (isTypeAssertionExpression(parent) && isImplementationExpression(parent.expression)) { - result.push(getReferenceEntryFromNode(parent.expression)); + maybeAdd(getReferenceEntryFromNode(parent.expression)); } } } + + // Type nodes can contain multiple references to the same type. For example: + // let x: Foo & (Foo & Bar) = ... + // Because we are returning the implementation locations and not the identifier locations, + // duplicate entries would be returned here as each of the type references is part of + // the same implementation. For that reason, check before we add a new entry + function maybeAdd(a: ReferenceEntry) { + if (!forEach(result, b => a.fileName === b.fileName && a.textSpan.start === b.textSpan.start && a.textSpan.length === b.textSpan.length)) { + result.push(a); + } + } } function getSymbolsForComponentTypes(type: UnionOrIntersectionType, result: Symbol[] = []): Symbol[] { diff --git a/tests/cases/fourslash/goToImplementationInterface_07.ts b/tests/cases/fourslash/goToImplementationInterface_07.ts index c14a98f9185..6a8665ec213 100644 --- a/tests/cases/fourslash/goToImplementationInterface_07.ts +++ b/tests/cases/fourslash/goToImplementationInterface_07.ts @@ -13,7 +13,7 @@ //// let x1: Foo = [|{ hello () { /**typeReference*/ } }|]; //// let x2: () => Foo = [|(() => { hello () { /**functionType*/} })|]; //// let x3: Foo | Bar = [|{ hello () { /**unionType*/} }|]; -//// let x4: Foo & Bar = [|{ hello () { /**intersectionType*/} }|]; +//// let x4: Foo & (Foo & Bar) = [|{ hello () { /**intersectionType*/} }|]; //// let x5: [Foo] = [|[{ hello () { /**tupleType*/} }]|]; //// let x6: (Foo) = [|{ hello () { /**parenthesizedType*/} }|]; //// let x7: (new() => Foo) = [|class { hello () { /**constructorType*/} }|];