Skip to content

Commit 081d644

Browse files
authored
Merge pull request #314 from thockin/master
Fine-tune the type-alias comment handling
2 parents f1dc67e + 9d74656 commit 081d644

File tree

6 files changed

+110
-38
lines changed

6 files changed

+110
-38
lines changed

v2/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ test:
1111
verify:
1212
GODEBUG=gotypesalias=0 ./hack/verify-examples.sh
1313
GODEBUG=gotypesalias=1 ./hack/verify-examples.sh
14-
./hack/verify-go-directive.sh 1.20
14+
./hack/verify-go-directive.sh 1.23

v2/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
module k8s.io/gengo/v2
22

3-
go 1.20
3+
go 1.23
44

55
require (
6+
github.com/google/go-cmp v0.6.0
67
github.com/spf13/pflag v1.0.5
78
golang.org/x/tools v0.16.1
89
k8s.io/klog/v2 v2.2.0
910
)
1011

1112
require (
1213
github.com/go-logr/logr v0.2.0 // indirect
13-
github.com/google/go-cmp v0.6.0 // indirect
1414
golang.org/x/mod v0.14.0 // indirect
1515
)

v2/go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
77
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
88
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
99
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
10+
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
1011
golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
1112
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
1213
k8s.io/klog/v2 v2.2.0 h1:XRvcwJozkgZ1UQJmfMGpvRthQHOvihEhYtDfAaxMz/A=

v2/parser/parse.go

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

446497
func 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.

v2/parser/parse_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ func TestStructParse(t *testing.T) {
11201120
"k8s.io/gengo/v2/parser/testdata/type-alias/v2",
11211121
},
11221122
expected: func() *types.Type {
1123-
expectedTypeComments := []string{"Blah is a test.", "A test, I tell you."}
1123+
expectedTypeComments := []string{"Blah is a test.", "A test, I tell you.", "", "Another paragraph of comments."}
11241124
if !typeAliasEnabled {
11251125
// Comments from the last processed package wins.
11261126
expectedTypeComments = []string{"This is an alias for v1.Blah."}
@@ -1230,7 +1230,7 @@ func TestCommentsWithAliasedType(t *testing.T) {
12301230
continue
12311231
}
12321232

1233-
expectedTypeComments := []string{"Blah is a test.", "A test, I tell you."}
1233+
expectedTypeComments := []string{"Blah is a test.", "A test, I tell you.", "", "Another paragraph of comments."}
12341234
if !typeAliasEnabled {
12351235
// Comments from the last processed package wins.
12361236
expectedTypeComments = []string{"This is an alias for v1.Blah."}
Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package v1
22

3+
// This is an alias within the same package.
4+
// It comes before the type it aliases.
5+
type LocalBlahAlias = Blah
6+
37
// Blah is a test.
48
// A test, I tell you.
9+
//
10+
// Another paragraph of comments.
511
type Blah struct {
612
// A is the first field.
713
A int64 `json:"a"`
@@ -11,5 +17,19 @@ type Blah struct {
1117
B string `json:"b"`
1218
}
1319

14-
// This is an alias within the same package.
15-
type LocalBlahAlias = Blah
20+
// Blah is a test.
21+
// A test, I tell you.
22+
//
23+
// Deprecated: use Blah instead. This is another alias within the same package.
24+
//
25+
// Another paragraph of comments.
26+
type LocalBlahAliasDeprecated = Blah
27+
28+
// Blah is a test.
29+
// A test, I tell you.
30+
//
31+
// Deprecated: use Blah instead. This is a third alias within the same package.
32+
// It's a whole paragraph of deprecated notes
33+
//
34+
// Another paragraph of comments.
35+
type LocalBlahAliasDeprecatedLong = Blah

0 commit comments

Comments
 (0)