( ref: UnresolvedRef, imports: ImportMapping[], context: ResolutionContext )
| 4116 | } |
| 4117 | |
| 4118 | function resolveModuleImportToFile( |
| 4119 | ref: UnresolvedRef, |
| 4120 | imports: ImportMapping[], |
| 4121 | context: ResolutionContext |
| 4122 | ): ResolvedRef | null { |
| 4123 | if (ref.referenceKind !== 'imports') return null; |
| 4124 | if (ref.referenceName.includes('.')) return null; |
| 4125 | |
| 4126 | for (const imp of imports) { |
| 4127 | if (imp.localName !== ref.referenceName) continue; |
| 4128 | |
| 4129 | let modulePath: string; |
| 4130 | if (imp.isNamespace || imp.isDefault) { |
| 4131 | // `import * as ns from './x'` (namespace) or `import x from './x'` |
| 4132 | // (default) — the dependency is on the MODULE FILE. A default import binds |
| 4133 | // a (possibly renamed) local to whatever the module's default export is |
| 4134 | // (`import articlesController from './article.controller'` ← `export |
| 4135 | // default router`), so the binding name can't be found as a symbol — link |
| 4136 | // the file the import resolves to instead. External modules don't resolve |
| 4137 | // (no file), so `import React from 'react'` creates no edge. |
| 4138 | modulePath = imp.source; |
| 4139 | } else if (ref.language === 'python') { |
| 4140 | // `from . import certs` — the imported NAME is a submodule of the source, |
| 4141 | // unless the package itself binds that name (see pythonPackageBinds). |
| 4142 | // As in resolvePythonModuleMember, use the exported name so an alias |
| 4143 | // still links to the real module file (#1626). |
| 4144 | if (pythonPackageBinds(imp, ref.filePath, context)) continue; |
| 4145 | const moduleName = imp.exportedName === '*' ? imp.localName : imp.exportedName; |
| 4146 | modulePath = imp.source.endsWith('.') |
| 4147 | ? imp.source + moduleName |
| 4148 | : imp.source + '.' + moduleName; |
| 4149 | } else { |
| 4150 | // A named TS/JS import binds a symbol, not a module — leave it alone. |
| 4151 | continue; |
| 4152 | } |
| 4153 | |
| 4154 | const declaring = declaredModuleFile(modulePath, ref, context); |
| 4155 | if (declaring) return { original: ref, targetNodeId: declaring.id, confidence: 0.9, resolvedBy: 'import' }; |
| 4156 | |
| 4157 | const resolvedPath = resolveImportPath(modulePath, ref.filePath, ref.language, context); |
| 4158 | if (resolvedPath && resolvedPath !== ref.filePath) { |
| 4159 | const fileNode = context.getNodesInFile(resolvedPath).find((n) => n.kind === 'file'); |
| 4160 | if (fileNode) { |
| 4161 | return { original: ref, targetNodeId: fileNode.id, confidence: 0.9, resolvedBy: 'import' }; |
| 4162 | } |
| 4163 | } |
| 4164 | } |
| 4165 | return null; |
| 4166 | } |
| 4167 | |
| 4168 | /** |
| 4169 | * Resolve a Python dotted module import (`import a.b.c`, `from .a.b import x`) to its file — |
no test coverage detected