-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add "revive" linter #1729
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 "revive" linter #1729
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,6 +117,7 @@ linters: | |
# - nestif | ||
# - prealloc | ||
# - testpackage | ||
# - revive | ||
# - wsl | ||
|
||
issues: | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,187 @@ | ||
package golinters | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"go/token" | ||
"io/ioutil" | ||
"strings" | ||
|
||
"github.com/mgechev/dots" | ||
|
||
"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" | ||
|
||
reviveConfig "github.com/mgechev/revive/config" | ||
"github.com/mgechev/revive/lint" | ||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
const ( | ||
reviveName = "revive" | ||
) | ||
|
||
// jsonObject defines a JSON object of an failure | ||
type jsonObject struct { | ||
Severity lint.Severity | ||
lint.Failure `json:",inline"` | ||
} | ||
|
||
// NewNewRevive returns a new Revive linter. | ||
func NewRevive(cfg *config.ReviveSettings) *goanalysis.Linter { | ||
var ( | ||
issues []goanalysis.Issue | ||
analyzer = &analysis.Analyzer{ | ||
Name: goanalysis.TheOnlyAnalyzerName, | ||
Doc: goanalysis.TheOnlyanalyzerDoc, | ||
} | ||
) | ||
|
||
return goanalysis.NewLinter( | ||
reviveName, | ||
"Fast, configurable, extensible, flexible, and beautiful linter for Go", | ||
[]*analysis.Analyzer{analyzer}, | ||
nil, | ||
).WithContextSetter(func(lintCtx *linter.Context) { | ||
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { | ||
var ( | ||
files = []string{} | ||
) | ||
sspaink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for _, file := range pass.Files { | ||
files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename) | ||
} | ||
|
||
conf, err := SetReviveConfig(cfg) | ||
sspaink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
formatter, err := reviveConfig.GetFormatter("json") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
revive := lint.New(ioutil.ReadFile) | ||
|
||
lintingRules, err := reviveConfig.GetLintingRules(conf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
packages, err := dots.ResolvePackages(files, normalizeSplit([]string{})) | ||
sspaink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
failures, err := revive.Lint(packages, lintingRules, *conf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
formatChan := make(chan lint.Failure) | ||
exitChan := make(chan bool) | ||
|
||
var output string | ||
go (func() { | ||
output, _ = formatter.Format(formatChan, *conf) | ||
exitChan <- true | ||
})() | ||
|
||
for f := range failures { | ||
if f.Confidence < conf.Confidence { | ||
continue | ||
} | ||
|
||
formatChan <- f | ||
} | ||
|
||
close(formatChan) | ||
<-exitChan | ||
|
||
var results []jsonObject | ||
err = json.Unmarshal([]byte(output), &results) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for i := range results { | ||
issues = append(issues, goanalysis.NewIssue(&result.Issue{ | ||
Severity: string(results[i].Severity), | ||
Text: fmt.Sprintf("%q", results[i].Failure.Failure), | ||
Pos: token.Position{ | ||
Filename: results[i].Position.Start.Filename, | ||
Line: results[i].Position.Start.Line, | ||
Offset: results[i].Position.Start.Offset, | ||
Column: results[i].Position.Start.Column, | ||
}, | ||
LineRange: &result.Range{ | ||
From: results[i].Position.Start.Line, | ||
To: results[i].Position.End.Line, | ||
}, | ||
FromLinter: reviveName, | ||
}, pass)) | ||
} | ||
return nil, nil | ||
} | ||
ldez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { | ||
return issues | ||
}).WithLoadMode(goanalysis.LoadModeSyntax) | ||
} | ||
|
||
func normalizeSplit(strs []string) []string { | ||
res := []string{} | ||
sspaink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for _, s := range strs { | ||
t := strings.Trim(s, " \t") | ||
if len(t) > 0 { | ||
res = append(res, t) | ||
} | ||
} | ||
return res | ||
} | ||
|
||
func SetReviveConfig(cfg *config.ReviveSettings) (*lint.Config, error) { | ||
sspaink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Get revive default configuration | ||
conf, err := reviveConfig.GetConfig("") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Default is false | ||
conf.IgnoreGeneratedHeader = cfg.IgnoreGeneratedHeader | ||
|
||
if cfg.Severity != "" { | ||
conf.Severity = lint.Severity(cfg.Severity) | ||
} | ||
|
||
if cfg.Confidence != 0 { | ||
conf.Confidence = cfg.Confidence | ||
} | ||
|
||
// By default golangci-lint ignores missing doc comments, follow same convention by removing this default rule | ||
// Relevant issue: https://github.com/golangci/golangci-lint/issues/456 | ||
delete(conf.Rules, "exported") | ||
|
||
if len(cfg.Rules) != 0 { | ||
// Clear default rules, only use rules defined in config | ||
conf.Rules = map[string]lint.RuleConfig{} | ||
sspaink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
for _, r := range cfg.Rules { | ||
conf.Rules[r.Name] = lint.RuleConfig{Arguments: r.Arguments, Severity: lint.Severity(r.Severity)} | ||
} | ||
|
||
conf.ErrorCode = cfg.ErrorCode | ||
conf.WarningCode = cfg.WarningCode | ||
|
||
if len(cfg.Directives) != 0 { | ||
// Clear default Directives, only use Directives defined in config | ||
conf.Directives = map[string]lint.DirectiveConfig{} | ||
sspaink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
for _, d := range cfg.Directives { | ||
conf.Directives[d.Name] = lint.DirectiveConfig{Severity: lint.Severity(d.Severity)} | ||
} | ||
|
||
return conf, nil | ||
} |
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,7 @@ | ||
linters-settings: | ||
revive: | ||
ignore-generated-header: true | ||
severity: warning | ||
rules: | ||
- name: indent-error-flow | ||
severity: warning | ||
sspaink marked this conversation as resolved.
Show resolved
Hide resolved
|
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,11 @@ | ||
//args: -Erevive | ||
//config_path: testdata/configs/revive.yml | ||
package testdata | ||
|
||
func testRevive(t string) error { | ||
if t == "" { | ||
return nil | ||
} else { // ERROR "if block ends with a return statement, so drop this else and outdent its block" | ||
return nil | ||
} | ||
} |
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.