Skip to content

Commit 6224d51

Browse files
author
Andy
authored
For { type: "a" } | { type: "b" }, find references for the union property (microsoft#21298)
1 parent eed8573 commit 6224d51

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/services/findAllReferences.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ namespace ts.FindAllReferences.Core {
356356

357357
/** Core find-all-references algorithm for a normal symbol. */
358358
function getReferencedSymbolsForSymbol(symbol: Symbol, node: Node, sourceFiles: ReadonlyArray<SourceFile>, checker: TypeChecker, cancellationToken: CancellationToken, options: Options): SymbolAndEntries[] {
359-
symbol = skipPastExportOrImportSpecifier(symbol, node, checker);
359+
symbol = skipPastExportOrImportSpecifierOrUnion(symbol, node, checker);
360360

361361
// Compute the meaning from the location and the symbol it references
362362
const searchMeaning = getIntersectingMeaningFromDeclarations(getMeaningFromLocation(node), symbol.declarations);
@@ -405,7 +405,7 @@ namespace ts.FindAllReferences.Core {
405405
}
406406

407407
/** Handle a few special cases relating to export/import specifiers. */
408-
function skipPastExportOrImportSpecifier(symbol: Symbol, node: Node, checker: TypeChecker): Symbol {
408+
function skipPastExportOrImportSpecifierOrUnion(symbol: Symbol, node: Node, checker: TypeChecker): Symbol {
409409
const { parent } = node;
410410
if (isExportSpecifier(parent)) {
411411
return getLocalSymbolForExportSpecifier(node as Identifier, symbol, parent, checker);
@@ -415,7 +415,11 @@ namespace ts.FindAllReferences.Core {
415415
return checker.getImmediateAliasedSymbol(symbol);
416416
}
417417

418-
return symbol;
418+
// If the symbol is declared as part of a declaration like `{ type: "a" } | { type: "b" }`, use the property on the union type to get more references.
419+
return firstDefined(symbol.declarations, decl =>
420+
isTypeLiteralNode(decl.parent) && isUnionTypeNode(decl.parent.parent)
421+
? checker.getPropertyOfType(checker.getTypeFromTypeNode(decl.parent.parent), symbol.name)
422+
: undefined) || symbol;
419423
}
420424

421425
/**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////type T =
4+
//// | { [|{| "isWriteAccess": true, "isDefinition": true |}type|]: "a" }
5+
//// | { [|{| "isWriteAccess": true, "isDefinition": true |}type|]: "b" };
6+
////declare const t: T;
7+
////if (t.[|type|] !== "failure") {
8+
//// t.[|type|];
9+
////}
10+
11+
const ranges = test.ranges();
12+
const [r0, r1, r2, r3] = ranges;
13+
verify.referenceGroups(ranges, [
14+
{ definition: '(property) type: "a"', ranges: [r0, r2, r3] }, // TODO: this have type `"a" | "b"`
15+
{ definition: '(property) type: "b"', ranges: [r1] },
16+
]);

0 commit comments

Comments
 (0)