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

Function extractFlaskUrlRules

src/resolution/frameworks/python.ts:552–610  ·  view source on GitHub ↗

* Flask's imperative registration: `bp.add_url_rule('/path', 'name', * view_func=View.as_view('name'), methods=[…])`, and a project's own wrapper * around it — any call handing a list of path strings and a `view_func=` * over, like flaskbb's `register_view(auth, routes=["/login"], * view_func=Lo

(filePath: string, safe: string)

Source from the content-addressed store, hash-verified

550 * says; a plain function defaults to GET, as Flask does.
551 */
552function extractFlaskUrlRules(filePath: string, safe: string): FrameworkExtractionResult {
553 const nodes: Node[] = [];
554 const references: UnresolvedRef[] = [];
555 const now = Date.now();
556 const seen = new Set<string>();
557 const opener = /(\.add_url_rule|\b[A-Za-z_]\w*)\s*\(/g;
558 let m: RegExpExecArray | null;
559 while ((m = opener.exec(safe)) !== null) {
560 const open = m.index + m[0].length;
561 const args = balancedArgs(safe, open);
562 if (args === null || !/\bview_func\s*=|\.add_url_rule/.test(m[1]! + args)) continue;
563 const isRule = m[1] === '.add_url_rule';
564 let paths: string[] = [];
565 if (isRule) {
566 const first = /^\s*(?:rule\s*=\s*)?(['"])([^'"]*)\1/.exec(args);
567 if (first) paths = [first[2]!];
568 } else {
569 const list = /(?:^|,)\s*(?:routes|rules|urls|paths)?\s*=?\s*\[((?:\s*(['"])[^'"]*\2\s*,?)+)\]/.exec(args);
570 if (list) paths = (list[1]!.match(/(['"])([^'"]*)\1/g) ?? []).map((q) => q.slice(1, -1));
571 }
572 paths = paths.filter((p) => p.startsWith('/'));
573 if (paths.length === 0) continue;
574 // `view_func=X` / `view_func=X.as_view(…)`, or add_url_rule's third positional argument.
575 const expr = /\bview_func\s*=\s*([A-Za-z_][\w.]*)/.exec(args)?.[1] ??
576 (isRule ? /^\s*(['"])[^'"]*\1\s*,\s*(?:(['"])[^'"]*\2|None)\s*,\s*([A-Za-z_][\w.]*)/.exec(args)?.[3] : undefined);
577 if (!expr) continue;
578 const classView = expr.endsWith('.as_view');
579 const target = classView ? expr.slice(0, -'.as_view'.length) : expr;
580 const listed = /\bmethods\s*=\s*[[(]([^\])]*)[\])]/.exec(args)?.[1];
581 const methods = listed
582 ? (listed.match(/['"](\w+)['"]/g) ?? []).map((q) => q.slice(1, -1).toUpperCase())
583 : [classView ? 'ANY' : 'GET'];
584 const line = lineAt(safe, m.index);
585 const name = target.split('.').pop()!;
586 for (const routePath of paths) {
587 for (const method of methods) {
588 const id = `route:${filePath}:${line}:${method}:${routePath}`;
589 if (seen.has(id)) continue;
590 seen.add(id);
591 nodes.push({
592 id,
593 kind: 'route',
594 name: `${method} ${routePath}`,
595 qualifiedName: `${filePath}::${method}:${routePath}`,
596 filePath,
597 startLine: line,
598 endLine: line,
599 startColumn: 0,
600 endColumn: 0,
601 language: 'python',
602 updatedAt: now,
603 });
604 references.push({ fromNodeId: id, referenceName: name, referenceKind: 'references', line, column: 0, filePath, language: 'python' });
605 }
606 }
607 opener.lastIndex = open + args.length;
608 }
609 return { nodes, references };

Callers 1

extractFunction · 0.85

Calls 7

lineAtFunction · 0.90
balancedArgsFunction · 0.85
mapMethod · 0.80
hasMethod · 0.80
pushMethod · 0.80
execMethod · 0.65
addMethod · 0.45

Tested by

no test coverage detected