🔍 Duplicate Code Detected: YAML Scalar Matcher Logic in Schema Suggestions
Analysis of commit 903f16a9eba25a03cc4cdad5c45d43e72ddd4b37
Assignee: @copilot
Summary
pkg/parser/schema_suggestions.go now contains two separate implementations of the same YAML scalar extraction strategy: one for top-level keys and one for nested child keys. Both paths use the same three scalar forms (single-quoted, double-quoted, unquoted-with-comment-trimming) and the same FindStringSubmatch/TrimSpace flow.
This crosses the reporting threshold because the duplicated logic spans well over 10 lines and was expanded by the latest refactor rather than removed.
Duplication Details
Duplication Details
Pattern: repeated YAML scalar regex matching and extraction
- Severity: Medium
- Occurrences: 2 implementations of the same parsing logic
- Locations:
pkg/parser/schema_suggestions.go:502
pkg/parser/schema_suggestions.go:597
- Code Sample:
// top-level extraction
reSingle, err := regexp.Compile(`(?m)^` + escapedField + `[ \t]*:[ \t]*'([^'\n]+)'`)
reDouble, err := regexp.Compile(`(?m)^` + escapedField + `[ \t]*:[ \t]*"([^"\n]+)"`)
reUnquoted, err := regexp.Compile(`(?m)^` + escapedField + `[ \t]*:[ \t]*([^'"\n#][^\n#]*?)(?:[ \t]*#.*)?$`)
// nested extraction helper
valuePatterns := []string{
childPrefix + `'([^'\n]+)'`,
childPrefix + `"([^"\n]+)"`,
childPrefix + `([^'"\n#][^\n#]*?)(?:[ \t]*#.*)?$`,
}
The only real difference is the anchor/prefix ((?m)^field: vs ^\s+child:). The scalar matching behavior itself is duplicated.
Impact Analysis
Impact Analysis
- Maintainability: any future fix to YAML scalar handling has to be applied in two places.
- Bug Risk: quoting, inline-comment, or whitespace fixes can easily diverge between top-level and nested extraction.
- Code Bloat: the refactor reduced per-line recompilation in the nested path, but it introduced another regex-specification source instead of centralizing one matcher builder.
Refactoring Recommendations
Refactoring Recommendations
-
Extract a shared scalar matcher builder
- Extract common functionality to:
pkg/parser/schema_suggestions.go
- Estimated effort: 1-2 hours
- Benefits: one source of truth for supported scalar forms and trimming behavior
-
Parameterize the key prefix/anchor separately from scalar matching
- Keep top-level vs nested traversal distinct, but reuse one helper that accepts the compiled prefix or full regex prefix
- Estimated effort: 1-2 hours
- Benefits: preserves the different traversal semantics without duplicating the scalar parsing rules
Implementation Checklist
Analysis Metadata
- Analyzed Files: 1 changed production file
- Detection Method: Serena semantic code analysis plus targeted pattern search
- Commit:
903f16a9eba25a03cc4cdad5c45d43e72ddd4b37
- Analysis Date: 2026-08-13 UTC
Generated by 🔍 Duplicate Code Detector · gpt54 · 51.9 AIC · ⊞ 12.8K · ◷
🔍 Duplicate Code Detected: YAML Scalar Matcher Logic in Schema Suggestions
Analysis of commit
903f16a9eba25a03cc4cdad5c45d43e72ddd4b37Assignee:
@copilotSummary
pkg/parser/schema_suggestions.gonow contains two separate implementations of the same YAML scalar extraction strategy: one for top-level keys and one for nested child keys. Both paths use the same three scalar forms (single-quoted, double-quoted, unquoted-with-comment-trimming) and the sameFindStringSubmatch/TrimSpaceflow.This crosses the reporting threshold because the duplicated logic spans well over 10 lines and was expanded by the latest refactor rather than removed.
Duplication Details
Duplication Details
Pattern: repeated YAML scalar regex matching and extraction
pkg/parser/schema_suggestions.go:502pkg/parser/schema_suggestions.go:597The only real difference is the anchor/prefix (
(?m)^field:vs^\s+child:). The scalar matching behavior itself is duplicated.Impact Analysis
Impact Analysis
Refactoring Recommendations
Refactoring Recommendations
Extract a shared scalar matcher builder
pkg/parser/schema_suggestions.goParameterize the key prefix/anchor separately from scalar matching
Implementation Checklist
Analysis Metadata
903f16a9eba25a03cc4cdad5c45d43e72ddd4b37