Skip to content

Commit abd233a

Browse files
tsuzuJoelSpeed
authored andcommitted
Fix message for imported embedded structs
1 parent 36e90ff commit abd233a

File tree

6 files changed

+38
-3
lines changed

6 files changed

+38
-3
lines changed

pkg/analysis/commentstart/analyzer.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"go/ast"
77
"go/token"
8+
"go/types"
89
"strings"
910

1011
"github.com/JoelSpeed/kal/pkg/analysis/helpers/extractjsontags"
@@ -49,8 +50,8 @@ func checkField(pass *analysis.Pass, field *ast.Field, tagInfo extractjsontags.F
4950
var fieldName string
5051
if len(field.Names) > 0 {
5152
fieldName = field.Names[0].Name
52-
} else if ident, ok := field.Type.(*ast.Ident); ok {
53-
fieldName = ident.Name
53+
} else {
54+
fieldName = types.ExprString(field.Type)
5455
}
5556

5657
if field.Doc == nil {

pkg/analysis/commentstart/analyzer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ import (
99

1010
func Test(t *testing.T) {
1111
testdata := analysistest.TestData()
12-
analysistest.RunWithSuggestedFixes(t, testdata, commentstart.Analyzer, "a")
12+
analysistest.RunWithSuggestedFixes(t, testdata, commentstart.Analyzer, "a/...")
1313
}

pkg/analysis/commentstart/testdata/src/a/a.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package a
22

3+
import "a/pkg"
4+
35
type CommentStartTestStruct struct {
46
NoJSONTag string
57
EmptyJSONTag string `json:""`
@@ -24,6 +26,12 @@ type CommentStartTestStruct struct {
2426

2527
A `json:"a"` // want "field A is missing godoc comment"
2628

29+
PkgA pkg.A `json:"pkgA"` // want "field PkgA is missing godoc comment"
30+
31+
pkg.Embedded `json:"embedded"` // want "field pkg.Embedded is missing godoc comment"
32+
33+
*pkg.EmbeddedPointer `json:"embeddedPointer"` // want "field \\*pkg.EmbeddedPointer is missing godoc comment"
34+
2735
// IncorrectStartComment is a field with an incorrect start to the comment. // want "godoc for field IncorrectStartComment should start with 'incorrectStartComment ...'"
2836
IncorrectStartComment string `json:"incorrectStartComment"`
2937

pkg/analysis/commentstart/testdata/src/a/a.go.golden

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package a
22

3+
import "a/pkg"
4+
35
type CommentStartTestStruct struct {
46
NoJSONTag string
57
EmptyJSONTag string `json:""`
@@ -24,6 +26,12 @@ type CommentStartTestStruct struct {
2426

2527
A `json:"a"` // want "field A is missing godoc comment"
2628

29+
PkgA pkg.A `json:"pkgA"` // want "field PkgA is missing godoc comment"
30+
31+
pkg.Embedded `json:"embedded"` // want "field pkg.Embedded is missing godoc comment"
32+
33+
*pkg.EmbeddedPointer `json:"embeddedPointer"` // want "field \\*pkg.EmbeddedPointer is missing godoc comment"
34+
2735
// incorrectStartComment is a field with an incorrect start to the comment. // want "godoc for field IncorrectStartComment should start with 'incorrectStartComment ...'"
2836
IncorrectStartComment string `json:"incorrectStartComment"`
2937

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package pkg
2+
3+
type A struct {
4+
NoComment string `json:"noComment"` // want "field NoComment is missing godoc comment"
5+
}
6+
7+
// To embed the same struct multiple times, we need to rename the type.
8+
type Embedded A
9+
type EmbeddedPointer A
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package pkg
2+
3+
type A struct {
4+
NoComment string `json:"noComment"` // want "field NoComment is missing godoc comment"
5+
}
6+
7+
// To embed the same struct multiple times, we need to rename the type.
8+
type Embedded A
9+
type EmbeddedPointer A

0 commit comments

Comments
 (0)