-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(deps): bump github.com/ultraware/funlen from 0.1.0 to 0.2.0 (#5231
- Loading branch information
1 parent
c751e5c
commit 966259a
Showing
3 changed files
with
19 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,33 @@ | ||
package funlen | ||
|
||
import ( | ||
"go/token" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/ultraware/funlen" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
"github.com/golangci/golangci-lint/pkg/goanalysis" | ||
"github.com/golangci/golangci-lint/pkg/lint/linter" | ||
"github.com/golangci/golangci-lint/pkg/result" | ||
) | ||
|
||
const linterName = "funlen" | ||
type Config struct { | ||
lineLimit int | ||
stmtLimit int | ||
ignoreComments bool | ||
} | ||
|
||
func New(settings *config.FunlenSettings) *goanalysis.Linter { | ||
var mu sync.Mutex | ||
var resIssues []goanalysis.Issue | ||
|
||
analyzer := &analysis.Analyzer{ | ||
Name: linterName, | ||
Doc: goanalysis.TheOnlyanalyzerDoc, | ||
Run: func(pass *analysis.Pass) (any, error) { | ||
issues := runFunlen(pass, settings) | ||
|
||
if len(issues) == 0 { | ||
return nil, nil | ||
} | ||
|
||
mu.Lock() | ||
resIssues = append(resIssues, issues...) | ||
mu.Unlock() | ||
|
||
return nil, nil | ||
}, | ||
cfg := Config{} | ||
if settings != nil { | ||
cfg.lineLimit = settings.Lines | ||
cfg.stmtLimit = settings.Statements | ||
cfg.ignoreComments = !settings.IgnoreComments | ||
} | ||
|
||
a := funlen.NewAnalyzer(cfg.lineLimit, cfg.stmtLimit, cfg.ignoreComments) | ||
|
||
return goanalysis.NewLinter( | ||
linterName, | ||
"Tool for detection of long functions", | ||
[]*analysis.Analyzer{analyzer}, | ||
a.Name, | ||
a.Doc, | ||
[]*analysis.Analyzer{a}, | ||
nil, | ||
).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { | ||
return resIssues | ||
}).WithLoadMode(goanalysis.LoadModeSyntax) | ||
} | ||
|
||
func runFunlen(pass *analysis.Pass, settings *config.FunlenSettings) []goanalysis.Issue { | ||
var lintIssues []funlen.Message | ||
for _, file := range pass.Files { | ||
fileIssues := funlen.Run(file, pass.Fset, settings.Lines, settings.Statements, settings.IgnoreComments) | ||
lintIssues = append(lintIssues, fileIssues...) | ||
} | ||
|
||
if len(lintIssues) == 0 { | ||
return nil | ||
} | ||
|
||
issues := make([]goanalysis.Issue, len(lintIssues)) | ||
for k, i := range lintIssues { | ||
issues[k] = goanalysis.NewIssue(&result.Issue{ | ||
Pos: token.Position{ | ||
Filename: i.Pos.Filename, | ||
Line: i.Pos.Line, | ||
}, | ||
Text: strings.TrimRight(i.Message, "\n"), | ||
FromLinter: linterName, | ||
}, pass) | ||
} | ||
|
||
return issues | ||
).WithLoadMode(goanalysis.LoadModeSyntax) | ||
} |