Printing *go/ast.CommentGroup

47 views
Skip to first unread message

Shane H

unread,
Jan 25, 2020, 9:49:15 PM1/25/20
to golang-nuts
Hi all, I'm trying to learn how to write a linter (because long weekend, etc)

I looked at Fatih's very fine blog post (https://arslan.io/2019/06/13/using-go-analysis-to-write-a-custom-linter/) as well as the one that precedes it, although I was a LOT lost reading that one.

Copying and pasting https://github.com/fatih/addlint/blob/master/addcheck/addcheck.go got me started, but the linter I have in mind needs to see strings, a trip to https://golang.org/src/go/token/token.go and https://golang.org/src/go/ast/ast.go showed me the possibilities. This  (fortunately?) made me switch priorities slightly, as I now want to write something that is going to print each and every node (and this is where my problems began).

I used Fatih's run and render functions but discovered that an error is being generated for the Doc comment, and I cannot see *why* (ast.CommentGroup is fine in parts of the code.. except for `func (p *printer) printNode(node interface{}) error` (https://golang.org/src/go/printer/printer.go line 1073), the format node section (lines 1125 - 1155) take me to the unsupported label, which generates an error

My code (main.go, lencheck.go, and complete output can be found at https://play.golang.org/p/olvJ64EDdKZ, please excuse the length of the paste, I put *everything* in there)

As you can see in my code there are 4 nodes that throw errors, and I don't understand if 1) that is intended or 2) I am doing (or not doing as the case may be) something to cause the issue.

 Any help/pointers appreciated

Shane H

unread,
Jan 26, 2020, 2:40:51 PM1/26/20
to golang-nuts
I've ended up writing my own print method, that prints the text for the given node. I'm not 100% sure why the format node function is needed for certain nodes, but I also realised I could just print the comments out in my code fairly easily.

The reason I need to do it is because the first phase of writing my tool is 'seeing' what the code is seeing, and reacting to.
Reply all
Reply to author
Forward
0 new messages