Skip to content

Fix import tracker for dynamic import #41473

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

Merged
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
44 changes: 29 additions & 15 deletions src/services/importTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ namespace ts.FindAllReferences {

switch (direct.kind) {
case SyntaxKind.CallExpression:
if (isImportCall(direct)) {
handleImportCall(direct);
break;
}
if (!isAvailableThroughGlobal) {
const parent = direct.parent;
if (exportKind === ExportKind.ExportEquals && parent.kind === SyntaxKind.VariableDeclaration) {
Expand Down Expand Up @@ -121,7 +125,7 @@ namespace ts.FindAllReferences {
}
else if (direct.exportClause.kind === SyntaxKind.NamespaceExport) {
// `export * as foo from "foo"` add to indirect uses
addIndirectUsers(getSourceFileLikeForImportDeclaration(direct));
addIndirectUser(getSourceFileLikeForImportDeclaration(direct), /** addTransitiveDependencies */ true);
}
else {
// This is `export { foo } from "foo"` and creates an alias symbol, so recursive search will get handle re-exports.
Expand All @@ -130,6 +134,10 @@ namespace ts.FindAllReferences {
break;

case SyntaxKind.ImportType:
// Only check for typeof import('xyz')
if (direct.isTypeOf && !direct.qualifier && isExported(direct)) {
addIndirectUser(direct.getSourceFile(), /** addTransitiveDependencies */ true);
}
directImports.push(direct);
break;

Expand All @@ -140,6 +148,18 @@ namespace ts.FindAllReferences {
}
}

function handleImportCall(importCall: ImportCall) {
const top = findAncestor(importCall, isAmbientModuleDeclaration) || importCall.getSourceFile();
addIndirectUser(top, /** addTransitiveDependencies */ !!isExported(importCall, /** stopAtAmbientModule */ true));
}

function isExported(node: Node, stopAtAmbientModule = false) {
return findAncestor(node, node => {
if (stopAtAmbientModule && isAmbientModuleDeclaration(node)) return "quit";
return some(node.modifiers, mod => mod.kind === SyntaxKind.ExportKeyword);
});
}

function handleNamespaceImport(importDeclaration: ImportEqualsDeclaration | ImportDeclaration, name: Identifier, isReExport: boolean, alreadyAddedDirect: boolean): void {
if (exportKind === ExportKind.ExportEquals) {
// This is a direct import, not import-as-namespace.
Expand All @@ -149,36 +169,30 @@ namespace ts.FindAllReferences {
const sourceFileLike = getSourceFileLikeForImportDeclaration(importDeclaration);
Debug.assert(sourceFileLike.kind === SyntaxKind.SourceFile || sourceFileLike.kind === SyntaxKind.ModuleDeclaration);
if (isReExport || findNamespaceReExports(sourceFileLike, name, checker)) {
addIndirectUsers(sourceFileLike);
addIndirectUser(sourceFileLike, /** addTransitiveDependencies */ true);
}
else {
addIndirectUser(sourceFileLike);
}
}
}

function addIndirectUser(sourceFileLike: SourceFileLike): boolean {
/** Adds a module and all of its transitive dependencies as possible indirect users. */
function addIndirectUser(sourceFileLike: SourceFileLike, addTransitiveDependencies = false): void {
Debug.assert(!isAvailableThroughGlobal);
const isNew = markSeenIndirectUser(sourceFileLike);
if (isNew) {
indirectUserDeclarations!.push(sourceFileLike); // TODO: GH#18217
}
return isNew;
}

/** Adds a module and all of its transitive dependencies as possible indirect users. */
function addIndirectUsers(sourceFileLike: SourceFileLike): void {
if (!addIndirectUser(sourceFileLike)) {
return;
}
if (!isNew) return;
indirectUserDeclarations!.push(sourceFileLike); // TODO: GH#18217

if (!addTransitiveDependencies) return;
const moduleSymbol = checker.getMergedSymbol(sourceFileLike.symbol);
if (!moduleSymbol) return;
Debug.assert(!!(moduleSymbol.flags & SymbolFlags.Module));
const directImports = getDirectImports(moduleSymbol);
if (directImports) {
for (const directImport of directImports) {
if (!isImportTypeNode(directImport)) {
addIndirectUsers(getSourceFileLikeForImportDeclaration(directImport));
addIndirectUser(getSourceFileLikeForImportDeclaration(directImport), /** addTransitiveDependencies */ true);
}
}
}
Expand Down
98 changes: 98 additions & 0 deletions tests/baselines/reference/findAllRefsForImportCall.baseline.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// === /app.ts ===
// /*FIND ALL REFS*/export function [|hello|]() {};

// === /indirect-use.ts ===
// import("./re-export").then(mod => mod.services.app.[|hello|]());

// === /direct-use.ts ===
// async function main() {
// const mod = await import("./app")
// mod.[|hello|]();
// }

[
{
"definition": {
"containerKind": "",
"containerName": "",
"fileName": "/app.ts",
"kind": "function",
"name": "function hello(): void",
"textSpan": {
"start": 16,
"length": 5
},
"displayParts": [
{
"text": "function",
"kind": "keyword"
},
{
"text": " ",
"kind": "space"
},
{
"text": "hello",
"kind": "functionName"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": ")",
"kind": "punctuation"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "void",
"kind": "keyword"
}
],
"contextSpan": {
"start": 0,
"length": 26
}
},
"references": [
{
"textSpan": {
"start": 16,
"length": 5
},
"fileName": "/app.ts",
"contextSpan": {
"start": 0,
"length": 26
},
"isWriteAccess": true,
"isDefinition": true
},
{
"textSpan": {
"start": 51,
"length": 5
},
"fileName": "/indirect-use.ts",
"isWriteAccess": false,
"isDefinition": false
},
{
"textSpan": {
"start": 70,
"length": 5
},
"fileName": "/direct-use.ts",
"isWriteAccess": false,
"isDefinition": false
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// === /app.ts ===
// /*FIND ALL REFS*/export function [|hello|]() {};

// === /indirect-use.ts ===
// import type { app } from "./re-export";
// declare const app: app
// app.[|hello|]();

[
{
"definition": {
"containerKind": "",
"containerName": "",
"fileName": "/app.ts",
"kind": "function",
"name": "function hello(): void",
"textSpan": {
"start": 16,
"length": 5
},
"displayParts": [
{
"text": "function",
"kind": "keyword"
},
{
"text": " ",
"kind": "space"
},
{
"text": "hello",
"kind": "functionName"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": ")",
"kind": "punctuation"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "void",
"kind": "keyword"
}
],
"contextSpan": {
"start": 0,
"length": 26
}
},
"references": [
{
"textSpan": {
"start": 16,
"length": 5
},
"fileName": "/app.ts",
"contextSpan": {
"start": 0,
"length": 26
},
"isWriteAccess": true,
"isDefinition": true
},
{
"textSpan": {
"start": 67,
"length": 5
},
"fileName": "/indirect-use.ts",
"isWriteAccess": false,
"isDefinition": false
}
]
}
]
Loading