11package  linter
22
33import  (
4+ 	"bytes" 
45	"fmt" 
56
67	"golang.org/x/tools/go/packages" 
8+ 	"gopkg.in/yaml.v3" 
79
810	"github.com/golangci/golangci-lint/v2/pkg/config" 
911)
@@ -20,10 +22,11 @@ const (
2022)
2123
2224type  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 )
2730}
2831
2932type  Config  struct  {
@@ -119,22 +122,26 @@ func (lc *Config) WithSince(version string) *Config {
119122	return  lc 
120123}
121124
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  {
123126	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 )
128134	}
135+ 
129136	return  lc 
130137}
131138
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 ... )
134141}
135142
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 ... )
138145}
139146
140147func  (lc  * Config ) IsDeprecated () bool  {
@@ -160,6 +167,45 @@ func (lc *Config) WithNoopFallback(cfg *config.Config, cond func(cfg *config.Con
160167	return  lc 
161168}
162169
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+ 
163209func  IsGoLowerThanGo122 () func (cfg  * config.Config ) error  {
164210	return  isGoLowerThanGo ("1.22" )
165211}
0 commit comments