@@ -23,7 +23,10 @@ import (
2323 "go/constant"
2424 "go/token"
2525 gotypes "go/types"
26+ "maps"
2627 "path/filepath"
28+ "reflect"
29+ "slices"
2730 "sort"
2831 "strings"
2932 "time"
@@ -385,8 +388,63 @@ func (p *Parser) NewUniverse() (types.Universe, error) {
385388// addCommentsToType takes any accumulated comment lines prior to obj and
386389// attaches them to the type t.
387390func (p * Parser ) addCommentsToType (obj gotypes.Object , t * types.Type ) {
388- t .CommentLines = p .docComment (obj .Pos ())
389- t .SecondClosestCommentLines = p .priorDetachedComment (obj .Pos ())
391+ if newLines , oldLines := p .docComment (obj .Pos ()), t .CommentLines ; len (newLines ) > 0 {
392+ switch {
393+ case len (oldLines ) == 0 , reflect .DeepEqual (oldLines , newLines ):
394+ // no comments associated, or comments match exactly
395+ t .CommentLines = newLines
396+
397+ case isTypeAlias (obj .Type ()):
398+ // ignore mismatched comments from obj because it's an alias
399+ klog .Warningf (
400+ "Mismatched comments seen for type %v. Using comments:\n %s\n Ignoring comments from type alias:\n %s\n " ,
401+ t .GoType ,
402+ formatCommentBlock (oldLines ),
403+ formatCommentBlock (newLines ),
404+ )
405+
406+ case ! isTypeAlias (obj .Type ()):
407+ // overwrite existing comments with ones from obj because obj is not an alias
408+ t .CommentLines = newLines
409+ klog .Warningf (
410+ "Mismatched comments seen for type %v. Using comments:\n %s\n Ignoring comments from type alias:\n %s\n " ,
411+ t .GoType ,
412+ formatCommentBlock (newLines ),
413+ formatCommentBlock (oldLines ),
414+ )
415+ }
416+ }
417+
418+ if newLines , oldLines := p .priorDetachedComment (obj .Pos ()), t .SecondClosestCommentLines ; len (newLines ) > 0 {
419+ switch {
420+ case len (oldLines ) == 0 , reflect .DeepEqual (oldLines , newLines ):
421+ // no comments associated, or comments match exactly
422+ t .SecondClosestCommentLines = newLines
423+
424+ case isTypeAlias (obj .Type ()):
425+ // ignore mismatched comments from obj because it's an alias
426+ klog .Warningf (
427+ "Mismatched secondClosestCommentLines seen for type %v. Using comments:\n %s\n Ignoring comments from type alias:\n %s\n " ,
428+ t .GoType ,
429+ formatCommentBlock (oldLines ),
430+ formatCommentBlock (newLines ),
431+ )
432+
433+ case ! isTypeAlias (obj .Type ()):
434+ // overwrite existing comments with ones from obj because obj is not an alias
435+ t .SecondClosestCommentLines = newLines
436+ klog .Warningf (
437+ "Mismatched secondClosestCommentLines seen for type %v. Using comments:\n %s\n Ignoring comments from type alias:\n %s\n " ,
438+ t .GoType ,
439+ formatCommentBlock (newLines ),
440+ formatCommentBlock (oldLines ),
441+ )
442+ }
443+ }
444+ }
445+
446+ func formatCommentBlock (lines []string ) string {
447+ return "```\n " + strings .Join (lines , "\n " ) + "\n ```"
390448}
391449
392450// packageDir tries to figure out the directory of the specified package.
@@ -510,7 +568,9 @@ func (p *Parser) addPkgToUniverse(pkg *packages.Package, u *types.Universe) erro
510568
511569 // Add all of this package's imports.
512570 importedPkgs := []string {}
513- for _ , imp := range pkg .Imports {
571+ // Iterate imports in a predictable order
572+ for _ , key := range slices .Sorted (maps .Keys (pkg .Imports )) {
573+ imp := pkg .Imports [key ]
514574 if err := p .addPkgToUniverse (imp , u ); err != nil {
515575 return err
516576 }
@@ -557,7 +617,11 @@ func (p *Parser) priorCommentLines(pos token.Pos, lines int) *ast.CommentGroup {
557617}
558618
559619func splitLines (str string ) []string {
560- return strings .Split (strings .TrimRight (str , "\n " ), "\n " )
620+ lines := strings .Split (strings .TrimRight (str , "\n " ), "\n " )
621+ if len (lines ) == 1 && lines [0 ] == "" {
622+ return nil
623+ }
624+ return lines
561625}
562626
563627func goFuncNameToName (in string ) types.Name {
0 commit comments