Skip to content

Commit 1fc4441

Browse files
authored
Add flag to include generated files (#152)
1 parent cf0fb6d commit 1fc4441

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

analyzer.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func defaultConfig() *Configuration {
3131
ForceCuddleErrCheckAndAssign: false,
3232
ForceExclusiveShortDeclarations: false,
3333
StrictAppend: true,
34+
IncludeGenerated: false,
3435
AllowCuddleWithCalls: []string{"Lock", "RLock"},
3536
AllowCuddleWithRHS: []string{"Unlock", "RUnlock"},
3637
ErrorVariableNames: []string{"err"},
@@ -65,6 +66,7 @@ func (wa *wslAnalyzer) flags() flag.FlagSet {
6566
flags.BoolVar(&wa.config.ForceCuddleErrCheckAndAssign, "force-err-cuddling", false, "Force cuddling of error checks with error var assignment")
6667
flags.BoolVar(&wa.config.ForceExclusiveShortDeclarations, "force-short-decl-cuddling", false, "Force short declarations to cuddle by themselves")
6768
flags.BoolVar(&wa.config.StrictAppend, "strict-append", true, "Strict rules for append")
69+
flags.BoolVar(&wa.config.IncludeGenerated, "include-generated", false, "Include generated files")
6870
flags.IntVar(&wa.config.ForceCaseTrailingWhitespaceLimit, "force-case-trailing-whitespace", 0, "Force newlines for case blocks > this number.")
6971

7072
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 {
7678

7779
func (wa *wslAnalyzer) run(pass *analysis.Pass) (interface{}, error) {
7880
for _, file := range pass.Files {
79-
if ast.IsGenerated(file) {
81+
if !wa.config.IncludeGenerated && ast.IsGenerated(file) {
8082
continue
8183
}
8284

testdata/src/default_config/generated_file.go

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/src/with_config/include_generated/code.go

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Code generated by xyz; DO NOT EDIT.
2+
package pkg
3+
4+
func Fn() { // want "block should not start with a whitespace"
5+
if true { // want "block should not start with a whitespace"
6+
_ = 1
7+
}
8+
9+
if false { // want "if statements should only be cuddled with assignments"
10+
_ = 2
11+
} // want "block should not end with a whitespace"
12+
}
13+
14+

wsl.go

+5
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ type Configuration struct {
174174
//
175175
// is not allowed. This logic overrides ForceCuddleErrCheckAndAssign among others.
176176
ForceExclusiveShortDeclarations bool
177+
178+
// IncludeGenerated will include generated files in the analysis and report
179+
// errors even for generated files. Can be useful when developing
180+
// generators.
181+
IncludeGenerated bool
177182
}
178183

179184
// fix is a range to fixup.

wsl_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ func TestWithConfig(t *testing.T) {
8888
config.AllowCuddleDeclaration = true
8989
},
9090
},
91+
{
92+
subdir: "include_generated",
93+
configFn: func(config *Configuration) {
94+
config.IncludeGenerated = true
95+
},
96+
},
9197
} {
9298
t.Run(tc.subdir, func(t *testing.T) {
9399
config := defaultConfig()

0 commit comments

Comments
 (0)