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