From 235a9b7ee461cdb9886be9336a5ebea29c8ce5f8 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 7 Jan 2022 01:30:18 +0300 Subject: [PATCH 1/4] optimize commentFormattingChecker --- checkers/commentFormatting_checker.go | 63 ++++++++++++++++++++------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/checkers/commentFormatting_checker.go b/checkers/commentFormatting_checker.go index 74eff3b09..82393980e 100644 --- a/checkers/commentFormatting_checker.go +++ b/checkers/commentFormatting_checker.go @@ -2,7 +2,6 @@ package checkers import ( "go/ast" - "regexp" "strings" "unicode" "unicode/utf8" @@ -21,19 +20,34 @@ func init() { collection.AddChecker(&info, func(ctx *linter.CheckerContext) (linter.FileWalker, error) { parts := []string{ - `^//go:generate .*$`, // e.g.: go:generate value - `^//[\w-]+:.*$`, // e.g.: key: value - `^//nolint\b`, // e.g.: nolint - `^//line /.*:\d+`, // e.g.: line /path/to/file:123 - `^//export \w+$`, // e.g.: export Foo - `^//[/+#-]+.*$`, // e.g.: vertical breaker ///////////// - `^//noinspection `, // e.g.: noinspection ALL, some GoLand and friends versions + //`^//go:generate .*$`, // e.g.: go:generate value + `^//[\w-]+:.*$`, // e.g.: key: value + //`^//nolint\b`, // e.g.: nolint + //`^//line /.*:\d+`, // e.g.: line /path/to/file:123 + //`^//export \w+$`, // e.g.: export Foo + //`^//[/+#-]+.*$`, // e.g.: vertical breaker ///////////// + //`^//noinspection `, // e.g.: noinspection ALL, some GoLand and friends versions + } + + equalPatterns := []string{ + "//nolint", + } + parts = []string{ + "//go:generate ", + "//line /", + "//nolint ", + "//noinspection ", + "//export ", + "///", + "//+", + "//#", + "//-", + "//!", } - pat := "(?m)" + strings.Join(parts, "|") - pragmaRE := regexp.MustCompile(pat) return astwalk.WalkerForComment(&commentFormattingChecker{ - ctx: ctx, - pragmaRE: pragmaRE, + ctx: ctx, + partPatterns: parts, + equalPatterns: equalPatterns, }), nil }) } @@ -42,19 +56,36 @@ type commentFormattingChecker struct { astwalk.WalkHandler ctx *linter.CheckerContext - pragmaRE *regexp.Regexp + partPatterns []string + equalPatterns []string } func (c *commentFormattingChecker) VisitComment(cg *ast.CommentGroup) { if strings.HasPrefix(cg.List[0].Text, "/*") { return } + +outerLoop: for _, comment := range cg.List { - if len(comment.Text) <= len("// ") { + l := len(comment.Text) + if l <= len("// ") { continue } - if c.pragmaRE.MatchString(comment.Text) { - continue + + for _, p := range c.partPatterns { + if l < len(p) { + continue + } + + if strings.EqualFold(comment.Text[:len(p)], p) { + continue outerLoop + } + } + + for _, p := range c.equalPatterns { + if strings.EqualFold(comment.Text, p) { + continue outerLoop + } } // Make a decision based on a first comment text rune. From f0949cf97f39326b2ef492608b4299d46fd89c7f Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 7 Jan 2022 16:48:43 +0300 Subject: [PATCH 2/4] refactor patterns for commentFormatting --- checkers/commentFormatting_checker.go | 31 +++++++++++++++------------ 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/checkers/commentFormatting_checker.go b/checkers/commentFormatting_checker.go index 82393980e..c8203e5c5 100644 --- a/checkers/commentFormatting_checker.go +++ b/checkers/commentFormatting_checker.go @@ -8,6 +8,7 @@ import ( "github.com/go-critic/go-critic/checkers/internal/astwalk" "github.com/go-critic/go-critic/framework/linter" + "regexp" ) func init() { @@ -19,20 +20,14 @@ func init() { info.After = `// This is a comment` collection.AddChecker(&info, func(ctx *linter.CheckerContext) (linter.FileWalker, error) { - parts := []string{ - //`^//go:generate .*$`, // e.g.: go:generate value - `^//[\w-]+:.*$`, // e.g.: key: value - //`^//nolint\b`, // e.g.: nolint - //`^//line /.*:\d+`, // e.g.: line /path/to/file:123 - //`^//export \w+$`, // e.g.: export Foo - //`^//[/+#-]+.*$`, // e.g.: vertical breaker ///////////// - //`^//noinspection `, // e.g.: noinspection ALL, some GoLand and friends versions + regexpPatterns := []*regexp.Regexp{ + regexp.MustCompile(`^//[\w-]+:.*$`), // e.g.: key: value } equalPatterns := []string{ "//nolint", } - parts = []string{ + parts := []string{ "//go:generate ", "//line /", "//nolint ", @@ -45,9 +40,10 @@ func init() { "//!", } return astwalk.WalkerForComment(&commentFormattingChecker{ - ctx: ctx, - partPatterns: parts, - equalPatterns: equalPatterns, + ctx: ctx, + partPatterns: parts, + equalPatterns: equalPatterns, + regexpPatterns: regexpPatterns, }), nil }) } @@ -56,8 +52,9 @@ type commentFormattingChecker struct { astwalk.WalkHandler ctx *linter.CheckerContext - partPatterns []string - equalPatterns []string + partPatterns []string + equalPatterns []string + regexpPatterns []*regexp.Regexp } func (c *commentFormattingChecker) VisitComment(cg *ast.CommentGroup) { @@ -88,6 +85,12 @@ outerLoop: } } + for _, p := range c.regexpPatterns { + if p.MatchString(comment.Text) { + continue outerLoop + } + } + // Make a decision based on a first comment text rune. r, _ := utf8.DecodeRuneInString(comment.Text[len("//"):]) if !c.specialChar(r) && !unicode.IsSpace(r) { From 687862bb6e33c572ca5cccaf8e7c940c94f6462f Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 7 Jan 2022 16:52:15 +0300 Subject: [PATCH 3/4] format code --- checkers/commentFormatting_checker.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/checkers/commentFormatting_checker.go b/checkers/commentFormatting_checker.go index c8203e5c5..708db0e05 100644 --- a/checkers/commentFormatting_checker.go +++ b/checkers/commentFormatting_checker.go @@ -2,13 +2,13 @@ package checkers import ( "go/ast" + "regexp" "strings" "unicode" "unicode/utf8" "github.com/go-critic/go-critic/checkers/internal/astwalk" "github.com/go-critic/go-critic/framework/linter" - "regexp" ) func init() { @@ -64,13 +64,13 @@ func (c *commentFormattingChecker) VisitComment(cg *ast.CommentGroup) { outerLoop: for _, comment := range cg.List { - l := len(comment.Text) - if l <= len("// ") { + commentLen := len(comment.Text) + if commentLen <= len("// ") { continue } for _, p := range c.partPatterns { - if l < len(p) { + if commentLen < len(p) { continue } From 80269edc34223ba9cb31335de1b95b8f5456617b Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 7 Jan 2022 16:54:43 +0300 Subject: [PATCH 4/4] return comments --- checkers/commentFormatting_checker.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/checkers/commentFormatting_checker.go b/checkers/commentFormatting_checker.go index 708db0e05..c849bdedd 100644 --- a/checkers/commentFormatting_checker.go +++ b/checkers/commentFormatting_checker.go @@ -23,22 +23,22 @@ func init() { regexpPatterns := []*regexp.Regexp{ regexp.MustCompile(`^//[\w-]+:.*$`), // e.g.: key: value } - equalPatterns := []string{ "//nolint", } parts := []string{ - "//go:generate ", - "//line /", - "//nolint ", - "//noinspection ", - "//export ", - "///", + "//go:generate ", // e.g.: go:generate value + "//line /", // e.g.: line /path/to/file:123 + "//nolint ", // e.g.: nolint + "//noinspection ", // e.g.: noinspection ALL, some GoLand and friends versions + "//export ", // e.g.: export Foo + "///", // e.g.: vertical breaker ///////////// "//+", "//#", "//-", "//!", } + return astwalk.WalkerForComment(&commentFormattingChecker{ ctx: ctx, partPatterns: parts,