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

Function commonJsRequireRefs

src/extraction/commonjs-requires.ts:44–68  ·  view source on GitHub ↗
(filePath: string, source: string, language: Language)

Source from the content-addressed store, hash-verified

42
43/** The file node both extractors mint: `file:<path>`. */
44export function commonJsRequireRefs(filePath: string, source: string, language: Language): UnresolvedReference[] {
45 if (!COMMONJS_LANGUAGES.has(language) || !source.includes('require')) return [];
46 const safe = stripCommentsForRegex(source, language === 'javascript' || language === 'jsx' ? 'javascript' : 'typescript');
47 const out: UnresolvedReference[] = [];
48 const seen = new Set<string>();
49 REQUIRE_CALL.lastIndex = 0;
50 let m: RegExpExecArray | null;
51 while ((m = REQUIRE_CALL.exec(safe)) !== null) {
52 const specifier = (m[1] ?? m[2] ?? m[3] ?? '').trim();
53 if (!LOCAL_SPECIFIER.test(specifier) || seen.has(specifier)) continue;
54 const lineStart = safe.lastIndexOf('\n', m.index - 1) + 1;
55 if (IMPORT_EQUALS.test(safe.slice(lineStart, m.index))) continue;
56 seen.add(specifier);
57 out.push({
58 fromNodeId: `file:${filePath}`,
59 referenceName: specifier,
60 referenceKind: 'imports',
61 line: safe.slice(0, m.index).split('\n').length,
62 column: m.index - lineStart,
63 filePath,
64 language,
65 });
66 }
67 return out;
68}

Callers 3

parse-worker.tsFile · 0.90
extractMethod · 0.90
tryKernelExtractFunction · 0.90

Calls 5

stripCommentsForRegexFunction · 0.90
hasMethod · 0.80
pushMethod · 0.80
execMethod · 0.65
addMethod · 0.45

Tested by

no test coverage detected