-
-
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.
- Loading branch information
Showing
7 changed files
with
97 additions
and
1 deletion.
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
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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package golinters | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/ldez/gomoddirectives" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
"github.com/golangci/golangci-lint/pkg/lint/linter" | ||
"github.com/golangci/golangci-lint/pkg/result" | ||
) | ||
|
||
const goModDirectivesName = "gomoddirectives" | ||
|
||
// NewGoModDirectives returns a new gomoddirectives linter. | ||
func NewGoModDirectives(settings *config.GoModDirectivesSettings) *goanalysis.Linter { | ||
var issues []goanalysis.Issue | ||
var once sync.Once | ||
|
||
var opts gomoddirectives.Options | ||
if settings != nil { | ||
opts.ReplaceAllowLocal = settings.ReplaceLocal | ||
opts.ReplaceAllowList = settings.ReplaceAllowList | ||
opts.RetractAllowNoExplanation = settings.RetractAllowNoExplanation | ||
opts.ExcludeForbidden = settings.ExcludeForbidden | ||
} | ||
|
||
analyzer := &analysis.Analyzer{ | ||
Name: goanalysis.TheOnlyAnalyzerName, | ||
Doc: goanalysis.TheOnlyanalyzerDoc, | ||
} | ||
|
||
return goanalysis.NewLinter( | ||
goModDirectivesName, | ||
"Manage the use of 'replace', 'retract', and 'excludes' directives in go.mod.", | ||
[]*analysis.Analyzer{analyzer}, | ||
nil, | ||
).WithContextSetter(func(lintCtx *linter.Context) { | ||
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { | ||
once.Do(func() { | ||
results, err := gomoddirectives.Analyze(opts) | ||
if err != nil { | ||
lintCtx.Log.Warnf("running %s failed: %s: "+ | ||
"if you are not using go modules it is suggested to disable this linter", goModDirectivesName, err) | ||
return | ||
} | ||
|
||
for _, p := range results { | ||
issues = append(issues, goanalysis.NewIssue(&result.Issue{ | ||
FromLinter: goModDirectivesName, | ||
Pos: p.Start, | ||
Text: p.Reason, | ||
}, pass)) | ||
} | ||
}) | ||
|
||
return nil, nil | ||
} | ||
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { | ||
return issues | ||
}).WithLoadMode(goanalysis.LoadModeSyntax) | ||
} |
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
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