dev/sg: rework lint package to allow FixableLinter and -fix
Created by: bobheadxi
Refactors the lint
package to now export two main ways to build linters:
-
Linter
, which implements aCheck(...)
-
FixableLinter
, which also implements aFix(...)
Using this, sg lint
now:
- on normal
sg lint
, runs checks and suggests that users runsg lint -fix
if any of the failed linters implementFixableLinter
- on
sg lint -fix
, runs all checks, but if a linter implementsFixableLinter
, also runs that
To enable this:
-
repo.State
now parses the full diff up-front, so that linters do not interfere with each other - the
go generate
check now resets the workspace inCheck
- fix an issue with
sg lint [target] -h
to allow us to surface hints that a target has fixable linters
For back-compat, we have:
-
lint.ScriptCheck
, which is more or less the same as the oldlint.Script
-
lint.FuncCheck
, which can wrap the oldlint.Runner
interface
To demonstrate the new API and features, this PR:
- Creates a new
go generate
linter implementation that implementsFixableLinter
- Migrates some linters that have configuration (logging library linter, go mod version linter) to implement the
Linter
interface directly, because the struct func approach gives a better home for configuration
Closes https://github.com/sourcegraph/sourcegraph/issues/35396
Test plan
go run ./dev/sg lint
with a generate issue will suggest that it is fixable, and leave no diff in the workspace:
go run ./dv/sg lint go -h
indicates this target has linters that are fixable:
With -fix
: