Glint — Extensible, declarative linter for Golang
I’ve become fascinated with go/ast — a library that allows you to inspect the abstract syntax tree of a golang program in its native tongue. While playing around with it over this past weekend after watching a “just for func” episode, I had a idea:
What if there was a declarative way of configuring a golang linter?
I began sketching out the initial program in my editor, creating a JSON file that would serve as the base for all the rules that could be defined.
{
"rules": [
{
"type": "",
"comment": "",
...
},
...
]
}After a little bit more toiling around with the backend engine I had something that could unmarshal the JSON-defined rules, walk the abstract syntax tree of a program, and apply rules to each node of the tree… I had a linter!
Glint
Since all software needs some sort of name, I dubbed this project “glint”. ✨
I wrote three different rule types which glint currently supports:
importto inspect imported packagesstructto inspect the fields of struct typesmethodto inspect method calls
For any of the aforementioned rule types, a comment is required which is displayed in the lint message. In order for someone to extend the available rule types in the core of glint, they would need to create a golang type that satisfies the following interface:
type Rule interface {
Action(fs *token.FileSet, node ast.Node)
}What’s this mean?: A rule must perform some sort of action, which is performed on a node found in the AST. Rule types can be added later as necessary, or even created by outside contributors.
However, the primary goal for glint isn’t really to be easily extensible at the engine source code level. Instead, the focus is to be extensible at the configuration level, describing rules for the linter in something I like to call a Glintfile.
Get Kent Gruber’s stories in your inbox
Join Medium for free to get updates from this writer.
Three example rules that can be described:
{
"rules": [
{
"type": "import",
"comment": "we don't use golang.org/x/* packages",
"match": [
"golang.org/x/\\w+"
],
},
{
"type": "struct",
"comment": "always set ReadTimeout to something not 0",
"name": "http.Server",
"field": "ReadTimeout",
"match": [
"nil", "0"
]
},
{
"type": "method",
"comment": "use 2048 bits or more when generating RSA keys",
"call": "rsa.GenerateKey",
"argument": 1,
"less_than": 2048
}
]
}Using this — I really hope — an operator shouldn’t necessarily need to be a golang AST expert to create or customize the behavior of a rule. Instead they can declaratively describe, share, and collaborate on rules at a higher-level presented in this mini DSL (domain specific language) using a familiar syntax that is both human and machine readable (JSON).
So, how do you run the linter? Great question!
It is primarily meant to be used from the command-line. Though, perhaps in the future it can integrated as a plugin within your text editor of choice.
Installation
$ go get -u github.com/picatz/glintInspect a Single File
$ glint /path/to/source/code.go
...Inspect a Package
$ glint github.com/picatz/backdoor
...Specify the Glintfile
$ glint -rules=/path/to/Glintfile code.goConclusion
You can check out documentation, source code, and examples for glint on GitHub. Contributions are of course welcome if you would like to help improve the core rule types. Otherwise trying it out or attempting to build rules with it to give feedback on the issue tracker would be just as welcome. It’s still in an early stage with some room to improve, but I think it should be quite useful — or at the very least, usable — in its current state.
👏 Finally, I want to give a big thank you to both Fransec Campoy and Fatih Arslan for making such wonderful blog posts about go/ast and related packages for analyzing golang source code.
Until next time, that’s all folks!







