608e53be
feat(scanner): detect untagged @claude markers in Go comments
a73x 2026-04-29 05:42
Commit message
internal/scanner/scanner.go
| Old | New | ||
|---|---|---|---|
| @@ -0,0 +1,60 @@ | |||
| 1 | package scanner | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "bufio" | ||
| 5 | "os" | ||
| 6 | "path/filepath" | ||
| 7 | "regexp" | ||
| 8 | "strings" | ||
| 9 | ) | ||
| 10 | |||
| 11 | type Marker struct { | ||
| 12 | Line int | ||
| 13 | Text string | ||
| 14 | Tagged bool | ||
| 15 | UUID string | ||
| 16 | } | ||
| 17 | |||
| 18 | // commentPrefixes maps file extension (lowercase, with dot) to the line-comment prefix. | ||
| 19 | var commentPrefixes = map[string]string{ | ||
| 20 | ".go": "//", | ||
| 21 | } | ||
| 22 | |||
| 23 | var ( | ||
| 24 | untaggedRE = regexp.MustCompile(`^@claude\s+\S`) | ||
| 25 | taggedRE = regexp.MustCompile(`^@claude\[(fw-[0-9a-f]{8})\]\s+\S`) | ||
| 26 | ) | ||
| 27 | |||
| 28 | func Scan(path string) ([]Marker, error) { | ||
| 29 | ext := strings.ToLower(filepath.Ext(path)) | ||
| 30 | prefix, ok := commentPrefixes[ext] | ||
| 31 | if !ok { | ||
| 32 | return nil, nil | ||
| 33 | } | ||
| 34 | f, err := os.Open(path) | ||
| 35 | if err != nil { | ||
| 36 | return nil, err | ||
| 37 | } | ||
| 38 | defer f.Close() | ||
| 39 | |||
| 40 | var markers []Marker | ||
| 41 | sc := bufio.NewScanner(f) | ||
| 42 | sc.Buffer(make([]byte, 1024*1024), 1024*1024) | ||
| 43 | line := 0 | ||
| 44 | for sc.Scan() { | ||
| 45 | line++ | ||
| 46 | raw := strings.TrimSpace(sc.Text()) | ||
| 47 | if !strings.HasPrefix(raw, prefix) { | ||
| 48 | continue | ||
| 49 | } | ||
| 50 | body := strings.TrimSpace(strings.TrimPrefix(raw, prefix)) | ||
| 51 | if m := taggedRE.FindStringSubmatch(body); m != nil { | ||
| 52 | markers = append(markers, Marker{Line: line, Text: body, Tagged: true, UUID: m[1]}) | ||
| 53 | continue | ||
| 54 | } | ||
| 55 | if untaggedRE.MatchString(body) { | ||
| 56 | markers = append(markers, Marker{Line: line, Text: body}) | ||
| 57 | } | ||
| 58 | } | ||
| 59 | return markers, sc.Err() | ||
| 60 | } | ||
internal/scanner/scanner_test.go
| Old | New | ||
|---|---|---|---|
| @@ -0,0 +1,39 @@ | |||
| 1 | package scanner | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "os" | ||
| 5 | "path/filepath" | ||
| 6 | "testing" | ||
| 7 | ) | ||
| 8 | |||
| 9 | func writeFile(t *testing.T, dir, name, content string) string { | ||
| 10 | t.Helper() | ||
| 11 | path := filepath.Join(dir, name) | ||
| 12 | if err := os.WriteFile(path, []byte(content), 0o644); err != nil { | ||
| 13 | t.Fatal(err) | ||
| 14 | } | ||
| 15 | return path | ||
| 16 | } | ||
| 17 | |||
| 18 | func TestScanGoFileSingleUntaggedMarker(t *testing.T) { | ||
| 19 | dir := t.TempDir() | ||
| 20 | path := writeFile(t, dir, "foo.go", "package foo\n\n// @claude write a test for foo\nfunc Foo() {}\n") | ||
| 21 | |||
| 22 | markers, err := Scan(path) | ||
| 23 | if err != nil { | ||
| 24 | t.Fatalf("Scan: %v", err) | ||
| 25 | } | ||
| 26 | if len(markers) != 1 { | ||
| 27 | t.Fatalf("got %d markers, want 1: %#v", len(markers), markers) | ||
| 28 | } | ||
| 29 | m := markers[0] | ||
| 30 | if m.Line != 3 { | ||
| 31 | t.Errorf("Line = %d, want 3", m.Line) | ||
| 32 | } | ||
| 33 | if m.Text != "@claude write a test for foo" { | ||
| 34 | t.Errorf("Text = %q, want %q", m.Text, "@claude write a test for foo") | ||
| 35 | } | ||
| 36 | if m.Tagged { | ||
| 37 | t.Errorf("Tagged = true, want false") | ||
| 38 | } | ||
| 39 | } | ||