Skip to content

Commit bc5ed29

Browse files
committed
godoc: skip build tag annotations when displaying examples
After moving the filepath.Walk example to a standalone example file in CL 122237 (so it could use a standalone function), godoc includes the build tag annotation ("// +build !windows,!plan9" in this case) in the runnable example. The example runs correctly, but the annotation might be confusing for new users. Change the behavior so that godoc skips these annotations when displaying examples. To avoid false positives in older versions of "go vet", which are still used on the build dashboard, we avoid using a multiline string in the test. Fixes golang/go#26490. Change-Id: I1da4b3b7e1e5a85a76773e25d9355b3f92479c19
1 parent ade7853 commit bc5ed29

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

godoc/godoc.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ func (p *Presentation) example_htmlFunc(info *PageInfo, funcName string) string
666666
play := ""
667667
if eg.Play != nil && p.ShowPlayground {
668668
var buf bytes.Buffer
669+
eg.Play.Comments = filterOutBuildAnnotations(eg.Play.Comments)
669670
if err := format.Node(&buf, info.FSet, eg.Play); err != nil {
670671
log.Print(err)
671672
} else {
@@ -694,6 +695,23 @@ func (p *Presentation) example_htmlFunc(info *PageInfo, funcName string) string
694695
return buf.String()
695696
}
696697

698+
func filterOutBuildAnnotations(cg []*ast.CommentGroup) []*ast.CommentGroup {
699+
if len(cg) == 0 {
700+
return cg
701+
}
702+
703+
for i := range cg {
704+
if !strings.HasPrefix(cg[i].Text(), "+build ") {
705+
// Found the first non-build tag, return from here until the end
706+
// of the slice.
707+
return cg[i:]
708+
}
709+
}
710+
711+
// There weren't any non-build tags, return an empty slice.
712+
return []*ast.CommentGroup{}
713+
}
714+
697715
// example_nameFunc takes an example function name and returns its display
698716
// name. For example, "Foo_Bar_quux" becomes "Foo.Bar (Quux)".
699717
func (p *Presentation) example_nameFunc(s string) string {

godoc/godoc_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,50 @@ func TestSrcToPkgLinkFunc(t *testing.T) {
321321
}
322322
}
323323
}
324+
325+
func TestFilterOutBuildAnnotations(t *testing.T) {
326+
// TODO: simplify this by using a multiline string once we stop
327+
// using go vet from 1.10 on the build dashboard.
328+
// https://golang.org/issue/26627
329+
src := []byte("// +build !foo\n" +
330+
"// +build !anothertag\n" +
331+
"\n" +
332+
"// non-tag comment\n" +
333+
"\n" +
334+
"package foo\n" +
335+
"\n" +
336+
"func bar() int {\n" +
337+
" return 42\n" +
338+
"}\n")
339+
340+
fset := token.NewFileSet()
341+
af, err := parser.ParseFile(fset, "foo.go", src, parser.ParseComments)
342+
if err != nil {
343+
t.Fatal(err)
344+
}
345+
346+
var found bool
347+
for _, cg := range af.Comments {
348+
if strings.HasPrefix(cg.Text(), "+build ") {
349+
found = true
350+
break
351+
}
352+
}
353+
if !found {
354+
t.Errorf("TestFilterOutBuildAnnotations is broken: missing build tag in test input")
355+
}
356+
357+
found = false
358+
for _, cg := range filterOutBuildAnnotations(af.Comments) {
359+
if strings.HasPrefix(cg.Text(), "+build ") {
360+
t.Errorf("filterOutBuildAnnotations failed to filter build tag")
361+
}
362+
363+
if strings.Contains(cg.Text(), "non-tag comment") {
364+
found = true
365+
}
366+
}
367+
if !found {
368+
t.Errorf("filterOutBuildAnnotations should not remove non-build tag comment")
369+
}
370+
}

0 commit comments

Comments
 (0)