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

Function extract

src/resolution/frameworks/nextjs.ts:251–322  ·  view source on GitHub ↗
(filePath: string, content: string)

Source from the content-addressed store, hash-verified

249 },
250
251 extract(filePath: string, content: string) {
252 const file = nextRouteForFile(filePath);
253 if (!file) return { nodes: [], references: [] };
254 const language = languageForFile(filePath);
255 const now = Date.now();
256 const nodes: Node[] = [];
257 const references: UnresolvedRef[] = [];
258 const stripped = stripCommentsForRegex(content, 'typescript');
259 const lineOf = (index: number): number => stripped.slice(0, index).split('\n').length;
260
261 if (file.kind === 'handler') {
262 // `export async function GET(req) {…}` / `export const POST = …` — one route per method.
263 const seen = new Set<string>();
264 const decl = new RegExp(`\\bexport\\s+(?:async\\s+)?function\\s+(${HTTP_EXPORTS})\\b|\\bexport\\s+(?:const|let)\\s+(${HTTP_EXPORTS})\\s*=`, 'g');
265 let m: RegExpExecArray | null;
266 while ((m = decl.exec(stripped)) !== null) {
267 const method = (m[1] ?? m[2])!;
268 if (seen.has(method)) continue;
269 seen.add(method);
270 const line = lineOf(m.index);
271 const node: Node = {
272 id: `route:${filePath}:${line}:${method}:${file.path}`,
273 kind: 'route',
274 name: `${method} ${file.path}`,
275 qualifiedName: `${filePath}::${method}:${file.path}`,
276 filePath,
277 startLine: line,
278 endLine: line,
279 startColumn: 0,
280 endColumn: m[0].length,
281 language,
282 isExported: true,
283 updatedAt: now,
284 };
285 nodes.push(node);
286 references.push({ fromNodeId: node.id, referenceName: method, referenceKind: 'references', line, column: 0, filePath, language, candidates: [method] });
287 }
288 return { nodes, references };
289 }
290
291 // A page, or a Pages Router API file: the default export is what runs.
292 const name = file.kind === 'api' ? `ANY ${file.path}` : file.path;
293 const node: Node = {
294 id: file.kind === 'api' ? `route:${filePath}:1:ANY:${file.path}` : `route:${filePath}:${file.path}`,
295 kind: 'route',
296 name,
297 qualifiedName: file.kind === 'api' ? `${filePath}::ANY:${file.path}` : `${filePath}::route:${file.path}`,
298 filePath,
299 startLine: 1,
300 endLine: 1,
301 startColumn: 0,
302 endColumn: 0,
303 language,
304 isExported: true,
305 updatedAt: now,
306 };
307 nodes.push(node);
308 const exported = defaultExportName(stripped);

Callers

nothing calls this directly

Calls 9

stripCommentsForRegexFunction · 0.90
defaultExportNameFunction · 0.90
nextRouteForFileFunction · 0.85
hasMethod · 0.80
pushMethod · 0.80
languageForFileFunction · 0.70
lineOfFunction · 0.70
execMethod · 0.65
addMethod · 0.45

Tested by

no test coverage detected