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

Function extract

src/resolution/frameworks/express.ts:203–282  ·  view source on GitHub ↗
(filePath, content)

Source from the content-addressed store, hash-verified

201 },
202
203 extract(filePath, content) {
204 if (!/\.(m?js|tsx?|cjs)$/.test(filePath)) return { nodes: [], references: [] };
205 const nodes: Node[] = [];
206 const references: UnresolvedRef[] = [];
207 const now = Date.now();
208 const lang = detectLanguage(filePath);
209 const safe = stripCommentsForRegex(content, lang);
210 if (ELYSIA_APP_FILE.test(safe)) return { nodes, references };
211 // Brackets and commas are counted on `structure`, where string and regex
212 // contents are blanked: `/[^]]/`, `/}/` or `'a, b)'` must not end an
213 // argument or a body early. Same offsets as `safe`, which the route path
214 // and reply calls are still read from.
215 // Match the route head up to the first arg: (app|router).METHOD('/path',
216 // (NOT the whole call — handlers are often inline arrows whose `)`/`{}` the
217 // old single-regex couldn't span, so inline-handler routes connected to nothing.)
218 const head = /\b(app|router)\s*\.\s*(get|post|put|patch|delete|all|use)\s*\(\s*['"]([^'"]+)['"]\s*,/g;
219 let match: RegExpExecArray | null;
220 while ((match = head.exec(safe)) !== null) {
221 const method = match[2]!;
222 const routePath = match[3]!;
223 if (method === 'use' && !routePath.startsWith('/')) continue;
224 const line = safe.slice(0, match.index).split('\n').length;
225 const routeNode: Node = {
226 id: `route:${filePath}:${line}:${method.toUpperCase()}:${routePath}`,
227 kind: 'route',
228 name: `${method.toUpperCase()} ${routePath}`,
229 qualifiedName: `${filePath}::${method.toUpperCase()}:${routePath}`,
230 filePath,
231 startLine: line,
232 endLine: line,
233 startColumn: 0,
234 endColumn: match[0].length,
235 language: lang,
236 updatedAt: now,
237 };
238 nodes.push(routeNode);
239
240 // The full argument list = balanced parens from the route call's open paren.
241 const openParen = safe.indexOf('(', match.index);
242 const call = openParen >= 0 ? scanCall(safe, openParen) : null;
243 const args = call ? call.structure.slice(1, -1) : '';
244 references.push(...handlerRefs(safe, openParen + 1, args, routeNode.id, line, filePath, lang));
245 }
246 // The chained form: `router.route('/:id').get(getProduct).put(protect, updateProduct)`
247 // — one path, several methods, each with its own handler. One route node
248 // per method, at the line of its `.method(`, bound like the plain form.
249 const chainHead = /\b(?:app|router)\s*\.\s*route\s*\(\s*['"]([^'"]+)['"]\s*\)/g;
250 while ((match = chainHead.exec(safe)) !== null) {
251 const routePath = match[1]!;
252 let at = match.index + match[0].length;
253 for (;;) {
254 const link = /^\s*\.\s*(get|post|put|patch|delete|all)\s*\(/.exec(safe.slice(at, at + 64));
255 if (!link) break;
256 const openParen = at + link[0].length - 1;
257 const call = scanCall(safe, openParen);
258 if (!call) break;
259 const closeParen = call.close;
260 const method = link[1]!;

Callers

nothing calls this directly

Calls 6

stripCommentsForRegexFunction · 0.90
scanCallFunction · 0.85
handlerRefsFunction · 0.85
pushMethod · 0.80
detectLanguageFunction · 0.70
execMethod · 0.65

Tested by

no test coverage detected