@@ -385,66 +385,117 @@ func (p *Parser) NewUniverse() (types.Universe, error) {
385385 return u , nil
386386}
387387
388+ // minimize returns a copy of lines with "irrelevant" lines removed. This
389+ // includes blank lines and paragraphs starting with "Deprecated:".
390+ func minimize (lines []string ) []string {
391+ out := make ([]string , 0 , len (lines ))
392+ inDeprecated := false // paragraph tracking
393+ prevWasBlank := false
394+ for _ , line := range lines {
395+ if len (strings .TrimSpace (line )) == 0 {
396+ prevWasBlank = true
397+ inDeprecated = false
398+ continue
399+ }
400+ if inDeprecated {
401+ continue
402+ }
403+ if prevWasBlank && strings .HasPrefix (strings .TrimSpace (line ), "Deprecated:" ) {
404+ prevWasBlank = false
405+ inDeprecated = true
406+ continue
407+ }
408+ prevWasBlank = false
409+ out = append (out , line )
410+ }
411+ return out
412+ }
413+
388414// addCommentsToType takes any accumulated comment lines prior to obj and
389415// attaches them to the type t.
390416func (p * Parser ) addCommentsToType (obj gotypes.Object , t * types.Type ) {
391417 if newLines , oldLines := p .docComment (obj .Pos ()), t .CommentLines ; len (newLines ) > 0 {
392418 switch {
393- case len (oldLines ) == 0 , reflect .DeepEqual (oldLines , newLines ):
419+ case reflect .DeepEqual (oldLines , newLines ):
420+ // nothing needed
421+
422+ case len (oldLines ) == 0 :
394423 // no comments associated, or comments match exactly
395424 t .CommentLines = newLines
396425
397426 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- )
427+ // Ignore mismatched comments from obj because it's an alias.
428+ // This can only be hit if gotypesalias is enabled.
429+ if ! reflect .DeepEqual (minimize (oldLines ), minimize (newLines )) {
430+ klog .Warningf (
431+ "Mismatched comments on type %v.\n Using comments:\n %s\n Ignoring comments from type alias:\n %s\n " ,
432+ t .GoType ,
433+ formatCommentBlock (oldLines ),
434+ formatCommentBlock (newLines ),
435+ )
436+ }
405437
406438 case ! isTypeAlias (obj .Type ()):
407- // overwrite existing comments with ones from obj because obj is not an alias
439+ // Overwrite existing comments with ones from obj because obj is not an alias.
440+ // If gotypesalias is enabled, this should mean we found the "real"
441+ // type, not an alias. If gotypesalias is disabled, we can end up
442+ // overwriting the "real" comments with an alias's comments, but
443+ // it is not clear if we can assume which one is the "real" one.
408444 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- )
445+ if ! reflect .DeepEqual (minimize (oldLines ), minimize (newLines )) {
446+ klog .Warningf (
447+ "Mismatched comments on type %v.\n Using comments:\n %s\n Ignoring comments from possible type alias:\n %s\n " ,
448+ t .GoType ,
449+ formatCommentBlock (newLines ),
450+ formatCommentBlock (oldLines ),
451+ )
452+ }
415453 }
416454 }
417455
418456 if newLines , oldLines := p .priorDetachedComment (obj .Pos ()), t .SecondClosestCommentLines ; len (newLines ) > 0 {
419457 switch {
420- case len (oldLines ) == 0 , reflect .DeepEqual (oldLines , newLines ):
458+ case reflect .DeepEqual (oldLines , newLines ):
459+ // nothing needed
460+
461+ case len (oldLines ) == 0 :
421462 // no comments associated, or comments match exactly
422463 t .SecondClosestCommentLines = newLines
423464
424465 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- )
466+ // Ignore mismatched comments from obj because it's an alias.
467+ // This can only be hit if gotypesalias is enabled.
468+ if ! reflect .DeepEqual (minimize (oldLines ), minimize (newLines )) {
469+ // ignore mismatched comments from obj because it's an alias
470+ klog .Warningf (
471+ "Mismatched secondClosestCommentLines on type %v.\n Using comments:\n %s\n Ignoring comments from type alias:\n %s\n " ,
472+ t .GoType ,
473+ formatCommentBlock (oldLines ),
474+ formatCommentBlock (newLines ),
475+ )
476+ }
432477
433478 case ! isTypeAlias (obj .Type ()):
434- // overwrite existing comments with ones from obj because obj is not an alias
479+ // Overwrite existing comments with ones from obj because obj is not an alias.
480+ // If gotypesalias is enabled, this should mean we found the "real"
481+ // type, not an alias. If gotypesalias is disabled, we can end up
482+ // overwriting the "real" comments with an alias's comments, but
483+ // it is not clear if we can assume which one is the "real" one.
435484 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- )
485+ if ! reflect .DeepEqual (minimize (oldLines ), minimize (newLines )) {
486+ klog .Warningf (
487+ "Mismatched secondClosestCommentLines on type %v.\n Using comments:\n %s\n Ignoring comments from possible type alias:\n %s\n " ,
488+ t .GoType ,
489+ formatCommentBlock (newLines ),
490+ formatCommentBlock (oldLines ),
491+ )
492+ }
442493 }
443494 }
444495}
445496
446497func formatCommentBlock (lines []string ) string {
447- return "```\n " + strings .Join (lines , "\n " ) + "\n ```"
498+ return " ```\n " + strings .Join (lines , "\n " ) + "\n ```"
448499}
449500
450501// packageDir tries to figure out the directory of the specified package.
0 commit comments