go-sumtype icon indicating copy to clipboard operation
go-sumtype copied to clipboard

Package analyzer as a library

Open ifross89 opened this issue 3 years ago • 2 comments

Fixes https://github.com/BurntSushi/go-sumtype/issues/4

Convert the go-sumtype linting logic to use the golang.org/x/tools/go/analysis package and export an analysis.Analyzer from the new sumtype package.

This required a bit more of an invasive change than initially thought. The main changes were:

  • move from packages.Packages package for loading the files to the new analysis package
  • report issues directly to the *analysis.Pass parameter, rather than collecting a list of errors
  • use token.Pos rather than a custom Location() method: the analysis.Pass.Reportf expects a token.Pos

An effort was taken to do this change with the minimum amount of logic changes as possible, to reduce the chance of regression.

There is, however, a minor regression: due to manually searching the go files for the comment directive, we do not have an accurate token.Pos. We instead use the Pos of the whole file which will report at the top of the file. It may be possible to craft a custom token.Pos here, but I do not currently know how to do this.

ifross89 avatar Jun 07 '21 16:06 ifross89

@BurntSushi hey so this is the PR that I mentioned in https://github.com/BurntSushi/go-sumtype/issues/4.

Wondering if there is anything I can do to help with any review? Thanks

ifross89 avatar Jun 15 '21 08:06 ifross89

It is now done at https://github.com/alecthomas/go-check-sumtype

AlekSi avatar Oct 25 '23 13:10 AlekSi