diff --git a/.gostyle.yml.init b/.gostyle.yml.init index b2334e1..f5dfbb4 100644 --- a/.gostyle.yml.init +++ b/.gostyle.yml.init @@ -15,3 +15,5 @@ analyzers: # medium-varname-max: 8 # max length of variable name for medium scope (default: -1) # large-varname-max: 16 # max length of variable name for large scope (default: -1) # very-large-varname-max: 32 # max length of variable name for very large scope (default: -1) +# exclude-files: # exclude files from analysis +# - path/to/**/*.go diff --git a/analyzer/code_review_comments/contexts/contexts.go b/analyzer/code_review_comments/contexts/contexts.go index 6f85f58..c1d0a7e 100644 --- a/analyzer/code_review_comments/contexts/contexts.go +++ b/analyzer/code_review_comments/contexts/contexts.go @@ -54,10 +54,12 @@ func run(pass *analysis.Pass) (any, error) { if err != nil { return nil, err } + var opts []reporter.Option if c != nil { disable = c.IsDisabled(name) includeGenerated = c.AnalyzersSettings.Contexts.IncludeGenerated excludeTest = c.AnalyzersSettings.Contexts.ExcludeTest + opts = append(opts, reporter.ExcludeFiles(c.ConfigDir, c.ExcludeFiles)) } if disable { return nil, nil @@ -73,7 +75,6 @@ func run(pass *analysis.Pass) (any, error) { (*ast.StructType)(nil), } - var opts []reporter.Option if includeGenerated { opts = append(opts, reporter.IncludeGenerated()) } diff --git a/analyzer/code_review_comments/dontpanic/dontpanic.go b/analyzer/code_review_comments/dontpanic/dontpanic.go index 6cf115f..2cb907f 100644 --- a/analyzer/code_review_comments/dontpanic/dontpanic.go +++ b/analyzer/code_review_comments/dontpanic/dontpanic.go @@ -53,10 +53,13 @@ func run(pass *analysis.Pass) (any, error) { if err != nil { return nil, err } + + var opts []reporter.Option if c != nil { disable = c.IsDisabled(name) includeGenerated = c.AnalyzersSettings.Dontpanic.IncludeGenerated excludeTest = c.AnalyzersSettings.Dontpanic.ExcludeTest + opts = append(opts, reporter.ExcludeFiles(c.ConfigDir, c.ExcludeFiles)) } if disable { return nil, nil @@ -70,7 +73,6 @@ func run(pass *analysis.Pass) (any, error) { (*ast.CallExpr)(nil), } - var opts []reporter.Option if includeGenerated { opts = append(opts, reporter.IncludeGenerated()) } diff --git a/analyzer/code_review_comments/errorstrings/errorstrings.go b/analyzer/code_review_comments/errorstrings/errorstrings.go index 5a988d8..568d2e7 100644 --- a/analyzer/code_review_comments/errorstrings/errorstrings.go +++ b/analyzer/code_review_comments/errorstrings/errorstrings.go @@ -54,10 +54,12 @@ func run(pass *analysis.Pass) (any, error) { if err != nil { return nil, err } + var opts []reporter.Option if c != nil { disable = c.IsDisabled(name) includeGenerated = c.AnalyzersSettings.Errorstrings.IncludeGenerated excludeTest = c.AnalyzersSettings.Errorstrings.ExcludeTest + opts = append(opts, reporter.ExcludeFiles(c.ConfigDir, c.ExcludeFiles)) } if disable { return nil, nil @@ -71,7 +73,6 @@ func run(pass *analysis.Pass) (any, error) { (*ast.CallExpr)(nil), } - var opts []reporter.Option if includeGenerated { opts = append(opts, reporter.IncludeGenerated()) } diff --git a/analyzer/code_review_comments/handlerrors/handlerrors.go b/analyzer/code_review_comments/handlerrors/handlerrors.go index 3f2f7f5..a390184 100644 --- a/analyzer/code_review_comments/handlerrors/handlerrors.go +++ b/analyzer/code_review_comments/handlerrors/handlerrors.go @@ -58,10 +58,12 @@ func run(pass *analysis.Pass) (any, error) { if err != nil { return nil, err } + var opts []reporter.Option if c != nil { disable = c.IsDisabled(name) includeGenerated = c.AnalyzersSettings.Handlerrors.IncludeGenerated excludeTest = c.AnalyzersSettings.Handlerrors.ExcludeTest + opts = append(opts, reporter.ExcludeFiles(c.ConfigDir, c.ExcludeFiles)) } if disable { return nil, nil @@ -75,7 +77,6 @@ func run(pass *analysis.Pass) (any, error) { (*ast.AssignStmt)(nil), } - var opts []reporter.Option if includeGenerated { opts = append(opts, reporter.IncludeGenerated()) } diff --git a/analyzer/decisions/funcfmt/funcfmt.go b/analyzer/decisions/funcfmt/funcfmt.go index 5afcaf8..9cc1462 100644 --- a/analyzer/decisions/funcfmt/funcfmt.go +++ b/analyzer/decisions/funcfmt/funcfmt.go @@ -50,11 +50,14 @@ var AnalyzerWithConfig = &analysis.Analyzer{ func run(pass *analysis.Pass) (any, error) { c, err := config.Load(pass) if err != nil { + return nil, err } + var opts []reporter.Option if c != nil { disable = c.IsDisabled(name) includeGenerated = c.AnalyzersSettings.Funcfmt.IncludeGenerated + opts = append(opts, reporter.ExcludeFiles(c.ConfigDir, c.ExcludeFiles)) } if disable { @@ -70,7 +73,6 @@ func run(pass *analysis.Pass) (any, error) { (*ast.CallExpr)(nil), } - var opts []reporter.Option if includeGenerated { opts = append(opts, reporter.IncludeGenerated()) } diff --git a/analyzer/decisions/getters/getters.go b/analyzer/decisions/getters/getters.go index 559c4d0..33b5ba5 100644 --- a/analyzer/decisions/getters/getters.go +++ b/analyzer/decisions/getters/getters.go @@ -57,10 +57,12 @@ func run(pass *analysis.Pass) (any, error) { return nil, err } words := strings.Split(exclude, ",") + var opts []reporter.Option if c != nil { disable = c.IsDisabled(name) words = c.AnalyzersSettings.Getters.Exclude includeGenerated = c.AnalyzersSettings.Getters.IncludeGenerated + opts = append(opts, reporter.ExcludeFiles(c.ConfigDir, c.ExcludeFiles)) } if disable { return nil, nil @@ -77,7 +79,6 @@ func run(pass *analysis.Pass) (any, error) { (*ast.AssignStmt)(nil), } - var opts []reporter.Option if includeGenerated { opts = append(opts, reporter.IncludeGenerated()) } diff --git a/analyzer/decisions/nilslices/nilslices.go b/analyzer/decisions/nilslices/nilslices.go index 0d42203..2730f5d 100644 --- a/analyzer/decisions/nilslices/nilslices.go +++ b/analyzer/decisions/nilslices/nilslices.go @@ -54,9 +54,11 @@ func run(pass *analysis.Pass) (any, error) { if err != nil { return nil, err } + var opts []reporter.Option if c != nil { disable = c.IsDisabled(name) includeGenerated = c.AnalyzersSettings.Nilslices.IncludeGenerated + opts = append(opts, reporter.ExcludeFiles(c.ConfigDir, c.ExcludeFiles)) } if disable { return nil, nil @@ -72,7 +74,6 @@ func run(pass *analysis.Pass) (any, error) { (*ast.BinaryExpr)(nil), } - var opts []reporter.Option if includeGenerated { opts = append(opts, reporter.IncludeGenerated()) } diff --git a/analyzer/decisions/pkgnames/pkgnames.go b/analyzer/decisions/pkgnames/pkgnames.go index 8d37404..63bf92c 100644 --- a/analyzer/decisions/pkgnames/pkgnames.go +++ b/analyzer/decisions/pkgnames/pkgnames.go @@ -60,9 +60,11 @@ func run(pass *analysis.Pass) (any, error) { if err != nil { return nil, err } + var opts []reporter.Option if c != nil { disable = c.IsDisabled(name) includeGenerated = c.AnalyzersSettings.Pkgnames.IncludeGenerated + opts = append(opts, reporter.ExcludeFiles(c.ConfigDir, c.ExcludeFiles)) } if disable { return nil, nil @@ -77,7 +79,6 @@ func run(pass *analysis.Pass) (any, error) { (*ast.ImportSpec)(nil), } - var opts []reporter.Option if includeGenerated { opts = append(opts, reporter.IncludeGenerated()) } diff --git a/analyzer/decisions/recvnames/recvnames.go b/analyzer/decisions/recvnames/recvnames.go index 763d2d2..e7469f0 100644 --- a/analyzer/decisions/recvnames/recvnames.go +++ b/analyzer/decisions/recvnames/recvnames.go @@ -55,10 +55,12 @@ func run(pass *analysis.Pass) (any, error) { if err != nil { return nil, err } + var opts []reporter.Option if c != nil { disable = c.IsDisabled(name) includeGenerated = c.AnalyzersSettings.Recvnames.IncludeGenerated max = c.AnalyzersSettings.Recvnames.Max + opts = append(opts, reporter.ExcludeFiles(c.ConfigDir, c.ExcludeFiles)) } if disable { @@ -73,7 +75,6 @@ func run(pass *analysis.Pass) (any, error) { (*ast.FuncDecl)(nil), } - var opts []reporter.Option if includeGenerated { opts = append(opts, reporter.IncludeGenerated()) } diff --git a/analyzer/decisions/recvtype/recvtype.go b/analyzer/decisions/recvtype/recvtype.go index e5d6d52..493303a 100644 --- a/analyzer/decisions/recvtype/recvtype.go +++ b/analyzer/decisions/recvtype/recvtype.go @@ -53,9 +53,11 @@ func run(pass *analysis.Pass) (any, error) { if err != nil { return nil, err } + var opts []reporter.Option if c != nil { disable = c.IsDisabled(name) includeGenerated = c.AnalyzersSettings.Recvtype.IncludeGenerated + opts = append(opts, reporter.ExcludeFiles(c.ConfigDir, c.ExcludeFiles)) } if disable { @@ -70,7 +72,6 @@ func run(pass *analysis.Pass) (any, error) { (*ast.FuncDecl)(nil), } - var opts []reporter.Option if includeGenerated { opts = append(opts, reporter.IncludeGenerated()) } diff --git a/analyzer/decisions/repetition/repetition.go b/analyzer/decisions/repetition/repetition.go index 3cb8420..f0366bb 100644 --- a/analyzer/decisions/repetition/repetition.go +++ b/analyzer/decisions/repetition/repetition.go @@ -59,10 +59,12 @@ func run(pass *analysis.Pass) (any, error) { return nil, err } words := strings.Split(exclude, ",") + var opts []reporter.Option if c != nil { disable = c.IsDisabled(name) includeGenerated = c.AnalyzersSettings.Recvnames.IncludeGenerated words = c.AnalyzersSettings.Repetition.Exclude + opts = append(opts, reporter.ExcludeFiles(c.ConfigDir, c.ExcludeFiles)) } if disable { @@ -80,7 +82,6 @@ func run(pass *analysis.Pass) (any, error) { (*ast.FuncDecl)(nil), } - var opts []reporter.Option if includeGenerated { opts = append(opts, reporter.IncludeGenerated()) } diff --git a/analyzer/decisions/typealiases/typealiases.go b/analyzer/decisions/typealiases/typealiases.go index 16a4626..c86c5e5 100644 --- a/analyzer/decisions/typealiases/typealiases.go +++ b/analyzer/decisions/typealiases/typealiases.go @@ -55,10 +55,12 @@ func run(pass *analysis.Pass) (any, error) { return nil, err } words := strings.Split(exclude, ",") + var opts []reporter.Option if c != nil { disable = c.IsDisabled(name) words = c.AnalyzersSettings.Typealiases.Exclude includeGenerated = c.AnalyzersSettings.Typealiases.IncludeGenerated + opts = append(opts, reporter.ExcludeFiles(c.ConfigDir, c.ExcludeFiles)) } if disable { return nil, nil @@ -72,7 +74,6 @@ func run(pass *analysis.Pass) (any, error) { (*ast.TypeSpec)(nil), } - var opts []reporter.Option if includeGenerated { opts = append(opts, reporter.IncludeGenerated()) } diff --git a/analyzer/decisions/underscores/underscores.go b/analyzer/decisions/underscores/underscores.go index 7d566a4..9c37c64 100644 --- a/analyzer/decisions/underscores/underscores.go +++ b/analyzer/decisions/underscores/underscores.go @@ -57,10 +57,12 @@ func run(pass *analysis.Pass) (any, error) { return nil, err } words := strings.Split(exclude, ",") + var opts []reporter.Option if c != nil { disable = c.IsDisabled(name) words = c.AnalyzersSettings.Underscores.Exclude includeGenerated = c.AnalyzersSettings.Underscores.IncludeGenerated + opts = append(opts, reporter.ExcludeFiles(c.ConfigDir, c.ExcludeFiles)) } if disable { return nil, nil @@ -81,7 +83,6 @@ func run(pass *analysis.Pass) (any, error) { (*ast.RangeStmt)(nil), } - var opts []reporter.Option if includeGenerated { opts = append(opts, reporter.IncludeGenerated()) } diff --git a/analyzer/decisions/useany/useany.go b/analyzer/decisions/useany/useany.go index 1a13084..b031baf 100644 --- a/analyzer/decisions/useany/useany.go +++ b/analyzer/decisions/useany/useany.go @@ -52,9 +52,11 @@ func run(pass *analysis.Pass) (any, error) { if err != nil { return nil, err } + var opts []reporter.Option if c != nil { disable = c.IsDisabled(name) includeGenerated = c.AnalyzersSettings.Useany.IncludeGenerated + opts = append(opts, reporter.ExcludeFiles(c.ConfigDir, c.ExcludeFiles)) } if disable { return nil, nil @@ -70,7 +72,6 @@ func run(pass *analysis.Pass) (any, error) { (*ast.CompositeLit)(nil), } - var opts []reporter.Option if includeGenerated { opts = append(opts, reporter.IncludeGenerated()) } diff --git a/analyzer/decisions/useq/useq.go b/analyzer/decisions/useq/useq.go index 9f40105..ab15636 100644 --- a/analyzer/decisions/useq/useq.go +++ b/analyzer/decisions/useq/useq.go @@ -57,9 +57,11 @@ func run(pass *analysis.Pass) (any, error) { if err != nil { return nil, err } + var opts []reporter.Option if c != nil { disable = c.IsDisabled(name) includeGenerated = c.AnalyzersSettings.Useq.IncludeGenerated + opts = append(opts, reporter.ExcludeFiles(c.ConfigDir, c.ExcludeFiles)) } if disable { return nil, nil @@ -73,7 +75,6 @@ func run(pass *analysis.Pass) (any, error) { (*ast.CallExpr)(nil), } - var opts []reporter.Option if includeGenerated { opts = append(opts, reporter.IncludeGenerated()) } diff --git a/analyzer/decisions/varnames/varnames.go b/analyzer/decisions/varnames/varnames.go index 9d0970f..b1e2ba9 100644 --- a/analyzer/decisions/varnames/varnames.go +++ b/analyzer/decisions/varnames/varnames.go @@ -70,6 +70,7 @@ func run(pass *analysis.Pass) (any, error) { return nil, err } words := strings.Split(exclude, ",") + var opts []reporter.Option if c != nil { disable = c.IsDisabled(name) words = c.AnalyzersSettings.Varnames.Exclude @@ -81,6 +82,7 @@ func run(pass *analysis.Pass) (any, error) { largeScopeMax = c.AnalyzersSettings.Varnames.LargeScopeMax largeVarnameMax = c.AnalyzersSettings.Varnames.LargeVarnameMax veryLargeVarnameMax = c.AnalyzersSettings.Varnames.VeryLargeVarnameMax + opts = append(opts, reporter.ExcludeFiles(c.ConfigDir, c.ExcludeFiles)) } if disable { return nil, nil @@ -101,7 +103,6 @@ func run(pass *analysis.Pass) (any, error) { (*ast.RangeStmt)(nil), } - var opts []reporter.Option if includeGenerated { opts = append(opts, reporter.IncludeGenerated()) } diff --git a/analyzer/effective/ifacenames/ifacenames.go b/analyzer/effective/ifacenames/ifacenames.go index 16f26d0..c346391 100644 --- a/analyzer/effective/ifacenames/ifacenames.go +++ b/analyzer/effective/ifacenames/ifacenames.go @@ -56,10 +56,12 @@ func run(pass *analysis.Pass) (any, error) { if err != nil { return nil, err } + var opts []reporter.Option if c != nil { disable = c.IsDisabled(name) includeGenerated = c.AnalyzersSettings.Ifacenames.IncludeGenerated all = c.AnalyzersSettings.Ifacenames.All + opts = append(opts, reporter.ExcludeFiles(c.ConfigDir, c.ExcludeFiles)) } if disable { return nil, nil @@ -75,7 +77,6 @@ func run(pass *analysis.Pass) (any, error) { } var ii *ast.Ident - var opts []reporter.Option if includeGenerated { opts = append(opts, reporter.IncludeGenerated()) } diff --git a/analyzer/guide/mixedcaps/mixedcaps.go b/analyzer/guide/mixedcaps/mixedcaps.go index 4b33495..b7a6c43 100644 --- a/analyzer/guide/mixedcaps/mixedcaps.go +++ b/analyzer/guide/mixedcaps/mixedcaps.go @@ -57,10 +57,12 @@ func run(pass *analysis.Pass) (any, error) { return nil, err } words := strings.Split(exclude, ",") + var opts []reporter.Option if c != nil { disable = c.IsDisabled(name) words = c.AnalyzersSettings.Mixedcaps.Exclude includeGenerated = c.AnalyzersSettings.Mixedcaps.IncludeGenerated + opts = append(opts, reporter.ExcludeFiles(c.ConfigDir, c.ExcludeFiles)) } if disable { return nil, nil @@ -81,7 +83,6 @@ func run(pass *analysis.Pass) (any, error) { (*ast.RangeStmt)(nil), } - var opts []reporter.Option if includeGenerated { opts = append(opts, reporter.IncludeGenerated()) } diff --git a/config/config.go b/config/config.go index 664c855..04b613e 100644 --- a/config/config.go +++ b/config/config.go @@ -20,6 +20,8 @@ const ( type Config struct { Analyzers Analyzers `yaml:"analyzers"` AnalyzersSettings AnalyzersSettings `yaml:"analyzers-settings"` + ExcludeFiles []string `yaml:"exclude-files"` + ConfigDir string `yaml:"-"` loaded bool err error } diff --git a/config/loader.go b/config/loader.go index 0045177..e561aea 100644 --- a/config/loader.go +++ b/config/loader.go @@ -3,6 +3,7 @@ package config import ( "fmt" "os" + "path/filepath" "reflect" "strings" @@ -44,6 +45,8 @@ func run(pass *analysis.Pass) (any, error) { c.err = fmt.Errorf("failed to decode config file: %w", err) return c, nil } + c.ConfigDir = filepath.Dir(configPath) + // Set default value if c.AnalyzersSettings.Recvnames.Max == 0 { c.AnalyzersSettings.Recvnames.Max = DefaultReceiverNameMax diff --git a/go.mod b/go.mod index b9372de..5dfd420 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,9 @@ module github.com/k1LoW/gostyle -go 1.22.1 +go 1.22.2 require ( + github.com/bmatcuk/doublestar/v4 v4.6.1 github.com/fatih/camelcase v1.0.0 github.com/gostaticanalysis/comment v1.4.2 github.com/gostaticanalysis/testutil v0.4.0 diff --git a/go.sum b/go.sum index bf86458..0a5b70c 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/bmatcuk/doublestar/v4 v4.6.1 h1:FH9SifrbvJhnlQpztAx++wlkk70QBf0iBWDwNy7PA4I= +github.com/bmatcuk/doublestar/v4 v4.6.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/fatih/camelcase v1.0.0 h1:hxNvNX/xYBp0ovncs8WyWZrOrpBNub/JfaMvbURyft8= github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= diff --git a/reporter/reporter.go b/reporter/reporter.go index 0a4821a..4726124 100644 --- a/reporter/reporter.go +++ b/reporter/reporter.go @@ -3,9 +3,11 @@ package reporter import ( "fmt" "go/token" + "path/filepath" "regexp" "strings" + "github.com/bmatcuk/doublestar/v4" "github.com/gostaticanalysis/comment" "github.com/gostaticanalysis/comment/passes/commentmap" "golang.org/x/tools/go/analysis" @@ -33,6 +35,8 @@ type Reporter struct { disableLintIgnore bool disableNoLint bool includeGenerated bool + configDir string + excludeFiles []string } type report struct { @@ -78,6 +82,14 @@ func IncludeGenerated() Option { } } +// ExcludeFiles excludes files from the report. +func ExcludeFiles(configDir string, files []string) Option { + return func(r *Reporter) { + r.configDir = configDir + r.excludeFiles = append(r.excludeFiles, files...) + } +} + // New returns a new Reporter. func New(name string, pass *analysis.Pass, opts ...Option) (*Reporter, error) { cm, ok := pass.ResultOf[commentmap.Analyzer].(comment.Maps) @@ -94,6 +106,15 @@ func New(name string, pass *analysis.Pass, opts ...Option) (*Reporter, error) { for _, opt := range opts { opt(r) } + var excludeFiles []string + if len(r.excludeFiles) > 0 { + for _, f := range r.excludeFiles { + p := filepath.Join(r.configDir, f) + excludeFiles = append(excludeFiles, p) + } + } + r.excludeFiles = excludeFiles + return r, nil } @@ -121,7 +142,19 @@ func (r *Reporter) ignoreReport(pos token.Pos) bool { if !pos.IsValid() { return false } + f1 := r.pass.Fset.File(pos) + for _, e := range r.excludeFiles { + match, err := doublestar.PathMatch(e, f1.Name()) + if err != nil { + fmt.Println(err) + continue + } + if match { + return true + } + } + for i := range r.cm { for n, cgs := range r.cm[i] { f2 := r.pass.Fset.File(n.Pos())