Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,8 @@ export class DeclarationReferenceGenerator {
if (!includeModuleSymbols) {
return undefined;
}
const sourceFile: ts.SourceFile | undefined =
followedSymbol.declarations &&
followedSymbol.declarations[0] &&
followedSymbol.declarations[0].getSourceFile();
const declaration: ts.Node | undefined = TypeScriptHelpers.tryGetADeclaration(symbol);
const sourceFile: ts.SourceFile | undefined = declaration?.getSourceFile();
return new DeclarationReference(this._sourceFileToModuleSource(sourceFile));
}

Expand All @@ -242,28 +240,8 @@ export class DeclarationReferenceGenerator {
return undefined;
}

const parent: ts.Symbol | undefined = TypeScriptInternals.getSymbolParent(followedSymbol);
let parentRef: DeclarationReference | undefined;
if (parent) {
parentRef = this._symbolToDeclarationReference(
parent,
ts.SymbolFlags.Namespace,
/*includeModuleSymbols*/ true
);
} else {
// this may be a local symbol in a module...
const sourceFile: ts.SourceFile | undefined =
followedSymbol.declarations &&
followedSymbol.declarations[0] &&
followedSymbol.declarations[0].getSourceFile();
if (sourceFile && ts.isExternalModule(sourceFile)) {
parentRef = new DeclarationReference(this._sourceFileToModuleSource(sourceFile));
} else {
parentRef = new DeclarationReference(GlobalSource.instance);
}
}

if (parentRef === undefined) {
let parentRef: DeclarationReference | undefined = this._getParentReference(followedSymbol);
if (!parentRef) {
return undefined;
}

Expand Down Expand Up @@ -306,6 +284,55 @@ export class DeclarationReferenceGenerator {
.withMeaning(DeclarationReferenceGenerator._getMeaningOfSymbol(followedSymbol, meaning));
}

private _getParentReference(symbol: ts.Symbol): DeclarationReference | undefined {
const declaration: ts.Node | undefined = TypeScriptHelpers.tryGetADeclaration(symbol);

// First, try to find a parent symbol via the symbol tree.
const parentSymbol: ts.Symbol | undefined = TypeScriptInternals.getSymbolParent(symbol);
if (parentSymbol) {
return this._symbolToDeclarationReference(
parentSymbol,
parentSymbol.flags,
/*includeModuleSymbols*/ true
);
}

// If that doesn't work, try to find a parent symbol via the node tree. As far as we can tell,
// this logic is only needed for local symbols within namespaces. For example:
//
// ```
// export namespace n {
// type SomeType = number;
// export function someFunction(): SomeType { return 5; }
// }
// ```
//
// In the example above, `SomeType` doesn't have a parent symbol per the TS internal API above,
// but its reference still needs to be qualified with the parent reference for `n`.
const grandParent: ts.Node | undefined = declaration?.parent?.parent;
if (grandParent && ts.isModuleDeclaration(grandParent)) {
const grandParentSymbol: ts.Symbol | undefined = TypeScriptInternals.tryGetSymbolForDeclaration(
grandParent,
this._typeChecker
);
if (grandParentSymbol) {
return this._symbolToDeclarationReference(
grandParentSymbol,
grandParentSymbol.flags,
/*includeModuleSymbols*/ true
);
}
}
Comment on lines +300 to +325
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the new logic that fixes the bug.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code looks okay, but these sorts of changes sometimes cause a regression due to some obscure edge case. Let's publish this as as separate PATCH release from your other fix, and then we can get some large monorepos to try it.


// At this point, we have a local symbol in a module.
const sourceFile: ts.SourceFile | undefined = declaration?.getSourceFile();
if (sourceFile && ts.isExternalModule(sourceFile)) {
return new DeclarationReference(this._sourceFileToModuleSource(sourceFile));
} else {
return new DeclarationReference(GlobalSource.instance);
}
}

private _getPackageName(sourceFile: ts.SourceFile): string {
if (this._program.isSourceFileFromExternalLibrary(sourceFile)) {
const packageJson: INodePackageJson | undefined = this._packageJsonLookup.tryLoadNodePackageJsonFor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
"namedDefaultImport",
"preapproved",
"readonlyDeclarations",
"referenceTokens",
"spanSorting",
"typeLiterals",
"typeOf",
"typeOf2",
"typeOf3",
Expand Down
Loading