Repository: bombsimon/wsl
Pull request #152 (2024-06-09 14:14:16)

Add flag to include generated files
Support to include generated files for linting when desired. Default is `false`.

Relates to #148, addresses https://github.com/bombsimon/wsl/pull/148#issuecomment-2156195156

coveralls: 
[![Coverage Status](https://coveralls.io/builds/67975086/badge)](https://coveralls.io/builds/67975086)

coverage: 93.664% (+0.01%) from 93.651%
when pulling **5f2f06f5bfab021575804e268c22ce76eb4c741d on flag-for-generated**
into **cf0fb6d3999c10ca25f03f87079aea5e84ebb88a on master**.

ldez: From the POV of wsl, I think the default to `false` is the right idea.

Inside golangci-lint, we will not expose this option and turn it to `true`, to avoid conflict with the `autogenerated` processor.

Patch:
diff --git a/analyzer.go b/analyzer.go
index 6ac49c8..46d5019 100644
--- a/analyzer.go
+++ b/analyzer.go
@@ -31,6 +31,7 @@ func defaultConfig() *Configuration {
 		ForceCuddleErrCheckAndAssign:     false,
 		ForceExclusiveShortDeclarations:  false,
 		StrictAppend:                     true,
+		IncludeGenerated:                 false,
 		AllowCuddleWithCalls:             []string{"Lock", "RLock"},
 		AllowCuddleWithRHS:               []string{"Unlock", "RUnlock"},
 		ErrorVariableNames:               []string{"err"},
@@ -65,6 +66,7 @@ func (wa *wslAnalyzer) flags() flag.FlagSet {
 	flags.BoolVar(&wa.config.ForceCuddleErrCheckAndAssign, "force-err-cuddling", false, "Force cuddling of error checks with error var assignment")
 	flags.BoolVar(&wa.config.ForceExclusiveShortDeclarations, "force-short-decl-cuddling", false, "Force short declarations to cuddle by themselves")
 	flags.BoolVar(&wa.config.StrictAppend, "strict-append", true, "Strict rules for append")
+	flags.BoolVar(&wa.config.IncludeGenerated, "include-generated", false, "Include generated files")
 	flags.IntVar(&wa.config.ForceCaseTrailingWhitespaceLimit, "force-case-trailing-whitespace", 0, "Force newlines for case blocks > this number.")
 
 	flags.Var(&multiStringValue{slicePtr: &wa.config.AllowCuddleWithCalls}, "allow-cuddle-with-calls", "Comma separated list of idents that can have cuddles after")
@@ -76,7 +78,7 @@ func (wa *wslAnalyzer) flags() flag.FlagSet {
 
 func (wa *wslAnalyzer) run(pass *analysis.Pass) (interface{}, error) {
 	for _, file := range pass.Files {
-		if ast.IsGenerated(file) {
+		if !wa.config.IncludeGenerated && ast.IsGenerated(file) {
 			continue
 		}
 
diff --git a/wsl.go b/wsl.go
index 6fd3333..0b8d8c0 100644
--- a/wsl.go
+++ b/wsl.go
@@ -174,6 +174,11 @@ type Configuration struct {
 	//
 	// is not allowed. This logic overrides ForceCuddleErrCheckAndAssign among others.
 	ForceExclusiveShortDeclarations bool
+
+	// IncludeGenerated will include generated files in the analysis and report
+	// errors even for generated files. Can be useful when developing
+	// generators.
+	IncludeGenerated bool
 }
 
 // fix is a range to fixup.


Test patch:
diff --git a/testdata/src/default_config/generated_file.go b/testdata/src/default_config/generated_file.go
new file mode 100644
index 0000000..b068b4d
--- /dev/null
+++ b/testdata/src/default_config/generated_file.go
@@ -0,0 +1,16 @@
+// Code generated by xyz; DO NOT EDIT.
+// This file will be ignored for default config since it is generated. Since
+// it's ignored it also doesn't have a golden file.
+package pkg
+
+func Fn() {
+
+	if true {
+
+		_ = 1
+	}
+	if false {
+		_ = 2
+
+	}
+}
diff --git a/testdata/src/with_config/include_generated/code.go b/testdata/src/with_config/include_generated/code.go
new file mode 100644
index 0000000..104a44c
--- /dev/null
+++ b/testdata/src/with_config/include_generated/code.go
@@ -0,0 +1,14 @@
+// Code generated by xyz; DO NOT EDIT.
+package pkg
+
+func Fn() { // want "block should not start with a whitespace"
+
+	if true { // want "block should not start with a whitespace"
+
+		_ = 1
+	}
+	if false { // want "if statements should only be cuddled with assignments"
+		_ = 2
+
+	} // want "block should not end with a whitespace"
+}
diff --git a/testdata/src/with_config/include_generated/code.go.golden b/testdata/src/with_config/include_generated/code.go.golden
new file mode 100644
index 0000000..a32f6eb
--- /dev/null
+++ b/testdata/src/with_config/include_generated/code.go.golden
@@ -0,0 +1,14 @@
+// Code generated by xyz; DO NOT EDIT.
+package pkg
+
+func Fn() { // want "block should not start with a whitespace"
+	if true { // want "block should not start with a whitespace"
+		_ = 1
+	}
+
+	if false { // want "if statements should only be cuddled with assignments"
+		_ = 2
+	} // want "block should not end with a whitespace"
+}
+
+
diff --git a/wsl_test.go b/wsl_test.go
index 4847e0a..fa3c587 100644
--- a/wsl_test.go
+++ b/wsl_test.go
@@ -88,6 +88,12 @@ func TestWithConfig(t *testing.T) {
 				config.AllowCuddleDeclaration = true
 			},
 		},
+		{
+			subdir: "include_generated",
+			configFn: func(config *Configuration) {
+				config.IncludeGenerated = true
+			},
+		},
 	} {
 		t.Run(tc.subdir, func(t *testing.T) {
 			config := defaultConfig()