Skip to content

[duplicate-code] Duplicate Code: YAML scalar matcher logic in schema suggestions parser #52591

Description

@github-actions

🔍 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

  1. 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
  2. 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

  • Review duplication findings
  • Prioritize refactoring task
  • Extract shared scalar matcher construction
  • Update top-level and nested extraction to reuse it
  • Update tests
  • Verify no behavior regression for quoted and unquoted values

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 ·

  • expires on Aug 15, 2026, 2:10 PM UTC-08:00

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions