-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add go-header linter #1181
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
Merged
Merged
Add go-header linter #1181
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
adb1a45
add go-header linter
denis-tingaikin ed39ee9
fix go mod issue
denis-tingaikin 6850237
fix some issues
denis-tingaikin 6e8a56e
checkout README.md
denis-tingaikin e61192d
go mod tidy
denis-tingaikin d52fb3a
apply review comments: add goheader example into .golangci.example.yml
denis-tingaikin 246fdaf
apply review comments: better to use more conservative solution to su…
denis-tingaikin 28fc9e0
apply review comments: correctly handle multiline comments
denis-tingaikin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,78 @@ | ||
package golinters | ||
|
||
import ( | ||
"go/token" | ||
"sync" | ||
|
||
goheader "github.com/denis-tingajkin/go-header" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
"github.com/golangci/golangci-lint/pkg/lint/linter" | ||
"github.com/golangci/golangci-lint/pkg/result" | ||
) | ||
|
||
const goHeaderName = "goheader" | ||
|
||
func NewGoHeader() *goanalysis.Linter { | ||
var mu sync.Mutex | ||
var issues []goanalysis.Issue | ||
|
||
analyzer := &analysis.Analyzer{ | ||
Name: goHeaderName, | ||
Doc: goanalysis.TheOnlyanalyzerDoc, | ||
} | ||
return goanalysis.NewLinter( | ||
goHeaderName, | ||
"Checks is file header matches to pattern", | ||
[]*analysis.Analyzer{analyzer}, | ||
nil, | ||
).WithContextSetter(func(lintCtx *linter.Context) { | ||
cfg := lintCtx.Cfg.LintersSettings.Goheader | ||
c := &goheader.Configuration{ | ||
Values: cfg.Values, | ||
Template: cfg.Template, | ||
TemplatePath: cfg.TemplatePath, | ||
} | ||
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { | ||
if c.TemplatePath == "" && c.Template == "" { | ||
// User did not pass template, so then do not run go-header linter | ||
return nil, nil | ||
} | ||
template, err := c.GetTemplate() | ||
if err != nil { | ||
return nil, err | ||
} | ||
values, err := c.GetValues() | ||
if err != nil { | ||
return nil, err | ||
} | ||
a := goheader.New(goheader.WithTemplate(template), goheader.WithValues(values)) | ||
var res []goanalysis.Issue | ||
for _, file := range pass.Files { | ||
i := a.Analyze(file) | ||
issue := result.Issue{ | ||
Pos: token.Position{ | ||
Line: i.Location().Line + 1, | ||
Column: i.Location().Position, | ||
Filename: pass.Fset.Position(file.Pos()).Filename, | ||
}, | ||
Text: i.Message(), | ||
FromLinter: goHeaderName, | ||
} | ||
res = append(res, goanalysis.NewIssue(&issue, pass)) | ||
} | ||
if len(res) == 0 { | ||
return nil, nil | ||
} | ||
|
||
mu.Lock() | ||
issues = append(issues, res...) | ||
mu.Unlock() | ||
|
||
return nil, nil | ||
} | ||
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { | ||
return issues | ||
}).WithLoadMode(goanalysis.LoadModeSyntax) | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,6 @@ | ||
linters-settings: | ||
goheader: | ||
template: MY {{title}} | ||
values: | ||
const: | ||
title: TITLE. |
This file contains hidden or 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,4 @@ | ||
/*MY TITLE!*/ // ERROR "Expected:TITLE., Actual: TITLE!" | ||
//args: -Egoheader | ||
//config_path: testdata/configs/go-header.yml | ||
package testdata |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.