| 2056 | * Extract import mappings from a file |
| 2057 | */ |
| 2058 | export function extractImportMappings( |
| 2059 | _filePath: string, |
| 2060 | content: string, |
| 2061 | language: Language |
| 2062 | ): ImportMapping[] { |
| 2063 | const mappings: ImportMapping[] = []; |
| 2064 | |
| 2065 | if (language === 'typescript' || language === 'javascript' || language === 'tsx' || language === 'jsx' || language === 'arkts') { |
| 2066 | mappings.push(...extractJSImports(content)); |
| 2067 | } else if (language === 'svelte' || language === 'vue' || language === 'astro') { |
| 2068 | // Svelte/Vue single-file components import via plain ES6 inside their |
| 2069 | // `<script>` block (Astro: the `---` frontmatter). Without this, a |
| 2070 | // `.svelte`/`.vue`/`.astro` consumer produces |
| 2071 | // zero import mappings, so `resolveViaImport` can't run and a barrel |
| 2072 | // import (`import { Foo } from './lib'`) falls back to name-matching — |
| 2073 | // which silently fails whenever the re-export alias differs from the |
| 2074 | // component's real name, yielding a false 0 callers (#629). The ES6 |
| 2075 | // import regex only matches `import … from '…'`, so running it over the |
| 2076 | // whole SFC (markup + styles included) is safe. |
| 2077 | mappings.push(...extractJSImports(content)); |
| 2078 | } else if (language === 'python') { |
| 2079 | mappings.push(...extractPythonImports(stripCommentsForRegex(content, 'python'))); |
| 2080 | } else if (language === 'go') { |
| 2081 | mappings.push(...extractGoImports(content)); |
| 2082 | } else if (language === 'java' || language === 'kotlin') { |
| 2083 | mappings.push(...extractJavaImports(content)); |
| 2084 | } else if (language === 'php') { |
| 2085 | mappings.push(...extractPHPImports(content)); |
| 2086 | } else if (language === 'c' || language === 'cpp') { |
| 2087 | mappings.push(...extractCppImports(content)); |
| 2088 | } |
| 2089 | |
| 2090 | return mappings; |
| 2091 | } |
| 2092 | |
| 2093 | /** A JavaScript identifier — `$`, `_` and Unicode letters included. Use with the `u` flag. */ |
| 2094 | const JS_IDENT = '[$_\\p{ID_Start}][$\\u200c\\u200d\\p{ID_Continue}]*'; |