Skip to content

Commit

Permalink
Add flag to include generated files (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
bombsimon authored Jun 9, 2024
1 parent cf0fb6d commit 1fc4441
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
4 changes: 3 additions & 1 deletion analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -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")
Expand All @@ -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
}

Expand Down
16 changes: 16 additions & 0 deletions testdata/src/default_config/generated_file.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions testdata/src/with_config/include_generated/code.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions testdata/src/with_config/include_generated/code.go.golden
Original file line number Diff line number Diff line change
@@ -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"
}


5 changes: 5 additions & 0 deletions wsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions wsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 1fc4441

Please sign in to comment.