| 441 | } |
| 442 | |
| 443 | function extractDecoratorRoutes(filePath: string, content: string, opts: DecoratorRouteOpts): FrameworkExtractionResult { |
| 444 | const nodes: Node[] = []; |
| 445 | const references: UnresolvedRef[] = []; |
| 446 | const now = Date.now(); |
| 447 | let match: RegExpExecArray | null; |
| 448 | while ((match = opts.decoratorRegex.exec(content)) !== null) { |
| 449 | const routePath = match[opts.pathGroup]; |
| 450 | let method = opts.defaultMethod; |
| 451 | if (opts.methodGroup && match[opts.methodGroup]) { |
| 452 | method = match[opts.methodGroup]!.toUpperCase(); |
| 453 | } else if (opts.methodFromGroup && match[opts.methodFromGroup]) { |
| 454 | const m = match[opts.methodFromGroup]!.match(/['"]([A-Z]+)['"]/i); |
| 455 | if (m) method = m[1]!.toUpperCase(); |
| 456 | } |
| 457 | const line = lineAt(content, match.index); |
| 458 | const name = method ? `${method} ${routePath || '/'}` : (routePath || '/'); |
| 459 | const routeNode: Node = { |
| 460 | id: `route:${filePath}:${line}:${method}:${routePath}`, |
| 461 | kind: 'route', |
| 462 | name, |
| 463 | qualifiedName: `${filePath}::${method}:${routePath}`, |
| 464 | filePath, |
| 465 | startLine: line, |
| 466 | endLine: line, |
| 467 | startColumn: 0, |
| 468 | endColumn: match[0].length, |
| 469 | language: opts.language, |
| 470 | updatedAt: now, |
| 471 | }; |
| 472 | nodes.push(routeNode); |
| 473 | |
| 474 | let handlerName: string | undefined; |
| 475 | if (opts.handlerGroup && match[opts.handlerGroup]) { |
| 476 | handlerName = match[opts.handlerGroup]; |
| 477 | } else if (opts.findHandler) { |
| 478 | const tail = content.slice(match.index + match[0].length); |
| 479 | const defMatch = tail.match(/\n\s*(?:async\s+)?def\s+(\w+)/); |
| 480 | if (defMatch) handlerName = defMatch[1]; |
| 481 | } |
| 482 | if (handlerName) { |
| 483 | references.push({ |
| 484 | fromNodeId: routeNode.id, |
| 485 | referenceName: handlerName, |
| 486 | referenceKind: 'references', |
| 487 | line, |
| 488 | column: 0, |
| 489 | filePath, |
| 490 | language: 'python', |
| 491 | }); |
| 492 | } |
| 493 | } |
| 494 | return { nodes, references }; |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Flask-RESTful: `api.add_resource(ResourceClass, '/path'[, '/path2'])` |