Skip to content

Commit

Permalink
refactor: one function by directive
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Dec 1, 2024
1 parent 0c3163c commit 42316e5
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions gomoddirectives.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ func Analyze(opts Options) ([]Result, error) {

// AnalyzeFile analyzes a mod file.
func AnalyzeFile(file *modfile.File, opts Options) []Result {
results := checkRetractDirectives(file, opts)
results = append(results, checkExcludeDirectives(file, opts)...)
results = append(results, checkToolDirectives(file, opts)...)
results = append(results, checkReplaceDirectives(file, opts)...)

return results
}

func checkRetractDirectives(file *modfile.File, opts Options) []Result {
var results []Result

if !opts.RetractAllowNoExplanation {
Expand All @@ -99,22 +108,40 @@ func AnalyzeFile(file *modfile.File, opts Options) []Result {
}
}

return results
}

func checkExcludeDirectives(file *modfile.File, opts Options) []Result {
var results []Result

if opts.ExcludeForbidden {
for _, e := range file.Exclude {
results = append(results, NewResult(file, e.Syntax, reasonExclude))
}
}

return results
}

func checkToolDirectives(file *modfile.File, opts Options) []Result {
var results []Result

if opts.ToolForbidden {
for _, e := range file.Tool {
results = append(results, NewResult(file, e.Syntax, reasonTool))
}
}

return results
}

func checkReplaceDirectives(file *modfile.File, opts Options) []Result {
var results []Result

uniqReplace := map[string]struct{}{}

for _, r := range file.Replace {
reason := check(opts, r)
reason := checkReplaceDirective(opts, r)
if reason != "" {
results = append(results, NewResult(file, r.Syntax, reason))
continue
Expand All @@ -135,7 +162,7 @@ func AnalyzeFile(file *modfile.File, opts Options) []Result {
return results
}

func check(o Options, r *modfile.Replace) string {
func checkReplaceDirective(o Options, r *modfile.Replace) string {
if isLocal(r) {
if o.ReplaceAllowLocal {
return ""
Expand Down

0 comments on commit 42316e5

Please sign in to comment.