Skip to content

Commit 8db518c

Browse files
authored
Add versions, improve deprecation system, improve linters page (#1854)
1 parent b6a6faa commit 8db518c

File tree

5 files changed

+156
-39
lines changed

5 files changed

+156
-39
lines changed

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

+20-16
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,35 @@ from scratch and integrate it into `golangci-lint`.
99

1010
## How to add a public linter to `golangci-lint`
1111

12-
You need to implement a new linter using `go/analysis` API. We don't accept not `go/analysis` linters.
12+
You need to implement a new linter using `go/analysis` API.
13+
We don't accept not `go/analysis` linters.
1314

1415
After that:
1516

16-
1. Implement functional tests for the linter: add one file into directory [`test/testdata`](https://github.com/golangci/golangci-lint/tree/master/test/testdata).
17-
Run `T=yourlintername.go make test_linters` to ensure that test fails.
18-
2. Add a new file `pkg/golinters/{yourlintername}.go`. Look at other linters in this directory. Implement linter integration and check that test passes.
17+
1. Implement functional tests for the linter:
18+
- 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`
21+
2. Add a new file `pkg/golinters/{yourlintername}.go`.
22+
Look at other linters in this directory.
23+
Implement linter integration and check that test passes.
1924
3. Add the new struct for the linter (which you've implemented in `pkg/golinters/{yourlintername}.go`) to the
2025
list of all supported linters in [`pkg/lint/lintersdb/manager.go`](https://github.com/golangci/golangci-lint/blob/master/pkg/lint/lintersdb/manager.go)
21-
to the function `GetAllSupportedLinterConfigs`. Enable it by default only if you are sure.
22-
4. Find out what options do you need to configure for the linter. For example, `nakedret` has
23-
only 1 option: [`max-func-lines`](https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml).
26+
to the function `GetAllSupportedLinterConfigs`.
27+
- Add `WithSince("next_version")`, where `next_version` must be replaced by the next minor version. (ex: v1.2.0 if the current version is v1.1.0)
28+
4. Find out what options do you need to configure for the linter.
29+
For example, `nakedret` has only 1 option: [`max-func-lines`](https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml).
2430
Choose default values to not being annoying for users of golangci-lint. Add configuration options to:
25-
26-
- [.golangci.example.yml](https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml) - the example of a configuration file. You can also add
27-
them to [.golangci.yml](https://github.com/golangci/golangci-lint/blob/master/.golangci.yml) if you think
28-
that this project needs not default values.
29-
- [config struct](https://github.com/golangci/golangci-lint/blob/master/pkg/config/config.go) - don't forget
30-
about `mapstructure` tag for proper configuration files parsing by [pflag](https://github.com/spf13/pflag).
31-
32-
5. Take a look at the example of [Pull Request with new linter support](https://github.com/golangci/golangci-lint/pull/850).
31+
- [.golangci.example.yml](https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml) - the example of a configuration file.
32+
You can also add them to [.golangci.yml](https://github.com/golangci/golangci-lint/blob/master/.golangci.yml)
33+
if you think that this project needs not default values.
34+
- [config struct](https://github.com/golangci/golangci-lint/blob/master/pkg/config/config.go) -
35+
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).
3337

3438
## How to add a private linter to `golangci-lint`
3539

36-
Some people and organizations may choose to have custom made linters run as a part of `golangci-lint`.
40+
Some people and organizations may choose to have custom-made linters run as a part of `golangci-lint`.
3741
Typically, these linters can't be open-sourced or too specific.
3842
Such linters can be added through Go's plugin library.
3943

pkg/lint/linter/config.go

+25-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ const (
2020
PresetUnused = "unused" // Related to the detection of unused code.
2121
)
2222

23+
type Deprecation struct {
24+
Since string
25+
Message string
26+
Replacement string
27+
}
28+
2329
type Config struct {
2430
Linter Linter
2531
EnabledByDefault bool
@@ -29,11 +35,13 @@ type Config struct {
2935
InPresets []string
3036
AlternativeNames []string
3137

32-
OriginalURL string // URL of original (not forked) repo, needed for autogenerated README
33-
CanAutoFix bool
34-
IsSlow bool
35-
DoesChangeTypes bool
36-
DeprecatedMessage string
38+
OriginalURL string // URL of original (not forked) repo, needed for autogenerated README
39+
CanAutoFix bool
40+
IsSlow bool
41+
DoesChangeTypes bool
42+
43+
Since string
44+
Deprecation *Deprecation
3745
}
3846

3947
func (lc *Config) ConsiderSlow() *Config {
@@ -82,13 +90,22 @@ func (lc *Config) WithChangeTypes() *Config {
8290
return lc
8391
}
8492

85-
func (lc *Config) Deprecated(message string) *Config {
86-
lc.DeprecatedMessage = message
93+
func (lc *Config) WithSince(version string) *Config {
94+
lc.Since = version
95+
return lc
96+
}
97+
98+
func (lc *Config) Deprecated(message, version, replacement string) *Config {
99+
lc.Deprecation = &Deprecation{
100+
Since: version,
101+
Message: message,
102+
Replacement: replacement,
103+
}
87104
return lc
88105
}
89106

90107
func (lc *Config) IsDeprecated() bool {
91-
return lc.DeprecatedMessage != ""
108+
return lc.Deprecation != nil
92109
}
93110

94111
func (lc *Config) AllNames() []string {

0 commit comments

Comments
 (0)