Repository: bombsimon/wsl
Pull request #148 (2024-05-24 13:34:32)

Skip analysis of generated files
Hi! I have a suggestion to make, hope you don't mind :)
Since `golangci-lint` doesn't support SuggestedFixes (https://github.com/golangci/golangci-lint/issues/1779), in my project I call `wsl --fix` separately. And it turns out that the linter fixes everything, including generated files (for example, files that are generated by protoc-gen-go). 
This PR suggest to skip such files. It uses `ast.IsGenerated(file *ast.File)` as recommended in https://github.com/golang/go/issues/43481 and https://github.com/golang/go/issues/28089



bombsimon: Thanks! I think this makes sense! Either you're not in control of the generator and can't do anything about it or you are and should fix the generator. Since generated files usually shouldn't be edited I can't think of a reason to not do this or make it an opt-in and just always ignore them.

I'll merge this and if someone really want's to fix generated files we can add a flag in the future.
coveralls: 
[![Coverage Status](https://coveralls.io/builds/67698259/badge)](https://coveralls.io/builds/67698259)

coverage: 93.651% (+0.01%) from 93.637%
when pulling **52dcdb2619775ae4b6e4a7e8a711a5a7f1881ed9 on kseniadumpling:feature/skip-generated-files**
into **115422b8a61107d8b8832ffa5d8b56e76453132d on bombsimon:master**.

kseniadumpling: @bombsimon sure!
rebased 👌🏻 

Patch:
diff --git a/analyzer.go b/analyzer.go
index b8eac15..6ac49c8 100644
--- a/analyzer.go
+++ b/analyzer.go
@@ -2,6 +2,7 @@ package wsl
 
 import (
 	"flag"
+	"go/ast"
 	"strings"
 
 	"golang.org/x/tools/go/analysis"
@@ -75,6 +76,10 @@ func (wa *wslAnalyzer) flags() flag.FlagSet {
 
 func (wa *wslAnalyzer) run(pass *analysis.Pass) (interface{}, error) {
 	for _, file := range pass.Files {
+		if ast.IsGenerated(file) {
+			continue
+		}
+
 		filename := pass.Fset.PositionFor(file.Pos(), false).Filename
 		if !strings.HasSuffix(filename, ".go") {
 			continue


Test patch:
diff --git a/testdata/src/default_config/generated.go b/testdata/src/default_config/generated.go
new file mode 100644
index 0000000..b93e418
--- /dev/null
+++ b/testdata/src/default_config/generated.go
@@ -0,0 +1,21 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+
+package chat
+
+func _ChatService_SayHello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
+	in := new(Message)
+	if err := dec(in); err != nil {
+		return nil, err
+	}
+	if interceptor == nil {
+		return srv.(ChatServiceServer).SayHello(ctx, in)
+	}
+	info := &grpc.UnaryServerInfo{
+		Server:     srv,
+		FullMethod: "/go.grpc.http.muxer.com.chat.v1.ChatService/SayHello",
+	}
+	handler := func(ctx context.Context, req interface{}) (interface{}, error) {
+		return srv.(ChatServiceServer).SayHello(ctx, req.(*Message))
+	}
+	return interceptor(ctx, in, info, handler)
+}
diff --git a/testdata/src/default_config/generated.go.golden b/testdata/src/default_config/generated.go.golden
new file mode 100644
index 0000000..b93e418
--- /dev/null
+++ b/testdata/src/default_config/generated.go.golden
@@ -0,0 +1,21 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+
+package chat
+
+func _ChatService_SayHello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
+	in := new(Message)
+	if err := dec(in); err != nil {
+		return nil, err
+	}
+	if interceptor == nil {
+		return srv.(ChatServiceServer).SayHello(ctx, in)
+	}
+	info := &grpc.UnaryServerInfo{
+		Server:     srv,
+		FullMethod: "/go.grpc.http.muxer.com.chat.v1.ChatService/SayHello",
+	}
+	handler := func(ctx context.Context, req interface{}) (interface{}, error) {
+		return srv.(ChatServiceServer).SayHello(ctx, req.(*Message))
+	}
+	return interceptor(ctx, in, info, handler)
+}