* Extract Python import mappings
(content: string)
| 2277 | * Extract Python import mappings |
| 2278 | */ |
| 2279 | function extractPythonImports(content: string): ImportMapping[] { |
| 2280 | const mappings: ImportMapping[] = []; |
| 2281 | const code = stripCommentsForRegex(content, 'python'); |
| 2282 | |
| 2283 | // from X import Y [as Z], … |
| 2284 | for (const { source, names } of pythonFromImports(code)) { |
| 2285 | for (const { name, alias } of names) { |
| 2286 | mappings.push({ |
| 2287 | localName: alias, |
| 2288 | exportedName: name, |
| 2289 | source, |
| 2290 | isDefault: false, |
| 2291 | isNamespace: false, |
| 2292 | }); |
| 2293 | } |
| 2294 | } |
| 2295 | |
| 2296 | // import X [as Y], … at module level. The mappings are the file's, so a |
| 2297 | // function-local import is left to its function: `import a.b as X` there |
| 2298 | // binds X in that function only (pythonFunctionModuleAlias). |
| 2299 | const importRe = /^import[ \t]+((?:[^\n\\;]|\\\r?\n)+)/gm; |
| 2300 | let match: RegExpExecArray | null; |
| 2301 | while ((match = importRe.exec(code)) !== null) { |
| 2302 | for (const raw of match[1]!.replace(/\\\r?\n/g, ' ').split(',')) { |
| 2303 | const im = raw.trim().match(/^([\w.]+)(?:\s+as\s+(\w+))?$/); |
| 2304 | if (!im) continue; |
| 2305 | mappings.push({ |
| 2306 | localName: im[2] || im[1]!.split('.').pop()!, |
| 2307 | exportedName: '*', |
| 2308 | source: im[1]!, |
| 2309 | isDefault: false, |
| 2310 | isNamespace: true, |
| 2311 | }); |
| 2312 | } |
| 2313 | } |
| 2314 | |
| 2315 | return mappings; |
| 2316 | } |
| 2317 | |
| 2318 | /** |
| 2319 | * Extract Go import mappings. An import is bound to the name written before |
no test coverage detected