( context: ResolutionContext, changedFiles: string[] )
| 1014 | * resolve. |
| 1015 | */ |
| 1016 | export function pythonReopenScope( |
| 1017 | context: ResolutionContext, |
| 1018 | changedFiles: string[] |
| 1019 | ): { targetFiles: string[]; sourceDirs: string[]; moduleLeaves: string[] } { |
| 1020 | const index = buildPythonModuleIndex(context); |
| 1021 | const roots = [...index.projectRoots, ...index.localRoots.keys()]; |
| 1022 | const moduleNames = (file: string): string[] => { |
| 1023 | const out: string[] = []; |
| 1024 | const mod = file.replace(/(?:^|\/)__init__\.py$/, '').replace(/\.py$/, ''); |
| 1025 | for (const root of roots) { |
| 1026 | if (root && !mod.startsWith(`${root}/`)) continue; |
| 1027 | const rel = root ? mod.slice(root.length + 1) : mod; |
| 1028 | if (rel) out.push(rel.replace(/\//g, '.')); |
| 1029 | } |
| 1030 | return out; |
| 1031 | }; |
| 1032 | const affected = new Set<string>(); |
| 1033 | const sourceDirs = new Set<string>(); |
| 1034 | for (const raw of changedFiles) { |
| 1035 | const file = raw.replace(/\\/g, '/'); |
| 1036 | if (!PYTHON_MODULE_FILE.test(file)) continue; |
| 1037 | for (const m of moduleNames(file)) affected.add(m); |
| 1038 | if (file === '__init__.py' || file.endsWith('/__init__.py')) sourceDirs.add(parentDir(file)); |
| 1039 | } |
| 1040 | const targetFiles: string[] = []; |
| 1041 | for (const file of index.files) { |
| 1042 | const hit = moduleNames(file).some((m) => { |
| 1043 | for (let i = m.length; i > 0; i = m.lastIndexOf('.', i - 1)) { |
| 1044 | if (affected.has(m.slice(0, i))) return true; |
| 1045 | } |
| 1046 | return false; |
| 1047 | }); |
| 1048 | if (hit) targetFiles.push(file); |
| 1049 | } |
| 1050 | const moduleLeaves = [...new Set([...affected].map((m) => m.slice(m.lastIndexOf('.') + 1)))]; |
| 1051 | return { targetFiles, sourceDirs: [...sourceDirs], moduleLeaves }; |
| 1052 | } |
| 1053 | |
| 1054 | /** |
| 1055 | * What an edit of these Python files can move through |
no test coverage detected