From 8ee7bbda6c6d5b8e7a87501a9da6bfd4ad82d333 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 31 Dec 2021 20:55:05 +0300 Subject: [PATCH 1/2] add ignore flag for user rules --- checkers/commentFormatting_checker.go | 1 + checkers/ruleguard_checker.go | 49 +++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/checkers/commentFormatting_checker.go b/checkers/commentFormatting_checker.go index 74eff3b09..fd459b357 100644 --- a/checkers/commentFormatting_checker.go +++ b/checkers/commentFormatting_checker.go @@ -78,4 +78,5 @@ func (c *commentFormattingChecker) specialChar(r rune) bool { func (c *commentFormattingChecker) warn(comment *ast.Comment) { c.ctx.Warn(comment, "put a space between `//` and comment text") + c.ctx.WarnFixable() } diff --git a/checkers/ruleguard_checker.go b/checkers/ruleguard_checker.go index d65669fdd..4dc378165 100644 --- a/checkers/ruleguard_checker.go +++ b/checkers/ruleguard_checker.go @@ -12,8 +12,9 @@ import ( "sort" "strings" - "github.com/go-critic/go-critic/framework/linter" "github.com/quasilyte/go-ruleguard/ruleguard" + + "github.com/go-critic/go-critic/framework/linter" ) func init() { @@ -41,6 +42,14 @@ If flag is set, the value must be a comma-separated list of error conditions. * 'import': rule refers to a package that cannot be loaded. * 'dsl': gorule file does not comply with the ruleguard DSL.`, }, + "enable": { + Value: "", + Usage: "comma-separated list of enabled groups or skip empty to enable everything", + }, + "disable": { + Value: "", + Usage: "comma-separated list of disabled groups or skip empty to enable everything", + }, } info.Summary = "Runs user-defined rules using ruleguard linter" info.Details = "Reads a rules file and turns them into go-critic checkers." @@ -124,13 +133,43 @@ func newRuleguardChecker(info *linter.CheckerInfo, ctx *linter.CheckerContext) ( fset := token.NewFileSet() filePatterns := strings.Split(rulesFlag, ",") + enabledGroups := make(map[string]bool) + disabledGroups := make(map[string]bool) + + for _, g := range strings.Split(info.Params.String("disabled"), ",") { + g = strings.TrimSpace(g) + disabledGroups[g] = true + } + flagEnable := info.Params.String("enabled") + if flagEnable != "" { + for _, g := range strings.Split(flagEnable, ",") { + g = strings.TrimSpace(g) + enabledGroups[g] = true + } + } ruleguardDebug := os.Getenv("GOCRITIC_RULEGUARD_DEBUG") != "" loadContext := &ruleguard.LoadContext{ Fset: fset, DebugImports: ruleguardDebug, - DebugPrint: func(s string) { - fmt.Println("debug:", s) + DebugPrint: debugPrint, + GroupFilter: func(g string) bool { + whyDisabled := "" + enabled := flagEnable == "" || enabledGroups[g] + switch { + case !enabled: + whyDisabled = "not enabled by -enabled flag" + case disabledGroups[g]: + whyDisabled = "disabled by -disable flag" + } + if ruleguardDebug { + if whyDisabled != "" { + debugPrint(fmt.Sprintf("(-) %s is %s", g, whyDisabled)) + } else { + debugPrint(fmt.Sprintf("(+) %s is enabled", g)) + } + } + return whyDisabled == "" }, } @@ -236,3 +275,7 @@ func runRuleguardEngine(ctx *linter.CheckerContext, f *ast.File, e *ruleguard.En } } } + +func debugPrint(s string) { + fmt.Println("debug:", s) +} From 6a2193c979f97e94ba20525ce03a01378da35bff Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 31 Dec 2021 21:33:04 +0300 Subject: [PATCH 2/2] fix typos --- checkers/boolExprSimplify_checker.go | 7 ++++--- checkers/commentFormatting_checker.go | 1 - checkers/ruleguard_checker.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/checkers/boolExprSimplify_checker.go b/checkers/boolExprSimplify_checker.go index 325fb56a3..8e931cc4b 100644 --- a/checkers/boolExprSimplify_checker.go +++ b/checkers/boolExprSimplify_checker.go @@ -6,15 +6,16 @@ import ( "go/token" "strconv" - "github.com/go-critic/go-critic/checkers/internal/astwalk" - "github.com/go-critic/go-critic/checkers/internal/lintutil" - "github.com/go-critic/go-critic/framework/linter" "github.com/go-toolsmith/astcast" "github.com/go-toolsmith/astcopy" "github.com/go-toolsmith/astequal" "github.com/go-toolsmith/astp" "github.com/go-toolsmith/typep" "golang.org/x/tools/go/ast/astutil" + + "github.com/go-critic/go-critic/checkers/internal/astwalk" + "github.com/go-critic/go-critic/checkers/internal/lintutil" + "github.com/go-critic/go-critic/framework/linter" ) func init() { diff --git a/checkers/commentFormatting_checker.go b/checkers/commentFormatting_checker.go index fd459b357..74eff3b09 100644 --- a/checkers/commentFormatting_checker.go +++ b/checkers/commentFormatting_checker.go @@ -78,5 +78,4 @@ func (c *commentFormattingChecker) specialChar(r rune) bool { func (c *commentFormattingChecker) warn(comment *ast.Comment) { c.ctx.Warn(comment, "put a space between `//` and comment text") - c.ctx.WarnFixable() } diff --git a/checkers/ruleguard_checker.go b/checkers/ruleguard_checker.go index 4dc378165..e164dede8 100644 --- a/checkers/ruleguard_checker.go +++ b/checkers/ruleguard_checker.go @@ -136,11 +136,11 @@ func newRuleguardChecker(info *linter.CheckerInfo, ctx *linter.CheckerContext) ( enabledGroups := make(map[string]bool) disabledGroups := make(map[string]bool) - for _, g := range strings.Split(info.Params.String("disabled"), ",") { + for _, g := range strings.Split(info.Params.String("disable"), ",") { g = strings.TrimSpace(g) disabledGroups[g] = true } - flagEnable := info.Params.String("enabled") + flagEnable := info.Params.String("enable") if flagEnable != "" { for _, g := range strings.Split(flagEnable, ",") { g = strings.TrimSpace(g)