Skip to content

[WIP] Reference missing with declare module #61854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
25 changes: 18 additions & 7 deletions src/services/findAllReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1687,13 +1687,24 @@ export namespace Core {
scope = next;
}
}
}

// If symbol.parent, this means we are in an export of an external module. (Otherwise we would have returned `undefined` above.)
// For an export of a module, we may be in a declaration file, and it may be accessed elsewhere. E.g.:
// declare module "a" { export type T = number; }
// declare module "b" { import { T } from "a"; export const x: T; }
// So we must search the whole source file. (Because we will mark the source file as seen, we we won't return to it when searching for imports.)
}

// If symbol.parent, this means we are in an export of an external module. (Otherwise we would have returned `undefined` above.)
// For an export of a module, we may be in a declaration file, and it may be accessed elsewhere. E.g.:
// declare module "a" { export type T = number; }
// declare module "b" { import { T } from "a"; export const x: T; }
// So we must search the whole source file. (Because we will mark the source file as seen, we we won't return to it when searching for imports.)
// However, for declare modules, the exports are globally accessible and can be used from any file, so we should do a global search.
if (exposedByParent && parent && isExternalModuleSymbol(parent)) {
// Check if this is a declare module by examining the declarations
const isInDeclareModule = parent.declarations?.some(decl =>
isModuleDeclaration(decl) && (decl.flags & NodeFlags.GlobalAugmentation) === 0
);
if (isInDeclareModule) {
// For declare modules, do a global search since their exports can be accessed from anywhere
return undefined;
}
}
return exposedByParent ? scope!.getSourceFile() : scope; // TODO: GH#18217
}

Expand Down
15 changes: 15 additions & 0 deletions tests/cases/fourslash/findAllRefsWithDeclareModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path="fourslash.ts" />

////declare module '@bug/api/index' {
//// export * from "@bug/api/miscFunctions";
////}
////declare module '@bug/api/miscFunctions' {
//// export function /*1*/myFunction(testParam: string): Promise<any>;
////}
////
////declare namespace bug.v0 {const api: typeof import('@bug/api/index')}
////
//////// test.ts
////bug.v0.api./*2*/myFunction('test')

verify.baselineFindAllReferences('1', '2');
23 changes: 23 additions & 0 deletions tests/cases/fourslash/findAllRefsWithDeclareModuleDuplicate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// <reference path="fourslash.ts" />

////declare module '@bug/api/index' {
//// export * from "@bug/api/miscFunctions";
////}
////declare module '@bug/api/miscFunctions' {
//// export function /*1*/myFunction(testParam: string): Promise<any>;
////}
////
////declare namespace bug.v0 {const api: typeof import('@bug/api/index')}

////declare module '@bug/api/index' {
//// export * from "@bug/api/miscFunctions";
////}
////declare module '@bug/api/miscFunctions' {
//// export function /*2*/myFunction(testParam: string): Promise<any>;
////}
////
////declare namespace bug.v0 {const api: typeof import('@bug/api/index')}

////bug.v0.api./*3*/myFunction('test')

verify.baselineFindAllReferences('1', '2', '3');
Loading