* Parse a Django URL handler expression and return the symbol/module to link. * Returns null for shapes we can't confidently link (e.g. lambdas).
(expr: string)
| 161 | * Returns null for shapes we can't confidently link (e.g. lambdas). |
| 162 | */ |
| 163 | function resolveHandlerName(expr: string): { name: string; kind: 'references' | 'imports' } | null { |
| 164 | // include('module.path') |
| 165 | const includeMatch = expr.match(/^include\s*\(\s*['"]([^'"]+)['"]/); |
| 166 | if (includeMatch) return { name: includeMatch[1]!, kind: 'imports' }; |
| 167 | |
| 168 | // Strip trailing .as_view(...) or .as_view() |
| 169 | let head = expr.replace(/\.as_view\s*\([^)]*\)\s*$/, ''); |
| 170 | // Drop any other trailing method call |
| 171 | head = head.replace(/\.\w+\s*\([^)]*\)\s*$/, ''); |
| 172 | |
| 173 | const dotted = head.split('.').filter(Boolean); |
| 174 | if (dotted.length === 0) return null; |
| 175 | const last = dotted[dotted.length - 1]!; |
| 176 | if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(last)) return null; |
| 177 | |
| 178 | return { name: last, kind: 'references' }; |
| 179 | } |
| 180 | |
| 181 | export const flaskResolver: FrameworkResolver = { |
| 182 | name: 'flask', |