( context: ResolutionContext, initFiles: string[] )
| 1118 | * its own importers are included too, to a fixed point. |
| 1119 | */ |
| 1120 | export function pythonPackageImporters( |
| 1121 | context: ResolutionContext, |
| 1122 | initFiles: string[] |
| 1123 | ): Map<string, Map<string, string | null>> { |
| 1124 | const index = pythonModuleIndex(context); |
| 1125 | const packages = new Set(initFiles.map((f) => f.replace(/\\/g, '/'))); |
| 1126 | const out = new Map<string, Map<string, string | null>>(); |
| 1127 | const bindingNow = (imp: ImportMapping, file: string): string | null => { |
| 1128 | if (imp.isNamespace || imp.exportedName === '*') return null; |
| 1129 | const packageFile = resolveImportPath(imp.source, file, 'python', context); |
| 1130 | if (!packageFile) return null; |
| 1131 | const symbol = findExportedSymbol( |
| 1132 | packageFile, |
| 1133 | { isDefault: false, isNamespace: false, exportedName: imp.exportedName, memberName: null }, |
| 1134 | 'python', |
| 1135 | context, |
| 1136 | new Set() |
| 1137 | ); |
| 1138 | if (symbol) return `${symbol.filePath}\0${symbol.name}`; |
| 1139 | const sub = resolveImportPath( |
| 1140 | imp.source.endsWith('.') ? imp.source + imp.exportedName : `${imp.source}.${imp.exportedName}`, |
| 1141 | file, |
| 1142 | 'python', |
| 1143 | context |
| 1144 | ); |
| 1145 | return sub ? `${sub}\0` : '\0'; |
| 1146 | }; |
| 1147 | // Candidate importers from the import nodes (named by their specifier), |
| 1148 | // so only files that import an edited package pay for a mapping parse. |
| 1149 | const importNodes = context.getNodesByKind('import').filter((n) => PYTHON_MODULE_FILE.test(n.filePath)); |
| 1150 | for (let grew = true; grew; ) { |
| 1151 | grew = false; |
| 1152 | const candidates = new Set<string>(); |
| 1153 | for (const n of importNodes) { |
| 1154 | const target = resolveImportPath(n.name, n.filePath, 'python', context); |
| 1155 | if (target && packages.has(target) && index.files.has(n.filePath)) candidates.add(n.filePath); |
| 1156 | } |
| 1157 | for (const file of candidates) { |
| 1158 | for (const imp of context.getImportMappings(file, 'python')) { |
| 1159 | const target = resolveImportPath(imp.source, file, 'python', context); |
| 1160 | if (!target || !packages.has(target) || target === file) continue; |
| 1161 | let names = out.get(file); |
| 1162 | if (!names) out.set(file, (names = new Map())); |
| 1163 | names.set(imp.localName, bindingNow(imp, file)); |
| 1164 | // `import pkg.sub` is used as `pkg.sub.N`. |
| 1165 | if (imp.isNamespace) names.set(imp.source.split('.')[0]!, null); |
| 1166 | if (/(?:^|\/)__init__\.py$/.test(file) && !packages.has(file)) { |
| 1167 | packages.add(file); |
| 1168 | grew = true; |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | return out; |
| 1174 | } |
| 1175 | |
| 1176 | /** The file node of the module `specifier` names, as seen from `fromFile`. */ |
| 1177 | function pythonModuleFileNode(specifier: string, fromFile: string, context: ResolutionContext): Node | null { |
no test coverage detected