From 7ff9e84894d4a3a6db8141723972e5bbf006f3e2 Mon Sep 17 00:00:00 2001 From: Ed Zynda Date: Sun, 22 Mar 2026 19:11:52 +0300 Subject: [PATCH] fix: resolve golangci-lint issues in prompts package - Fix gofmt formatting in loader.go and loader_test.go - Modernize strings.Index to strings.Cut (template.go:162,231) - Use min() builtin instead of manual if check (template.go:273) --- internal/prompts/loader.go | 10 ++++----- internal/prompts/loader_test.go | 40 ++++++++++++++++----------------- internal/prompts/template.go | 20 +++++------------ 3 files changed, 30 insertions(+), 40 deletions(-) diff --git a/internal/prompts/loader.go b/internal/prompts/loader.go index 345a66a5..42f62c84 100644 --- a/internal/prompts/loader.go +++ b/internal/prompts/loader.go @@ -41,11 +41,11 @@ type Diagnostic struct { // to highest), with later templates overriding earlier ones of the same name. // // Discovery paths searched in order: -// 1. Default templates (if IncludeDefaults) -// 2. ~/.kit/prompts/ (global user templates) -// 3. .kit/prompts/ (project-local templates) -// 4. ConfigPaths (from configuration) -// 5. ExtraPaths (explicit paths, highest precedence) +// 1. Default templates (if IncludeDefaults) +// 2. ~/.kit/prompts/ (global user templates) +// 3. .kit/prompts/ (project-local templates) +// 4. ConfigPaths (from configuration) +// 5. ExtraPaths (explicit paths, highest precedence) func LoadAll(opts LoadOptions) ([]*PromptTemplate, []Diagnostic, error) { if opts.Cwd == "" { opts.Cwd, _ = os.Getwd() diff --git a/internal/prompts/loader_test.go b/internal/prompts/loader_test.go index 378a83bf..47ac9054 100644 --- a/internal/prompts/loader_test.go +++ b/internal/prompts/loader_test.go @@ -9,51 +9,51 @@ import ( func TestLoadAll_Integration(t *testing.T) { // Create a temp directory for testing tempDir := t.TempDir() - + // Create the .kit/prompts subdirectory structure promptsDir := filepath.Join(tempDir, ".kit", "prompts") if err := os.MkdirAll(promptsDir, 0755); err != nil { t.Fatalf("Failed to create prompts dir: %v", err) } - + // Create a test template file templateContent := `--- description: Test template for integration --- Review $1 with focus on $2` - + testFile := filepath.Join(promptsDir, "test.md") if err := os.WriteFile(testFile, []byte(templateContent), 0644); err != nil { t.Fatalf("Failed to create test file: %v", err) } - + // Test loading from the temp directory tpls, diags, err := LoadAll(LoadOptions{ HomeDir: tempDir, IncludeDefaults: false, // Skip default locations for this test }) - + if err != nil { t.Fatalf("LoadAll failed: %v", err) } - + if len(diags) > 0 { t.Logf("Got %d diagnostics", len(diags)) } - + if len(tpls) != 1 { t.Fatalf("Expected 1 template, got %d", len(tpls)) } - + tpl := tpls[0] if tpl.Name != "test" { t.Errorf("Expected name 'test', got '%s'", tpl.Name) } - + if tpl.Description != "Test template for integration" { t.Errorf("Expected description 'Test template for integration', got '%s'", tpl.Description) } - + // Test expansion expanded := tpl.Expand("code security") expected := "Review code with focus on security" @@ -69,25 +69,25 @@ func TestParseTemplate_WithFrontmatter(t *testing.T) { description: A test template --- Create a $1 component with $2 features` - + testFile := filepath.Join(tempDir, "component.md") if err := os.WriteFile(testFile, []byte(templateContent), 0644); err != nil { t.Fatalf("Failed to create test file: %v", err) } - + tpl, err := ParseTemplate(testFile) if err != nil { t.Fatalf("ParseTemplate failed: %v", err) } - + if tpl.Name != "component" { t.Errorf("Expected name 'component', got '%s'", tpl.Name) } - + if tpl.Description != "A test template" { t.Errorf("Expected description 'A test template', got '%s'", tpl.Description) } - + expectedContent := "Create a $1 component with $2 features" if tpl.Content != expectedContent { t.Errorf("Expected content '%s', got '%s'", expectedContent, tpl.Content) @@ -99,26 +99,26 @@ func TestParseTemplate_WithoutFrontmatter(t *testing.T) { tempDir := t.TempDir() templateContent := `Simple template without frontmatter Supports $1 and $2 placeholders` - + testFile := filepath.Join(tempDir, "simple.md") if err := os.WriteFile(testFile, []byte(templateContent), 0644); err != nil { t.Fatalf("Failed to create test file: %v", err) } - + tpl, err := ParseTemplate(testFile) if err != nil { t.Fatalf("ParseTemplate failed: %v", err) } - + if tpl.Name != "simple" { t.Errorf("Expected name 'simple', got '%s'", tpl.Name) } - + // Description should be empty since there's no frontmatter if tpl.Description != "" { t.Errorf("Expected empty description, got '%s'", tpl.Description) } - + // Content should include everything if tpl.Content != templateContent { t.Errorf("Content mismatch\nExpected:\n%s\nGot:\n%s", templateContent, tpl.Content) diff --git a/internal/prompts/template.go b/internal/prompts/template.go index 308d2e7f..188f3e2d 100644 --- a/internal/prompts/template.go +++ b/internal/prompts/template.go @@ -159,8 +159,8 @@ func SubstituteArgs(content string, args []string) string { } // Check if there's a second colon for length ${N:M:L} - if secondColonIdx := strings.Index(rest, ":"); secondColonIdx >= 0 { - lengthStr := rest[secondColonIdx+1:] + lengthStr, _, ok := strings.Cut(rest, ":") + if ok { length, err := strconv.Atoi(lengthStr) if err != nil || length < 0 { return match @@ -228,15 +228,7 @@ func expandAtArgs(inner string, args []string) string { rest = rest[1:] // Parse start index - colonIdx := strings.Index(rest, ":") - var startStr, lengthStr string - - if colonIdx >= 0 { - startStr = rest[:colonIdx] - lengthStr = rest[colonIdx+1:] - } else { - startStr = rest - } + startStr, lengthStr, hasLength := strings.Cut(rest, ":") start, err := strconv.Atoi(startStr) if err != nil || start < 0 { @@ -249,7 +241,7 @@ func expandAtArgs(inner string, args []string) string { start-- } - if lengthStr != "" { + if hasLength { length, err := strconv.Atoi(lengthStr) if err != nil || length < 0 { return "${" + inner + "}" @@ -270,9 +262,7 @@ func joinArgsRange(args []string, start, length int) string { return "" } end := start + length - if end > len(args) { - end = len(args) - } + end = min(end, len(args)) return strings.Join(args[start:end], " ") }