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

Function extract

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

Source from the content-addressed store, hash-verified

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

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