-
-
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.
* add go-header linter * apply review comments: add goheader example into .golangci.example.yml * apply review comments: correctly handle multiline comments
- Loading branch information
1 parent
b22e3f1
commit 01b566a
Showing
9 changed files
with
139 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
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
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,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 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
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,6 @@ | ||
linters-settings: | ||
goheader: | ||
template: MY {{title}} | ||
values: | ||
const: | ||
title: TITLE. |
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,4 @@ | ||
/*MY TITLE!*/ // ERROR "Expected:TITLE., Actual: TITLE!" | ||
//args: -Egoheader | ||
//config_path: testdata/configs/go-header.yml | ||
package testdata |