From 7148f28b27544bb5fbebda105901339ff5323702 Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Sun, 21 Jan 2024 14:42:12 +0200 Subject: [PATCH] dev: option to disable nolint directive check closes https://github.com/butuzov/ireturn/issues/78 --- analyzer/analyzer.go | 20 ++++++++++++++------ analyzer/analyzer_test.go | 17 +++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/analyzer/analyzer.go b/analyzer/analyzer.go index 21e5897..bd2650e 100644 --- a/analyzer/analyzer.go +++ b/analyzer/analyzer.go @@ -22,10 +22,11 @@ type validator interface { } type analyzer struct { - once sync.Once - mu sync.RWMutex - handler validator - err error + once sync.Once + mu sync.RWMutex + handler validator + err error + diabledNolint bool found []analysis.Diagnostic } @@ -61,8 +62,7 @@ func (a *analyzer) run(pass *analysis.Pass) (interface{}, error) { } // 003. Is it allowed to be checked? - // TODO(butuzov): add inline comment - if hasDisallowDirective(f.Doc) { + if !a.diabledNolint && hasDisallowDirective(f.Doc) { return } @@ -112,6 +112,13 @@ func (a *analyzer) readConfiguration(fs *flag.FlagSet) { return } + // First: checking nonolint directive + val := fs.Lookup("nonolint") + if val != nil { + a.diabledNolint = fs.Lookup("nonolint").Value.String() == "true" + } + + // Second: validators implementation next if validatorImpl, ok := cnf.(validator); ok { a.handler = validatorImpl return @@ -136,6 +143,7 @@ func flags() flag.FlagSet { set := flag.NewFlagSet("", flag.PanicOnError) set.String("allow", "", "accept-list of the comma-separated interfaces") set.String("reject", "", "reject-list of the comma-separated interfaces") + set.Bool("nonolint", false, "disable nolint checks") return *set } diff --git a/analyzer/analyzer_test.go b/analyzer/analyzer_test.go index 5cf658d..386c767 100644 --- a/analyzer/analyzer_test.go +++ b/analyzer/analyzer_test.go @@ -101,6 +101,23 @@ func TestAll(t *testing.T) { }, }) + tests = append(tests, testCase{ + name: "Disallow Directives 2", + mask: []string{"disallow_directive_ok.go", "go.*"}, + meta: map[string]string{ + "reject": types.NameEmpty, + "nonolint": "true", + }, + want: []string{ + "dissAllowDirective1 returns interface (interface{})", + "dissAllowDirective2 returns interface (interface{})", + "dissAllowDirective3 returns interface (interface{})", + "dissAllowDirective4 returns interface (interface{})", + "dissAllowDirective5 returns interface (interface{})", + "dissAllowDirective6 returns interface (interface{})", + }, + }) + tests = append(tests, testCase{ name: "Error/allow", mask: []string{"errors.go", "go.*"},