From 72e043e48bf47ce03229b011d2468f46081055ad Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Tue, 12 Nov 2019 00:52:57 +0530 Subject: [PATCH] cmd/doc: show variables of unexported types for -all We use the typedValue map to prevent showing typed variables and constants from appearing in the VARIABLES/CONSTANTS section because they will be anyways shown in the TYPES section for that type. However, when a type is unexported, but the variable is exported, then unconditionally setting it to true in the map suppresses it from being shown in the VARIABLES section. Thus, we set the variable or constant in the typedValue map only when the type name is exported. Fixes #31067 Change-Id: Id3ec4b313c9ea7e3ce6fe279680d56f65451719f Reviewed-on: https://go-review.googlesource.com/c/go/+/206129 Run-TryBot: Agniva De Sarker Reviewed-by: Rob Pike TryBot-Result: Gobot Gobot --- src/cmd/doc/doc_test.go | 1 + src/cmd/doc/pkg.go | 20 ++++++++++---------- src/cmd/doc/testdata/pkg.go | 2 ++ 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/cmd/doc/doc_test.go b/src/cmd/doc/doc_test.go index 5c6ec85703..e425045ba5 100644 --- a/src/cmd/doc/doc_test.go +++ b/src/cmd/doc/doc_test.go @@ -176,6 +176,7 @@ var tests = []test{ `Comment about block of variables`, `VarFive = 5`, `var ExportedVariable = 1`, + `var ExportedVarOfUnExported unexportedType`, `var LongLine = newLongLine\(`, `var MultiLineVar = map\[struct {`, `FUNCTIONS`, diff --git a/src/cmd/doc/pkg.go b/src/cmd/doc/pkg.go index fa31eba64b..bfbe765d32 100644 --- a/src/cmd/doc/pkg.go +++ b/src/cmd/doc/pkg.go @@ -172,18 +172,18 @@ func parsePackage(writer io.Writer, pkg *build.Package, userPath string) *Packag constructor := make(map[*doc.Func]bool) for _, typ := range docPkg.Types { docPkg.Consts = append(docPkg.Consts, typ.Consts...) - for _, value := range typ.Consts { - typedValue[value] = true - } docPkg.Vars = append(docPkg.Vars, typ.Vars...) - for _, value := range typ.Vars { - typedValue[value] = true - } docPkg.Funcs = append(docPkg.Funcs, typ.Funcs...) - for _, fun := range typ.Funcs { - // We don't count it as a constructor bound to the type - // if the type itself is not exported. - if isExported(typ.Name) { + if isExported(typ.Name) { + for _, value := range typ.Consts { + typedValue[value] = true + } + for _, value := range typ.Vars { + typedValue[value] = true + } + for _, fun := range typ.Funcs { + // We don't count it as a constructor bound to the type + // if the type itself is not exported. constructor[fun] = true } } diff --git a/src/cmd/doc/testdata/pkg.go b/src/cmd/doc/testdata/pkg.go index 759b7723a6..d695bdf1c5 100644 --- a/src/cmd/doc/testdata/pkg.go +++ b/src/cmd/doc/testdata/pkg.go @@ -35,6 +35,8 @@ const ( // Comment about exported variable. var ExportedVariable = 1 +var ExportedVarOfUnExported unexportedType + // Comment about internal variable. var internalVariable = 2 -- 2.50.0