Skip to content

perf: nolint simplifications and improvements #5847

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 6 additions & 21 deletions pkg/golinters/nolintlint/internal/nolintlint.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Needs uint

const commentMark = "//"

var commentPattern = regexp.MustCompile(`^//\s*(nolint)(:\s*[\w-]+\s*(?:,\s*[\w-]+\s*)*)?\b`)
var commentPattern = regexp.MustCompile(`^(//(\s*)nolint)(?::\s*[\w-]+\s*(?:,\s*[\w-]+\s*)*)?\b`)

// matches a complete nolint directive
var fullDirectivePattern = regexp.MustCompile(`^//\s*nolint(?::(\s*[\w-]+\s*(?:,\s*[\w-]+\s*)*))?\s*(//.*)?\s*\n?$`)
Expand All @@ -49,10 +49,7 @@ func NewLinter(needs Needs, excludes []string) (*Linter, error) {
}, nil
}

var (
leadingSpacePattern = regexp.MustCompile(`^//(\s*)`)
trailingBlankExplanation = regexp.MustCompile(`\s*(//\s*)?$`)
)
var trailingBlankExplanation = regexp.MustCompile(`\s*(//\s*)?$`)

//nolint:funlen,gocyclo // the function is going to be refactored in the future
func (l Linter) Run(pass *analysis.Pass) ([]goanalysis.Issue, error) {
Expand All @@ -61,25 +58,13 @@ func (l Linter) Run(pass *analysis.Pass) ([]goanalysis.Issue, error) {
for _, file := range pass.Files {
for _, c := range file.Comments {
for _, comment := range c.List {
if !commentPattern.MatchString(comment.Text) {
m := commentPattern.FindStringSubmatch(comment.Text)
if m == nil {
continue
}

// check for a space between the "//" and the directive
leadingSpaceMatches := leadingSpacePattern.FindStringSubmatch(comment.Text)

var leadingSpace string
if len(leadingSpaceMatches) > 0 {
leadingSpace = leadingSpaceMatches[1]
}

directiveWithOptionalLeadingSpace := commentMark
if leadingSpace != "" {
directiveWithOptionalLeadingSpace += " "
}

split := strings.Split(strings.SplitN(comment.Text, ":", 2)[0], commentMark)
directiveWithOptionalLeadingSpace += strings.TrimSpace(split[1])
directiveWithOptionalLeadingSpace := m[1]
leadingSpace := m[2]

pos := pass.Fset.Position(comment.Pos())
end := pass.Fset.Position(comment.End())
Expand Down
2 changes: 1 addition & 1 deletion pkg/result/processors/nolint_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func (p *NolintFilter) extractInlineRangeFromComment(text string, g ast.Node, fs

// ignore specific linters
var linters []string
text = strings.Split(text, "//")[0] // allow another comment after this comment
text, _, _ = strings.Cut(text, "//") // allow another comment after this comment
linterItems := strings.Split(strings.TrimPrefix(text, "nolint:"), ",")
for _, item := range linterItems {
linterName := strings.ToLower(strings.TrimSpace(item))
Expand Down
Loading