aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcloss <the.cody.oss@gmail.com>2017-07-15 16:07:30 -0600
committerBryan Mills <bcmills@google.com>2017-07-16 01:43:40 +0000
commit91afca94e07aa1366afba9ece04c2def9f99d4c4 (patch)
tree450b8829b190ccd193f6b6ac72e02338a9d5be18
parent42aec4c03857a9bfcc8e445b2ae6abe1c7a055b4 (diff)
downloadgo-91afca94e07aa1366afba9ece04c2def9f99d4c4.tar.gz
go-91afca94e07aa1366afba9ece04c2def9f99d4c4.zip
ast: make ExampleCommentMap a runnable example
Fixes #20450 Change-Id: I2256282a8880e99508e98fefedfb94a7cccacbcf Reviewed-on: https://go-review.googlesource.com/48969 Run-TryBot: Bryan Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com>
-rw-r--r--src/go/ast/example_test.go23
1 files changed, 9 insertions, 14 deletions
diff --git a/src/go/ast/example_test.go b/src/go/ast/example_test.go
index d2e734f2cb..52a77981b8 100644
--- a/src/go/ast/example_test.go
+++ b/src/go/ast/example_test.go
@@ -44,7 +44,7 @@ var X = f(3.14)*2 + c
return true
})
- // output:
+ // Output:
// src.go:2:9: p
// src.go:3:7: c
// src.go:3:11: 1.0
@@ -75,7 +75,7 @@ func main() {
// Print the AST.
ast.Print(fset, f)
- // output:
+ // Output:
// 0 *ast.File {
// 1 . Package: 2:1
// 2 . Name: *ast.Ident {
@@ -172,7 +172,12 @@ func main() {
cmap := ast.NewCommentMap(fset, f, f.Comments)
// Remove the first variable declaration from the list of declarations.
- f.Decls = removeFirstVarDecl(f.Decls)
+ for i, decl := range f.Decls {
+ if gen, ok := decl.(*ast.GenDecl); ok && gen.Tok == token.VAR {
+ copy(f.Decls[i:], f.Decls[i+1:])
+ f.Decls = f.Decls[:len(f.Decls)-1]
+ }
+ }
// Use the comment map to filter comments that don't belong anymore
// (the comments associated with the variable declaration), and create
@@ -186,7 +191,7 @@ func main() {
}
fmt.Printf("%s", buf.Bytes())
- // output:
+ // Output:
// // This is the package comment.
// package main
//
@@ -198,13 +203,3 @@ func main() {
// fmt.Println(hello) // line comment 3
// }
}
-
-func removeFirstVarDecl(list []ast.Decl) []ast.Decl {
- for i, decl := range list {
- if gen, ok := decl.(*ast.GenDecl); ok && gen.Tok == token.VAR {
- copy(list[i:], list[i+1:])
- return list[:len(list)-1]
- }
- }
- panic("variable declaration not found")
-}