MCPcopy Create free account
hub / github.com/atripati/ark / AutoFixGoCode

Function AutoFixGoCode

pkg/runtime/quality.go:494–534  ·  view source on GitHub ↗
(code string)

Source from the content-addressed store, hash-verified

492}
493
494func AutoFixGoCode(code string) string {
495 lines := strings.Split(code, "\n")
496 var fixed []string
497
498 for i := 0; i < len(lines); i++ {
499 trimmed := strings.TrimSpace(lines[i])
500
501 if i > 0 && trimmed == "}" {
502 prevTrimmed := strings.TrimSpace(lines[i-1])
503 if strings.Contains(prevTrimmed, ", err :=") || strings.Contains(prevTrimmed, ", err =") {
504 // Detect indentation from the assignment line
505 indent := getIndent(lines[i-1])
506 fixed = append(fixed, indent+"if err != nil {")
507 fixed = append(fixed, indent+indentUnit(indent)+"return nil, err")
508 fixed = append(fixed, indent+"}")
509 continue
510 }
511 }
512
513 if strings.Contains(trimmed, "return") && strings.Contains(trimmed, "err") &&
514 !strings.HasPrefix(trimmed, "if ") && !strings.HasPrefix(trimmed, "//") {
515 if i > 0 {
516 prevTrimmed := strings.TrimSpace(lines[i-1])
517 if prevTrimmed == "}" && i >= 2 {
518 prevPrev := strings.TrimSpace(lines[i-2])
519 if strings.Contains(prevPrev, "io.EOF") || strings.Contains(prevPrev, "break") {
520 indent := getIndent(lines[i])
521 fixed = append(fixed, indent+"if err != nil {")
522 fixed = append(fixed, indent+indentUnit(indent)+trimmed)
523 fixed = append(fixed, indent+"}")
524 continue
525 }
526 }
527 }
528 }
529
530 fixed = append(fixed, lines[i])
531 }
532
533 return strings.Join(fixed, "\n")
534}
535func getIndent(line string) string {
536 for i, c := range line {
537 if c != ' ' && c != '\t' {

Callers 2

autoFixCodeInResponseFunction · 0.85
verifyCodeResponseFunction · 0.85

Calls 2

getIndentFunction · 0.85
indentUnitFunction · 0.85

Tested by

no test coverage detected