|
| 1 | +# Go Template Parser — Structural Analysis |
| 2 | + |
| 3 | +> Added in PR #47 · Branch: `feat/go-template-parser` → `dev` |
| 4 | +
|
| 5 | +## Overview |
| 6 | + |
| 7 | +Adds full structural analysis support for **Go HTML templates** (`.html`, `.tmpl`, `.gohtml`) to the RagCode indexer. The new parser extracts template definitions, includes, block relationships, control-flow blocks, variables, and custom functions — tracking them either as emitted `parser.Symbol` entries, relational edges like `RelInheritance`, or structured `metadata` attached to parent symbols. |
| 8 | + |
| 9 | +## New Files |
| 10 | + |
| 11 | +| File | Purpose | |
| 12 | +|------|---------| |
| 13 | +| `pkg/parser/html/gotemplate/analyzer.go` | Line-by-line scanner: extracts defines, blocks, templates, range/with/if/else-if, variables, custom funcs | |
| 14 | +| `pkg/parser/html/gotemplate/adapter.go` | Converts `GoTemplate` structs → `parser.Symbol` + `RelDependency` relations | |
| 15 | +| `pkg/parser/html/gotemplate/analyzer_test.go` | Unit tests covering layout, page, partial, multi-file, else-if, block relations | |
| 16 | + |
| 17 | +## Modified Files |
| 18 | + |
| 19 | +| File | Change | |
| 20 | +|------|--------| |
| 21 | +| `pkg/parser/html/analyzer.go` | Integrates GoTemplate detection into the existing HTML analyzer (single directory walk, logger-based error handling) | |
| 22 | +| `pkg/parser/go/analyzer.go` | Template file dependency extraction also handles `*ast.Ident` calls (dot-imports, wrappers) | |
| 23 | +| `internal/uninstall/uninstall.go` | V2 registry detection tightened: `Version == "v2"` instead of `!= ""` | |
| 24 | + |
| 25 | +## What Gets Indexed |
| 26 | + |
| 27 | +For each `.html` / `.tmpl` / `.gohtml` file containing `{{` syntax: |
| 28 | + |
| 29 | +- **`{{define "name"}}`** → `parser.Type` symbol with `metadata.template_type = go_template_define` and start/end lines (plus a separate file-level `go_template` symbol) |
| 30 | +- **`{{block "name" .}}`** → represented via `RelInheritance` relations + `blocks` metadata on the file-level template symbol (no standalone `block` symbol) |
| 31 | +- **`{{template "name" .}}`** → `RelDependency` edge from current template to included one |
| 32 | +- **`{{range .Items}}`** → recorded in template metadata; **`{{with .Obj}}`** → only its source ranges are tracked (no dedicated symbol metadata yet) |
| 33 | +- **`{{if ...}}` / `{{else if ...}}`** → correct stack handling (no extra `{{ end }}` consumed) |
| 34 | +- **`.Variables`** → all dot-variables extracted from inside any `{{ ... }}` action, including pipelines like `{{ .Body | truncate 200 }}` and lowercase vars like `.user`, `.items` |
| 35 | +- **Custom functions** → non-keyword identifiers followed by arguments |
| 36 | + |
| 37 | +## Architecture |
| 38 | + |
| 39 | +``` |
| 40 | +html/analyzer.go |
| 41 | + ├─ WalkDir (GoTemplate detection) |
| 42 | + │ └─ gotemplate/analyzer.go (analyzeFile) |
| 43 | + │ └─ gotemplate/adapter.go (ConvertToSymbols) |
| 44 | + └─ ca.AnalyzePaths (HTML DOM analysis) |
| 45 | +``` |
| 46 | + |
| 47 | +## Review Fixes Applied (PR #47) |
| 48 | + |
| 49 | +| # | Issue | Fix | |
| 50 | +|---|-------|-----| |
| 51 | +| 1 | Double I/O in directory walk | Reduced for GoTemplate; DOM analysis still performs separate walk | |
| 52 | +| 2 | `WalkDir` errors silently ignored | Logged via `logger.Instance.Debug` | |
| 53 | +| 3 | `fmt.Fprintf(os.Stderr)` in library code | Replaced with project logger | |
| 54 | +| 4 | `*ast.Ident` template deps missed | Added Ident case in `extractCallsFromAST` | |
| 55 | +| 5 | V2 registry: loose version check | `== "v2"` exact match | |
| 56 | +| 6 | Variables in pipelines missed | `reAction` + `reActionVar` pattern replacing narrow `reVariable` | |
| 57 | +| 7 | `reActionVar` missed lowercase vars | Regex broadened to `[A-Za-z_]` | |
| 58 | +| 8 | `htmlPaths` collected but unused | Variable removed entirely | |
| 59 | +| 9 | scanner.Err() unchecked after scan | Fixed: added check in analyzeFile | |
| 60 | + |
| 61 | +## Tests |
| 62 | + |
| 63 | +```bash |
| 64 | +go test ./pkg/parser/html/... |
| 65 | +go test ./pkg/parser/go/... |
| 66 | +go test ./internal/uninstall/... |
| 67 | +``` |
| 68 | + |
| 69 | +All pass ✅ |
0 commit comments