Skip to content

Commit 604196b

Browse files
committed
Ignore deprecation in type-alias comment handling
If the only difference is something like "Deprecated" then we don't need to issue a warning.
1 parent f1dc67e commit 604196b

File tree

2 files changed

+72
-25
lines changed

2 files changed

+72
-25
lines changed

v2/parser/parse.go

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,32 @@ 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 inDeprecated {
396+
continue
397+
}
398+
if len(strings.TrimSpace(line)) == 0 {
399+
prevWasBlank = true
400+
inDeprecated = false
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) {
@@ -396,22 +422,26 @@ func (p *Parser) addCommentsToType(obj gotypes.Object, t *types.Type) {
396422

397423
case isTypeAlias(obj.Type()):
398424
// 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-
)
425+
if !reflect.DeepEqual(minimize(oldLines), minimize(newLines)) {
426+
klog.Warningf(
427+
"Mismatched comments 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+
}
405433

406434
case !isTypeAlias(obj.Type()):
407435
// overwrite existing comments with ones from obj because obj is not an alias
408436
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-
)
437+
if !reflect.DeepEqual(minimize(oldLines), minimize(newLines)) {
438+
klog.Warningf(
439+
"Mismatched comments seen for type %v. Using comments:\n%s\nIgnoring comments from type alias:\n%s\n",
440+
t.GoType,
441+
formatCommentBlock(newLines),
442+
formatCommentBlock(oldLines),
443+
)
444+
}
415445
}
416446
}
417447

@@ -423,28 +453,32 @@ func (p *Parser) addCommentsToType(obj gotypes.Object, t *types.Type) {
423453

424454
case isTypeAlias(obj.Type()):
425455
// 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-
)
456+
if !reflect.DeepEqual(minimize(oldLines), minimize(newLines)) {
457+
klog.Warningf(
458+
"Mismatched secondClosestCommentLines seen for type %v. Using comments:\n%s\nIgnoring comments from type alias:\n%s\n",
459+
t.GoType,
460+
formatCommentBlock(oldLines),
461+
formatCommentBlock(newLines),
462+
)
463+
}
432464

433465
case !isTypeAlias(obj.Type()):
434466
// overwrite existing comments with ones from obj because obj is not an alias
435467
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-
)
468+
if !reflect.DeepEqual(minimize(oldLines), minimize(newLines)) {
469+
klog.Warningf(
470+
"Mismatched secondClosestCommentLines seen for type %v. Using comments:\n%s\nIgnoring comments from type alias:\n%s\n",
471+
t.GoType,
472+
formatCommentBlock(newLines),
473+
formatCommentBlock(oldLines),
474+
)
475+
}
442476
}
443477
}
444478
}
445479

446480
func formatCommentBlock(lines []string) string {
447-
return "```\n" + strings.Join(lines, "\n") + "\n```"
481+
return " ```\n " + strings.Join(lines, "\n ") + "\n ```"
448482
}
449483

450484
// packageDir tries to figure out the directory of the specified package.

v2/parser/testdata/type-alias/v1/file.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,16 @@ type Blah struct {
1313

1414
// This is an alias within the same package.
1515
type LocalBlahAlias = Blah
16+
17+
// This is for back compat.
18+
// It has the same number of lines as Blah's comment.
19+
//
20+
// Deprecated: use Blah instead.
21+
type LocalBlahAliasDeprecated = Blah
22+
23+
// This is for back compat.
24+
// It has the same number of lines as Blah's comment.
25+
//
26+
// Deprecated: use Blah instead.
27+
// It's a whole paragraph of deprecated notes
28+
type LocalBlahAliasDeprecatedLong = Blah

0 commit comments

Comments
 (0)