( context: ResolutionContext, changedFiles: string[] )
| 1030 | * resolve. |
| 1031 | */ |
| 1032 | export function pythonReopenScope( |
| 1033 | context: ResolutionContext, |
| 1034 | changedFiles: string[] |
| 1035 | ): { targetFiles: string[]; sourceDirs: string[]; moduleLeaves: string[] } { |
| 1036 | const index = buildPythonModuleIndex(context); |
| 1037 | const roots = [...index.projectRoots, ...index.localRoots.keys()]; |
| 1038 | const moduleNames = (file: string): string[] => { |
| 1039 | const out: string[] = []; |
| 1040 | const mod = file.replace(/(?:^|\/)__init__\.py$/, '').replace(/\.py$/, ''); |
| 1041 | for (const root of roots) { |
| 1042 | if (root && !mod.startsWith(`${root}/`)) continue; |
| 1043 | const rel = root ? mod.slice(root.length + 1) : mod; |
| 1044 | if (rel) out.push(rel.replace(/\//g, '.')); |
| 1045 | } |
| 1046 | return out; |
| 1047 | }; |
| 1048 | const affected = new Set<string>(); |
| 1049 | const sourceDirs = new Set<string>(); |
| 1050 | for (const raw of changedFiles) { |
| 1051 | const file = raw.replace(/\\/g, '/'); |
| 1052 | if (!PYTHON_MODULE_FILE.test(file)) continue; |
| 1053 | for (const m of moduleNames(file)) affected.add(m); |
| 1054 | if (file === '__init__.py' || file.endsWith('/__init__.py')) sourceDirs.add(parentDir(file)); |
| 1055 | } |
| 1056 | const targetFiles: string[] = []; |
| 1057 | for (const file of index.files) { |
| 1058 | const hit = moduleNames(file).some((m) => { |
| 1059 | for (let i = m.length; i > 0; i = m.lastIndexOf('.', i - 1)) { |
| 1060 | if (affected.has(m.slice(0, i))) return true; |
| 1061 | } |
| 1062 | return false; |
| 1063 | }); |
| 1064 | if (hit) targetFiles.push(file); |
| 1065 | } |
| 1066 | const moduleLeaves = [...new Set([...affected].map((m) => m.slice(m.lastIndexOf('.') + 1)))]; |
| 1067 | return { targetFiles, sourceDirs: [...sourceDirs], moduleLeaves }; |
| 1068 | } |
| 1069 | |
| 1070 | /** |
| 1071 | * What an edit of these Python files can move through |
no test coverage detected