2020 clientErrLenPattern = regexp .MustCompile (`\blen\(errs\)` )
2121 clientErrComparePattern = regexp .MustCompile (`err\s*!=\s*nil\s*>\s*0` )
2222 clientErrMapPattern = regexp .MustCompile (`"errs"\s*:\s*errs` )
23- clientErrVarPattern = regexp .MustCompile (`\berrs\b` )
24- clientErrsDeclPattern = regexp .MustCompile (`\berrs\s+\[]error\b` )
23+ clientErrsDeclPattern = regexp .MustCompile (`(?m)^\s*var\s+errs\b` )
2524
2625 // Non-alias-dependent patterns
2726 requestFromAgent = regexp .MustCompile (`^([ \t]*)(\w+)\s*:=\s*(\w+)\.Request\(\)\s*$` )
@@ -236,13 +235,17 @@ func rewriteAcquireAgentBlocksWithAlias(content, alias string) (string, bool) {
236235 structStart := - 1
237236 preservedLines := []string {}
238237 preservedIdx := []int {}
238+ blankIdx := []int {}
239+ blankAfterParse := false
239240 for k := parseEnd + 1 ; k < len (lines ); k ++ {
240241 if skipLines [k ] {
241242 continue
242243 }
243244
244245 trimmed := strings .TrimSpace (lines [k ])
245246 if trimmed == "" {
247+ blankAfterParse = true
248+ blankIdx = append (blankIdx , k )
246249 continue
247250 }
248251 if strings .HasPrefix (trimmed , "//" ) {
@@ -270,18 +273,41 @@ func rewriteAcquireAgentBlocksWithAlias(content, alias string) (string, bool) {
270273 }
271274
272275 if structStart == - 1 {
273- out = append (out , line )
274- continue
276+ structStart = parseEnd
275277 }
276278
277279 structMatch := structAssignPattern .FindStringSubmatch (lines [structStart ])
278280 bytesMatch := bytesAssignPattern .FindStringSubmatch (lines [structStart ])
279281 stringMatch := stringAssignPattern .FindStringSubmatch (lines [structStart ])
282+ parseOnly := len (structMatch ) == 0 && len (bytesMatch ) == 0 && len (stringMatch ) == 0
283+
284+ addPreserved := func () {
285+ if len (preservedLines ) == 0 {
286+ return
287+ }
288+ if blankAfterParse && parseOnly {
289+ out = append (out , "" )
290+ for _ , idx := range blankIdx {
291+ skipLines [idx ] = true
292+ }
293+ blankAfterParse = false
294+ }
295+
296+ out = append (out , preservedLines ... )
297+ for _ , idx := range preservedIdx {
298+ skipLines [idx ] = true
299+ }
300+ }
301+
302+ if len (preservedLines ) == 0 && ! parseOnly {
303+ for _ , idx := range blankIdx {
304+ skipLines [idx ] = true
305+ }
306+ blankAfterParse = false
307+ }
280308
281309 if len (structMatch ) == 0 && len (bytesMatch ) == 0 && len (stringMatch ) == 0 {
282310 structStart = parseEnd
283- preservedLines = nil
284- preservedIdx = nil
285311 }
286312
287313 errName := chooseErrName (out , lines , i )
@@ -322,12 +348,7 @@ func rewriteAcquireAgentBlocksWithAlias(content, alias string) (string, bool) {
322348
323349 switch {
324350 case len (structMatch ) > 0 && structMatch [5 ] == agentVar :
325- if len (preservedLines ) > 0 {
326- out = append (out , preservedLines ... )
327- for _ , idx := range preservedIdx {
328- skipLines [idx ] = true
329- }
330- }
351+ addPreserved ()
331352 errAssign := errAssignmentOperator (errName , out , lines , i , "resp" )
332353 statusVar := strings .TrimSpace (structMatch [2 ])
333354 bodyVar := strings .TrimSpace (structMatch [3 ])
@@ -395,12 +416,7 @@ func rewriteAcquireAgentBlocksWithAlias(content, alias string) (string, bool) {
395416 changed = true
396417 continue
397418 case len (bytesMatch ) > 0 && bytesMatch [5 ] == agentVar :
398- if len (preservedLines ) > 0 {
399- out = append (out , preservedLines ... )
400- for _ , idx := range preservedIdx {
401- skipLines [idx ] = true
402- }
403- }
419+ addPreserved ()
404420 errAssign := errAssignmentOperator (errName , out , lines , i , "resp" )
405421 statusVar := strings .TrimSpace (bytesMatch [2 ])
406422 bodyVar := strings .TrimSpace (bytesMatch [3 ])
@@ -442,12 +458,7 @@ func rewriteAcquireAgentBlocksWithAlias(content, alias string) (string, bool) {
442458 changed = true
443459 continue
444460 case len (stringMatch ) > 0 && stringMatch [5 ] == agentVar :
445- if len (preservedLines ) > 0 {
446- out = append (out , preservedLines ... )
447- for _ , idx := range preservedIdx {
448- skipLines [idx ] = true
449- }
450- }
461+ addPreserved ()
451462 errAssign := errAssignmentOperator (errName , out , lines , i , "resp" )
452463 statusVar := strings .TrimSpace (stringMatch [2 ])
453464 bodyVar := strings .TrimSpace (stringMatch [3 ])
@@ -489,12 +500,18 @@ func rewriteAcquireAgentBlocksWithAlias(content, alias string) (string, bool) {
489500 changed = true
490501 continue
491502 default :
503+ if ! parseOnly {
504+ addPreserved ()
505+ }
492506 errAssign := errAssignmentOperator (errName , out , lines , i )
493507 respLine := fmt .Sprintf ("%s_, %s %s %s.Send()" , indent , errName , errAssign , reqVar )
494508 out = append (out , respLine )
495509 out = append (out , fmt .Sprintf ("%sif %s != nil {" , parseIndent , errName ))
496510 out = append (out , replaceErrIdentifier (parseBody [:len (parseBody )- 1 ], errName , false )... )
497511 out = append (out , parseIndent + "}" )
512+ if parseOnly {
513+ addPreserved ()
514+ }
498515
499516 i = parseEnd
500517 changed = true
@@ -712,17 +729,18 @@ func identifierDeclaredInLines(lines []string, name string) bool {
712729}
713730
714731func declaredInVarBlockLine (name , trimmed string ) bool {
715- if ! strings .HasPrefix (trimmed , name ) {
732+ fields := strings .Fields (trimmed )
733+ if len (fields ) < 2 {
716734 return false
717735 }
718-
719- remainder := strings .TrimSpace (strings .TrimPrefix (trimmed , name ))
720- if remainder == "" {
736+ if fields [0 ] != name {
721737 return false
722738 }
723739
724- if strings .Contains (remainder , ":=" ) || strings .Contains (remainder , "=" ) {
725- return false
740+ for _ , f := range fields [1 :] {
741+ if strings .Contains (f , ":=" ) || strings .Contains (f , "=" ) {
742+ return false
743+ }
726744 }
727745
728746 return true
@@ -1185,7 +1203,14 @@ func ensureClientImport(content string) string {
11851203func rewriteClientErrorHandling (content string ) string {
11861204 // Only rewrite when we see the legacy multi-error usage patterns. This avoids
11871205 // mutating unrelated identifiers such as custom "errs" slices.
1188- hasLegacyErrs := clientErrIfPattern .MatchString (content ) || clientErrLenPattern .MatchString (content ) || clientErrComparePattern .MatchString (content ) || clientErrMapPattern .MatchString (content )
1206+ baseLegacy := clientErrIfPattern .MatchString (content ) || clientErrLenPattern .MatchString (content ) || clientErrComparePattern .MatchString (content ) || clientErrMapPattern .MatchString (content )
1207+ declaredErrs := clientErrsDeclPattern .MatchString (content )
1208+ if ! baseLegacy {
1209+ // Declaration-only cases keep errs slices intact.
1210+ return content
1211+ }
1212+
1213+ hasLegacyErrs := baseLegacy || declaredErrs
11891214 if ! hasLegacyErrs {
11901215 return content
11911216 }
@@ -1194,8 +1219,20 @@ func rewriteClientErrorHandling(content string) string {
11941219 updated = clientErrLenPattern .ReplaceAllString (updated , "err != nil" )
11951220 updated = clientErrComparePattern .ReplaceAllString (updated , "err != nil" )
11961221 updated = clientErrMapPattern .ReplaceAllString (updated , `"err": err` )
1197- updated = clientErrsDeclPattern .ReplaceAllString (updated , "err error" )
1198- updated = clientErrVarPattern .ReplaceAllString (updated , "err" )
1222+
1223+ errToken := regexp .MustCompile (`\berrs\b` )
1224+ lines := strings .Split (updated , "\n " )
1225+ for i , line := range lines {
1226+ trimmed := strings .TrimSpace (line )
1227+ if strings .HasPrefix (trimmed , "var errs" ) || strings .HasPrefix (trimmed , "errs" ) {
1228+ continue
1229+ }
1230+
1231+ lines [i ] = errToken .ReplaceAllString (line , "err" )
1232+ }
1233+
1234+ updated = strings .Join (lines , "\n " )
1235+
11991236 return updated
12001237}
12011238
0 commit comments