1
1
package linter
2
2
3
3
import (
4
+ "bytes"
4
5
"fmt"
5
6
6
7
"golang.org/x/tools/go/packages"
8
+ "gopkg.in/yaml.v3"
7
9
8
10
"github.com/golangci/golangci-lint/v2/pkg/config"
9
11
)
@@ -20,10 +22,11 @@ const (
20
22
)
21
23
22
24
type Deprecation struct {
23
- Since string
24
- Message string
25
- Replacement string
26
- Level DeprecationLevel
25
+ Since string
26
+ Message string
27
+ Replacement string
28
+ Level DeprecationLevel
29
+ ConfigSuggestion func () (string , error )
27
30
}
28
31
29
32
type Config struct {
@@ -119,22 +122,26 @@ func (lc *Config) WithSince(version string) *Config {
119
122
return lc
120
123
}
121
124
122
- func (lc * Config ) Deprecated (message , version , replacement string , level DeprecationLevel ) * Config {
125
+ func (lc * Config ) Deprecated (message , version string , level DeprecationLevel , opts ... func ( * Deprecation ) ) * Config {
123
126
lc .Deprecation = & Deprecation {
124
- Since : version ,
125
- Message : message ,
126
- Replacement : replacement ,
127
- Level : level ,
127
+ Since : version ,
128
+ Message : message ,
129
+ Level : level ,
130
+ }
131
+
132
+ for _ , opt := range opts {
133
+ opt (lc .Deprecation )
128
134
}
135
+
129
136
return lc
130
137
}
131
138
132
- func (lc * Config ) DeprecatedWarning (message , version , replacement string ) * Config {
133
- return lc .Deprecated (message , version , replacement , DeprecationWarning )
139
+ func (lc * Config ) DeprecatedWarning (message , version string , opts ... func ( * Deprecation ) ) * Config {
140
+ return lc .Deprecated (message , version , DeprecationWarning , opts ... )
134
141
}
135
142
136
- func (lc * Config ) DeprecatedError (message , version , replacement string ) * Config {
137
- return lc .Deprecated (message , version , replacement , DeprecationError )
143
+ func (lc * Config ) DeprecatedError (message , version string , opts ... func ( * Deprecation ) ) * Config {
144
+ return lc .Deprecated (message , version , DeprecationError , opts ... )
138
145
}
139
146
140
147
func (lc * Config ) IsDeprecated () bool {
@@ -160,6 +167,45 @@ func (lc *Config) WithNoopFallback(cfg *config.Config, cond func(cfg *config.Con
160
167
return lc
161
168
}
162
169
170
+ func Replacement [T any ](replacement string , mgr func (T ) any , data T ) func (* Deprecation ) {
171
+ return func (d * Deprecation ) {
172
+ if replacement == "" {
173
+ return
174
+ }
175
+
176
+ d .Replacement = replacement
177
+
178
+ if mgr == nil {
179
+ return
180
+ }
181
+
182
+ d .ConfigSuggestion = func () (string , error ) {
183
+ buf := bytes .NewBuffer ([]byte {})
184
+
185
+ encoder := yaml .NewEncoder (buf )
186
+ encoder .SetIndent (2 )
187
+
188
+ suggestion := map [string ]any {
189
+ "linters" : map [string ]any {
190
+ "enable" : []string {
191
+ d .Replacement ,
192
+ },
193
+ "settings" : map [string ]any {
194
+ d .Replacement : mgr (data ),
195
+ },
196
+ },
197
+ }
198
+
199
+ err := encoder .Encode (suggestion )
200
+ if err != nil {
201
+ return "" , fmt .Errorf ("%s: invalid configuration: %w" , d .Replacement , err )
202
+ }
203
+
204
+ return buf .String (), nil
205
+ }
206
+ }
207
+ }
208
+
163
209
func IsGoLowerThanGo122 () func (cfg * config.Config ) error {
164
210
return isGoLowerThanGo ("1.22" )
165
211
}
0 commit comments