Skip to content

Commit c208db8

Browse files
committed
docs: update doc to use the New function
1 parent 096c704 commit c208db8

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

docs/src/docs/contributing/new-linters.mdx

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: New linters
33
---
44

5-
## How to write a custom linter
5+
## How to write a linter
66

77
Use `go/analysis` and take a look at [this tutorial](https://disaev.me/p/writing-useful-go-analysis-linter/):
88
it shows how to write `go/analysis` linter from scratch and integrate it into `golangci-lint`.
@@ -16,8 +16,14 @@ After that:
1616

1717
1. Implement functional tests for the linter:
1818
- Add one file into directory [`test/testdata`](https://github.com/golangci/golangci-lint/tree/master/test/testdata).
19-
- Run `T=yourlintername.go make test_linters` to ensure that test fails.
20-
- Run `go run ./cmd/golangci-lint/ run --no-config --disable-all --enable=yourlintername ./test/testdata/yourlintername.go`
19+
- Run the test to ensure that test fails:
20+
```bash
21+
T=yourlintername.go make test_linters
22+
```
23+
- Run:
24+
```bash
25+
go run ./cmd/golangci-lint/ run --no-config --disable-all --enable=yourlintername ./test/testdata/yourlintername.go
26+
```
2127
2. Add a new file `pkg/golinters/{yourlintername}.go`.
2228
Look at other linters in this directory.
2329
Implement linter integration and check that test passes.
@@ -33,7 +39,7 @@ After that:
3339
if you think that this project needs not default values.
3440
- [config struct](https://github.com/golangci/golangci-lint/blob/master/pkg/config/config.go) -
3541
don't forget about `mapstructure` tag for proper configuration files parsing by [pflag](https://github.com/spf13/pflag).
36-
5. Take a look at the example of [Pull Request with new linter support](https://github.com/golangci/golangci-lint/pulls?q=is%3Apr+is%3Amerged+label%3A%22linter%3A+new%22).
42+
5. Take a look at the example of [pull requests with new linter support](https://github.com/golangci/golangci-lint/pulls?q=is%3Apr+is%3Amerged+label%3A%22linter%3A+new%22).
3743

3844
## How to add a private linter to `golangci-lint`
3945

@@ -43,7 +49,9 @@ Typically, these linters can't be open-sourced or too specific.
4349
Such linters can be added through Go's plugin library.
4450

4551
For a private linter (which acts as a plugin) to work properly,
46-
the plugin as well as the golangci-lint binary needs to be built for the same environment. `CGO_ENABLED` is another requirement.
52+
the plugin as well as the golangci-lint binary **needs to be built for the same environment**.
53+
54+
`CGO_ENABLED` is another requirement.
4755

4856
This means that `golangci-lint` needs to be built for whatever machine you intend to run it on
4957
(cloning the golangci-lint repository and running a `CGO_ENABLED=1 make build` should do the trick for your machine).
@@ -65,6 +73,12 @@ If you're looking for instructions on how to configure your own custom linter, t
6573
path: /example.so
6674
description: The description of the linter
6775
original-url: github.com/golangci/example-linter
76+
settings: # Settings are optional.
77+
one: Foo
78+
two:
79+
- name: Bar
80+
three:
81+
name: Bar
6882
```
6983
7084
That is all the configuration that is required to run a custom linter in your project.
@@ -74,26 +88,27 @@ They can be enabled by adding them the `linters.enable` list, or providing the e
7488

7589
### Create a Plugin
7690

77-
Your linter must implement one or more `golang.org/x/tools/go/analysis.Analyzer` structs.
91+
Your linter must provide one or more `golang.org/x/tools/go/analysis.Analyzer` structs.
7892

7993
Your project should also use `go.mod`.
8094

8195
All versions of libraries that overlap `golangci-lint` (including replaced libraries) MUST be set to the same version as `golangci-lint`.
8296
You can see the versions by running `go version -m golangci-lint`.
8397

84-
You'll also need to create a go file like `plugin/example.go`.
85-
This MUST be in the package `main`, and define a variable of name `AnalyzerPlugin`.
86-
87-
The `AnalyzerPlugin` instance MUST implement the following interface:
98+
You'll also need to create a Go file like `plugin/example.go`.
8899

100+
This file MUST be in the package `main`, and MUST define an exposed function called `New` with the following signature:
89101
```go
90-
type AnalyzerPlugin interface {
91-
GetAnalyzers() []*analysis.Analyzer
102+
func New(conf any) ([]*analysis.Analyzer, error) {
103+
// ...
92104
}
93105
```
94106

95-
The type of `AnalyzerPlugin` is not important, but is by convention `type analyzerPlugin struct {}`.
96107
See [plugin/example.go](https://github.com/golangci/example-plugin-linter/blob/master/plugin/example.go) for more info.
97108

98-
To build the plugin, from the root project directory, run `go build -buildmode=plugin plugin/example.go`.
99-
This will create a plugin `*.so` file that can be copied into your project or another well known location for usage in golangci-lint.
109+
To build the plugin, from the root project directory, run:
110+
```bash
111+
go build -buildmode=plugin plugin/example.go
112+
```
113+
114+
This will create a plugin `*.so` file that can be copied into your project or another well known location for usage in `golangci-lint`.

0 commit comments

Comments
 (0)