diff --git a/cmd/bundle/.gitignore b/cmd/bundle/.gitignore new file mode 100644 index 0000000000..caaeb09d8c --- /dev/null +++ b/cmd/bundle/.gitignore @@ -0,0 +1 @@ +testdata/out.got diff --git a/cmd/bundle/main.go b/cmd/bundle/main.go index e4b261faab..02bd83573e 100644 --- a/cmd/bundle/main.go +++ b/cmd/bundle/main.go @@ -88,6 +88,7 @@ import ( "go/build" "go/format" "go/parser" + "go/printer" "go/token" "go/types" "io/ioutil" @@ -348,18 +349,13 @@ func bundle(src, dst, dstpkg, prefix string) ([]byte, error) { // Pretty-print package-level declarations. // but no package or import declarations. - // - // TODO(adonovan): this may cause loss of comments - // preceding or associated with the package or import - // declarations or not associated with any declaration. - // Check. var buf bytes.Buffer for _, decl := range f.Decls { if decl, ok := decl.(*ast.GenDecl); ok && decl.Tok == token.IMPORT { continue } buf.Reset() - format.Node(&buf, lprog.Fset, decl) + format.Node(&buf, lprog.Fset, &printer.CommentedNode{Node: decl, Comments: f.Comments}) // Remove each "@@@." in the output. // TODO(adonovan): not hygienic. out.Write(bytes.Replace(buf.Bytes(), []byte("@@@."), nil, -1)) diff --git a/cmd/bundle/testdata/out.golden b/cmd/bundle/testdata/out.golden index 9374b2e9f7..b76c5ae749 100644 --- a/cmd/bundle/testdata/out.golden +++ b/cmd/bundle/testdata/out.golden @@ -26,7 +26,9 @@ type prefixS struct { } // Function bar. -func prefixbar(s *prefixS) { fmt.Println(s.prefixt, s.u) } +func prefixbar(s *prefixS) { + fmt.Println(s.prefixt, s.u) // comment inside function +} type prefixt int diff --git a/cmd/bundle/testdata/src/initial/a.go b/cmd/bundle/testdata/src/initial/a.go index 99cd145abc..5d0d19009e 100644 --- a/cmd/bundle/testdata/src/initial/a.go +++ b/cmd/bundle/testdata/src/initial/a.go @@ -12,4 +12,6 @@ type S struct { } // Function bar. -func bar(s *S) { fmt.Println(s.t, s.u) } +func bar(s *S) { + fmt.Println(s.t, s.u) // comment inside function +}