-
Notifications
You must be signed in to change notification settings - Fork 18.1k
go/ast: CommentMap mishandles loose, trailing file comments #21755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
CC @griesemer |
I also see that there even is a test that includes this:
In that case, the trailing comment in the file is attached to an Another option would be to attach it to the whole |
Closed by mistake in one of @josharian's private repos, I assume. |
A couple of other weird cases: https://play.golang.org/p/d46URuVRTJ |
Indeed. FWIW, here's the diff I used, which fixed it the way I wanted it fixed for my private purposes. I haven't thought about whether it is an appropriate fix for the diff --git a/astutil/commentmap.go b/astutil/commentmap.go
index c6f0d62..7c8170f 100644
--- a/astutil/commentmap.go
+++ b/astutil/commentmap.go
@@ -206,7 +206,8 @@ func NewCommentMap(fset *token.FileSet, node ast.Node, comments []*ast.CommentGr
switch {
case pg != nil &&
(pgend.Line == r.pos.Line ||
- pgend.Line+1 == r.pos.Line && r.end.Line+1 < qpos.Line):
+ pgend.Line+1 == r.pos.Line && r.end.Line+1 < qpos.Line ||
+ q == nil):
// 1) comment starts on same line as previous node group ends, or
// 2) comment starts on the line immediately after the
// previous node group and there is an empty line before
diff --git a/astutil/commentmap_test.go b/astutil/commentmap_test.go
index 3df0e65..a645032 100644
--- a/astutil/commentmap_test.go
+++ b/astutil/commentmap_test.go
@@ -92,9 +92,9 @@ var res = map[string]string{
"31: *ast.ExprStmt": " associated with s1\nalso associated with s1\n",
"37: *ast.ExprStmt": "associated with s2\nalso associated with s2\nline comment for s2\n",
"45: *ast.FuncDecl": "associated with f2\nf2\n",
+ "48: *ast.FuncDecl": "the very last comment\n",
"49: *ast.AssignStmt": "addition\n",
"49: *ast.BasicLit": " 1\n",
- "50: *ast.Ident": "the very last comment\n",
} |
go version devel +812b34efae Mon Sep 4 00:07:33 2017 +0000 linux/amd64
Construct a comment map for:
The loose trailing comment is associated in the CommentMap with the
return statement inside f. It should probably be associated with f
instead, and the NewCommentMap docs updated with new rules.
Live proof: https://play.golang.org/p/XCktcSxYl8
The text was updated successfully, but these errors were encountered: