* Extract Python import mappings
(content: string)
| 900 | * Extract Python import mappings |
| 901 | */ |
| 902 | function extractPythonImports(content: string): ImportMapping[] { |
| 903 | const mappings: ImportMapping[] = []; |
| 904 | |
| 905 | // from X import Y |
| 906 | const fromImportRegex = /from\s+([\w.]+)\s+import\s+([^#\n]+)/g; |
| 907 | let match; |
| 908 | |
| 909 | while ((match = fromImportRegex.exec(content)) !== null) { |
| 910 | const [, source, imports] = match; |
| 911 | const names = imports!.split(',').map((s) => s.trim()); |
| 912 | |
| 913 | for (const name of names) { |
| 914 | const aliasMatch = name.match(/(\w+)\s+as\s+(\w+)/); |
| 915 | if (aliasMatch) { |
| 916 | mappings.push({ |
| 917 | localName: aliasMatch[2]!, |
| 918 | exportedName: aliasMatch[1]!, |
| 919 | source: source!, |
| 920 | isDefault: false, |
| 921 | isNamespace: false, |
| 922 | }); |
| 923 | } else if (name && name !== '*') { |
| 924 | mappings.push({ |
| 925 | localName: name, |
| 926 | exportedName: name, |
| 927 | source: source!, |
| 928 | isDefault: false, |
| 929 | isNamespace: false, |
| 930 | }); |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | // import X |
| 936 | const importRegex = /^import\s+([\w.]+)(?:\s+as\s+(\w+))?/gm; |
| 937 | while ((match = importRegex.exec(content)) !== null) { |
| 938 | const [, source, alias] = match; |
| 939 | const localName = alias || source!.split('.').pop()!; |
| 940 | mappings.push({ |
| 941 | localName, |
| 942 | exportedName: '*', |
| 943 | source: source!, |
| 944 | isDefault: false, |
| 945 | isNamespace: true, |
| 946 | }); |
| 947 | } |
| 948 | |
| 949 | return mappings; |
| 950 | } |
| 951 | |
| 952 | /** |
| 953 | * Extract Go import mappings |
no test coverage detected