* Extract Python import mappings
(content: string)
| 2612 | * Extract Python import mappings |
| 2613 | */ |
| 2614 | function extractPythonImports(content: string): ImportMapping[] { |
| 2615 | const mappings: ImportMapping[] = []; |
| 2616 | const code = stripCommentsForRegex(content, 'python'); |
| 2617 | |
| 2618 | // from X import Y [as Z], … |
| 2619 | for (const { source, names } of pythonFromImports(code)) { |
| 2620 | for (const { name, alias } of names) { |
| 2621 | mappings.push({ |
| 2622 | localName: alias, |
| 2623 | exportedName: name, |
| 2624 | source, |
| 2625 | isDefault: false, |
| 2626 | isNamespace: false, |
| 2627 | }); |
| 2628 | } |
| 2629 | } |
| 2630 | |
| 2631 | // import X [as Y], … at module level. The mappings are the file's, so a |
| 2632 | // function-local import is left to its function: `import a.b as X` there |
| 2633 | // binds X in that function only (pythonFunctionModuleAlias). |
| 2634 | const importRe = /^import[ \t]+((?:[^\n\\;]|\\\r?\n)+)/gm; |
| 2635 | let match: RegExpExecArray | null; |
| 2636 | while ((match = importRe.exec(code)) !== null) { |
| 2637 | for (const raw of match[1]!.replace(/\\\r?\n/g, ' ').split(',')) { |
| 2638 | const im = raw.trim().match(/^([\w.]+)(?:\s+as\s+(\w+))?$/); |
| 2639 | if (!im) continue; |
| 2640 | mappings.push({ |
| 2641 | localName: im[2] || im[1]!.split('.').pop()!, |
| 2642 | exportedName: '*', |
| 2643 | source: im[1]!, |
| 2644 | isDefault: false, |
| 2645 | isNamespace: true, |
| 2646 | }); |
| 2647 | } |
| 2648 | } |
| 2649 | |
| 2650 | return mappings; |
| 2651 | } |
| 2652 | |
| 2653 | /** |
| 2654 | * Extract Go import mappings. An import is bound to the name written before |
no test coverage detected