MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / extractImportMappings

Function extractImportMappings

src/resolution/import-resolver.ts:2058–2091  ·  view source on GitHub ↗
(
  _filePath: string,
  content: string,
  language: Language
)

Source from the content-addressed store, hash-verified

2056 * Extract import mappings from a file
2057 */
2058export 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. */
2094const JS_IDENT = '[$_\\p{ID_Start}][$\\u200c\\u200d\\p{ID_Continue}]*';

Callers 9

readFunction · 0.90
createContextMethod · 0.90
mappingsFunction · 0.90
bindingsFunction · 0.90
namesFunction · 0.90
resolution.test.tsFile · 0.90
namesFunction · 0.90
mappingsFunction · 0.90

Calls 8

stripCommentsForRegexFunction · 0.90
extractJSImportsFunction · 0.85
extractPythonImportsFunction · 0.85
extractGoImportsFunction · 0.85
extractJavaImportsFunction · 0.85
extractPHPImportsFunction · 0.85
extractCppImportsFunction · 0.85
pushMethod · 0.80

Tested by 5

mappingsFunction · 0.72
bindingsFunction · 0.72
namesFunction · 0.72
namesFunction · 0.72
mappingsFunction · 0.72