* Extract hook implementation references from a Drupal PHP file. * * Strategy A (primary): look for docblocks containing `Implements hook_X().` * followed immediately by the function definition. This is the Drupal coding * standard and is precise. * * Strategy B (fallback): for functions whose
( filePath: string, content: string )
| 235 | * canonical hook name, e.g. `hook_form_alter`. |
| 236 | */ |
| 237 | function extractDrupalHooks( |
| 238 | filePath: string, |
| 239 | content: string |
| 240 | ): { nodes: Node[]; references: UnresolvedRef[] } { |
| 241 | const references: UnresolvedRef[] = []; |
| 242 | |
| 243 | // Build a map of function name → 1-indexed line number for all top-level functions. |
| 244 | // This mirrors tree-sitter's line numbering so we can reconstruct node IDs. |
| 245 | const funcLineMap = new Map<string, number>(); |
| 246 | const funcDef = /^function\s+(\w+)\s*\(/gm; |
| 247 | let fm: RegExpExecArray | null; |
| 248 | while ((fm = funcDef.exec(content)) !== null) { |
| 249 | const name = fm[1]!; |
| 250 | if (!funcLineMap.has(name)) { |
| 251 | // line = number of newlines before match start + 1 |
| 252 | funcLineMap.set(name, content.slice(0, fm.index).split('\n').length); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | const emitHookRef = (hookName: string, funcName: string) => { |
| 257 | const lineNum = funcLineMap.get(funcName); |
| 258 | if (lineNum === undefined) return; |
| 259 | const nodeId = generateNodeId(filePath, 'function', funcName, lineNum); |
| 260 | references.push({ |
| 261 | fromNodeId: nodeId, |
| 262 | referenceName: hookName, |
| 263 | referenceKind: 'references', |
| 264 | line: lineNum, |
| 265 | column: 0, |
| 266 | filePath, |
| 267 | language: 'php', |
| 268 | }); |
| 269 | }; |
| 270 | |
| 271 | // Strategy A: docblock `Implements hook_X().` followed by function definition. |
| 272 | // The docblock and function may be separated by blank lines. |
| 273 | const docblockPattern = |
| 274 | /\/\*\*[\s\S]*?(?:@|\*\s+)Implements\s+(hook_\w+)\s*\(\)[\s\S]*?\*\/\s*\n(?:\s*\n)*function\s+(\w+)\s*\(/g; |
| 275 | const docblockMatched = new Set<string>(); |
| 276 | let match: RegExpExecArray | null; |
| 277 | while ((match = docblockPattern.exec(content)) !== null) { |
| 278 | const [, hookName, funcName] = match; |
| 279 | emitHookRef(hookName!, funcName!); |
| 280 | docblockMatched.add(funcName!); |
| 281 | } |
| 282 | |
| 283 | // Strategy B: fallback name-pattern matching for functions without docblocks. |
| 284 | // Only applies to functions whose name starts with {moduleName}_ and that were |
| 285 | // not already matched by Strategy A. |
| 286 | const moduleName = moduleNameFromPath(filePath); |
| 287 | if (moduleName) { |
| 288 | const prefix = moduleName + '_'; |
| 289 | for (const [funcName] of funcLineMap) { |
| 290 | if (docblockMatched.has(funcName)) continue; |
| 291 | if (!funcName.startsWith(prefix)) continue; |
| 292 | const hookSuffix = funcName.slice(prefix.length); |
| 293 | if (!hookSuffix) continue; |
| 294 | // Emit a reference to hook_{suffix} — the resolver will link it if the |
no test coverage detected