-
Notifications
You must be signed in to change notification settings - Fork 302
Closed
Description
🔍 Duplicate Code Detected: Codemod frontmatter line-walk/replace pattern
Analysis of commit e750213
Assignee: @copilot
Summary
Multiple codemod implementations repeat the same frontmatter line-walk, block-tracking, replace, and reconstruction pattern. This is duplicated across many pkg/cli/codemod_*.go files and exceeds the duplication threshold.
Duplication Details
Pattern: Frontmatter line walk with block tracking and replacement
- Severity: Medium
- Occurrences: 3+ (seen across many codemods)
- Locations:
pkg/cli/codemod_slash_command.go(lines 9-89)pkg/cli/codemod_assign_to_agent.go(lines 12-125)pkg/cli/codemod_permissions.go(lines 12-85)
- Code Sample:
frontmatterLines, markdown, err := parseFrontmatterLines(content)
if err != nil {
return content, false, err
}
var modified bool
result := make([]string, len(frontmatterLines))
for i, line := range frontmatterLines {
trimmedLine := strings.TrimSpace(line)
// block tracking and replacement logic...
result[i] = line
}
if !modified {
return content, false, nil
}
newContent := reconstructContent(result, markdown)
return newContent, true, nilImpact Analysis
- Maintainability: Changes to block-walk behavior (indent handling, comment skipping, etc.) must be made in many files.
- Bug Risk: Inconsistent fixes across codemods can create subtle YAML parsing differences.
- Code Bloat: Repeated 50–100 line loops across codemods increases file size and review overhead.
Refactoring Recommendations
-
Extract a shared helper in
pkg/cli/codemod_yaml_utils.go- Provide a utility that takes frontmatter lines, a block key, and a line transform callback.
- Estimated effort: medium (2–4 hours)
- Benefits: single source of truth for block-walk behavior and indentation handling.
-
Standardize codemod apply flow
- Build a small scaffold helper like
applyFrontmatterLineTransform(content, fn)to handle parse/reconstruct boilerplate. - Estimated effort: low to medium (1–2 hours)
- Benefits: removes repeated parse/reconstruct code and ensures consistent error handling.
- Build a small scaffold helper like
Implementation Checklist
- Review duplication findings
- Prioritize refactoring tasks
- Create refactoring plan
- Implement changes
- Update tests
- Verify no functionality broken
Analysis Metadata
- Analyzed Files: 3 core codemod files (pattern appears in many more)
- Detection Method: Serena semantic code analysis
- Commit: e750213
- Analysis Date: February 24, 2026
Generated by Duplicate Code Detector
Reactions are currently unavailable
Metadata
Metadata
Labels
No labels
Type
Fields
Give feedbackNo fields configured for issues without a type.