Skip to content

Commit 94f6839

Browse files
author
Andy Hanson
committed
Factor out FindAllReferences.Core and use Definition/Entry to allow findReferencedSymbols and getImplementationsAtPosition to return different results
1 parent ec558b8 commit 94f6839

File tree

7 files changed

+277
-196
lines changed

7 files changed

+277
-196
lines changed

src/compiler/types.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,7 +1849,6 @@ namespace ts {
18491849
}
18501850

18511851
export interface ExternalModuleReference extends Node {
1852-
parent: ImportEqualsDeclaration;
18531852
kind: SyntaxKind.ExternalModuleReference;
18541853
parent?: ImportEqualsDeclaration;
18551854
expression?: Expression;
@@ -1909,7 +1908,6 @@ namespace ts {
19091908
}
19101909

19111910
export interface NamedExports extends Node {
1912-
parent: ExportDeclaration;
19131911
kind: SyntaxKind.NamedExports;
19141912
parent?: ExportDeclaration;
19151913
elements: NodeArray<ExportSpecifier>;
@@ -1925,7 +1923,6 @@ namespace ts {
19251923
}
19261924

19271925
export interface ExportSpecifier extends Declaration {
1928-
parent: NamedExports;
19291926
kind: SyntaxKind.ExportSpecifier;
19301927
parent?: NamedExports;
19311928
propertyName?: Identifier; // Name preceding "as" keyword (or undefined when "as" is absent)

src/harness/fourslash.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ namespace FourSlash {
992992

993993
for (const startRange of toArray(startRanges)) {
994994
this.goToRangeStart(startRange);
995-
const fullActual = this.findReferencesAtCaret().map<ReferenceJson>(({ definition, references }) => ({
995+
const fullActual = ts.map<ts.ReferencedSymbol, ReferenceJson>(this.findReferencesAtCaret(), ({ definition, references }) => ({
996996
definition: definition.displayParts.map(d => d.text).join(""),
997997
ranges: references
998998
}));
@@ -2383,7 +2383,7 @@ namespace FourSlash {
23832383
else {
23842384
if (actual === undefined) {
23852385
this.raiseError(`${name} failed - expected the template {newText: "${expected.newText}", caretOffset: "${expected.caretOffset}"} but got nothing instead`);
2386-
2386+
23872387
}
23882388

23892389
if (actual.newText !== expected.newText) {

src/services/documentHighlights.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,15 @@ namespace ts.DocumentHighlights {
2121
return referenceEntries && convertReferencedSymbols(referenceEntries);
2222
}
2323

24-
function convertReferencedSymbols(referenceEntries: ReferenceEntry[]): DocumentHighlights[] {
24+
function convertReferencedSymbols(referenceEntries: FindAllReferences.Entry[]): DocumentHighlights[] {
2525
const fileNameToDocumentHighlights = createMap<HighlightSpan[]>();
26-
for (const referenceEntry of referenceEntries) {
27-
const fileName = referenceEntry.fileName;
26+
for (const entry of referenceEntries) {
27+
const { fileName, span } = FindAllReferences.toHighlightSpan(entry);
2828
let highlightSpans = fileNameToDocumentHighlights.get(fileName);
2929
if (!highlightSpans) {
3030
fileNameToDocumentHighlights.set(fileName, highlightSpans = []);
3131
}
32-
33-
highlightSpans.push({
34-
textSpan: referenceEntry.textSpan,
35-
kind: referenceEntry.isWriteAccess ? HighlightSpanKind.writtenReference : HighlightSpanKind.reference
36-
});
32+
highlightSpans.push(span);
3733
}
3834

3935
return arrayFrom(fileNameToDocumentHighlights.entries(), ([fileName, highlightSpans ]) => ({ fileName, highlightSpans }));

src/services/findAllReferences.ts

Lines changed: 254 additions & 182 deletions
Large diffs are not rendered by default.

src/services/importTracker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ namespace ts.FindAllReferences {
491491
/** If at an export specifier, go to the symbol it refers to. */
492492
function skipExportSpecifierSymbol(symbol: Symbol, checker: TypeChecker): Symbol {
493493
// For `export { foo } from './bar", there's nothing to skip, because it does not create a new alias. But `export { foo } does.
494-
for (const declaration of symbol.declarations) {
494+
if (symbol.declarations) for (const declaration of symbol.declarations) {
495495
if (isExportSpecifier(declaration) && !(declaration as ExportSpecifier).propertyName && !(declaration as ExportSpecifier).parent.parent.moduleSpecifier) {
496496
return checker.getExportSpecifierLocalTargetSymbol(declaration);
497497
}

src/services/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ namespace ts {
479479
displayParts: SymbolDisplayPart[];
480480
}
481481

482+
//!!! internal implementation details leaked!!!
482483
export interface ReferencedSymbolOf<T extends DocumentSpan> {
483484
definition: ReferencedSymbolDefinitionInfo;
484485
references: T[];

src/services/utilities.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,21 @@ namespace ts {
11321132
return false;
11331133
}
11341134
}
1135+
1136+
/** True if the symbol is for an external module, as opposed to a namespace. */
1137+
export function isExternalModuleSymbol(moduleSymbol: Symbol): boolean {
1138+
Debug.assert(!!(moduleSymbol.flags & SymbolFlags.Module));
1139+
return moduleSymbol.name.charCodeAt(0) === CharacterCodes.doubleQuote;
1140+
}
1141+
1142+
/** Returns `true` the first time it encounters a node and `false` afterwards. */
1143+
export function nodeSeenTracker<T extends Node>(): (node: T) => boolean {
1144+
const seen: Array<true> = [];
1145+
return node => {
1146+
const id = getNodeId(node);
1147+
return !seen[id] && (seen[id] = true);
1148+
};
1149+
}
11351150
}
11361151

11371152
// Display-part writer helpers

0 commit comments

Comments
 (0)