Skip to content

Commit ca8cf6f

Browse files
authored
Merge pull request #294 from shashankram/fix-292
v2/parser: prefer original comment when evaluating aliased Type
2 parents ddb642e + b3fb8bb commit ca8cf6f

File tree

7 files changed

+249
-28
lines changed

7 files changed

+249
-28
lines changed

v2/parser/parse.go

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
387390
func (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\nIgnoring 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\nIgnoring 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\nIgnoring 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\nIgnoring 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

559619
func 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

563627
func goFuncNameToName(in string) types.Name {

v2/parser/parse_122.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ func (p *Parser) walkAliasType(u types.Universe, in gotypes.Type) *types.Type {
3131
}
3232
return nil
3333
}
34+
35+
func isTypeAlias(in gotypes.Type) bool {
36+
_, isAlias := in.(*gotypes.Alias)
37+
return isAlias
38+
}

v2/parser/parse_pre_122.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ import (
2828
func (p *Parser) walkAliasType(u types.Universe, in gotypes.Type) *types.Type {
2929
return nil
3030
}
31+
32+
func isTypeAlias(in gotypes.Type) bool {
33+
return false
34+
}

0 commit comments

Comments
 (0)