|
| 1 | +package golinters |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "go/token" |
| 7 | + "io/ioutil" |
| 8 | + |
| 9 | + "github.com/mgechev/dots" |
| 10 | + reviveConfig "github.com/mgechev/revive/config" |
| 11 | + "github.com/mgechev/revive/lint" |
| 12 | + "golang.org/x/tools/go/analysis" |
| 13 | + |
| 14 | + "github.com/golangci/golangci-lint/pkg/config" |
| 15 | + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" |
| 16 | + "github.com/golangci/golangci-lint/pkg/lint/linter" |
| 17 | + "github.com/golangci/golangci-lint/pkg/result" |
| 18 | +) |
| 19 | + |
| 20 | +const reviveName = "revive" |
| 21 | + |
| 22 | +// jsonObject defines a JSON object of an failure |
| 23 | +type jsonObject struct { |
| 24 | + Severity lint.Severity |
| 25 | + lint.Failure `json:",inline"` |
| 26 | +} |
| 27 | + |
| 28 | +// NewNewRevive returns a new Revive linter. |
| 29 | +func NewRevive(cfg *config.ReviveSettings) *goanalysis.Linter { |
| 30 | + var issues []goanalysis.Issue |
| 31 | + |
| 32 | + analyzer := &analysis.Analyzer{ |
| 33 | + Name: goanalysis.TheOnlyAnalyzerName, |
| 34 | + Doc: goanalysis.TheOnlyanalyzerDoc, |
| 35 | + } |
| 36 | + |
| 37 | + return goanalysis.NewLinter( |
| 38 | + reviveName, |
| 39 | + "Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint.", |
| 40 | + []*analysis.Analyzer{analyzer}, |
| 41 | + nil, |
| 42 | + ).WithContextSetter(func(lintCtx *linter.Context) { |
| 43 | + analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { |
| 44 | + var files []string |
| 45 | + |
| 46 | + for _, file := range pass.Files { |
| 47 | + files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename) |
| 48 | + } |
| 49 | + |
| 50 | + conf, err := setReviveConfig(cfg) |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
| 55 | + formatter, err := reviveConfig.GetFormatter("json") |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + |
| 60 | + revive := lint.New(ioutil.ReadFile) |
| 61 | + |
| 62 | + lintingRules, err := reviveConfig.GetLintingRules(conf) |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + |
| 67 | + packages, err := dots.ResolvePackages(files, []string{}) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + |
| 72 | + failures, err := revive.Lint(packages, lintingRules, *conf) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + formatChan := make(chan lint.Failure) |
| 78 | + exitChan := make(chan bool) |
| 79 | + |
| 80 | + var output string |
| 81 | + go func() { |
| 82 | + output, err = formatter.Format(formatChan, *conf) |
| 83 | + if err != nil { |
| 84 | + lintCtx.Log.Errorf("Format error: %v", err) |
| 85 | + } |
| 86 | + exitChan <- true |
| 87 | + }() |
| 88 | + |
| 89 | + for f := range failures { |
| 90 | + if f.Confidence < conf.Confidence { |
| 91 | + continue |
| 92 | + } |
| 93 | + |
| 94 | + formatChan <- f |
| 95 | + } |
| 96 | + |
| 97 | + close(formatChan) |
| 98 | + <-exitChan |
| 99 | + |
| 100 | + var results []jsonObject |
| 101 | + err = json.Unmarshal([]byte(output), &results) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + |
| 106 | + for i := range results { |
| 107 | + issues = append(issues, goanalysis.NewIssue(&result.Issue{ |
| 108 | + Severity: string(results[i].Severity), |
| 109 | + Text: fmt.Sprintf("%q", results[i].Failure.Failure), |
| 110 | + Pos: token.Position{ |
| 111 | + Filename: results[i].Position.Start.Filename, |
| 112 | + Line: results[i].Position.Start.Line, |
| 113 | + Offset: results[i].Position.Start.Offset, |
| 114 | + Column: results[i].Position.Start.Column, |
| 115 | + }, |
| 116 | + LineRange: &result.Range{ |
| 117 | + From: results[i].Position.Start.Line, |
| 118 | + To: results[i].Position.End.Line, |
| 119 | + }, |
| 120 | + FromLinter: reviveName, |
| 121 | + }, pass)) |
| 122 | + } |
| 123 | + |
| 124 | + return nil, nil |
| 125 | + } |
| 126 | + }).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { |
| 127 | + return issues |
| 128 | + }).WithLoadMode(goanalysis.LoadModeSyntax) |
| 129 | +} |
| 130 | + |
| 131 | +func setReviveConfig(cfg *config.ReviveSettings) (*lint.Config, error) { |
| 132 | + // Get revive default configuration |
| 133 | + conf, err := reviveConfig.GetConfig("") |
| 134 | + if err != nil { |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + |
| 138 | + // Default is false |
| 139 | + conf.IgnoreGeneratedHeader = cfg.IgnoreGeneratedHeader |
| 140 | + |
| 141 | + if cfg.Severity != "" { |
| 142 | + conf.Severity = lint.Severity(cfg.Severity) |
| 143 | + } |
| 144 | + |
| 145 | + if cfg.Confidence != 0 { |
| 146 | + conf.Confidence = cfg.Confidence |
| 147 | + } |
| 148 | + |
| 149 | + // By default golangci-lint ignores missing doc comments, follow same convention by removing this default rule |
| 150 | + // Relevant issue: https://github.com/golangci/golangci-lint/issues/456 |
| 151 | + delete(conf.Rules, "exported") |
| 152 | + |
| 153 | + if len(cfg.Rules) != 0 { |
| 154 | + // Clear default rules, only use rules defined in config |
| 155 | + conf.Rules = make(map[string]lint.RuleConfig, len(cfg.Rules)) |
| 156 | + } |
| 157 | + for _, r := range cfg.Rules { |
| 158 | + conf.Rules[r.Name] = lint.RuleConfig{Arguments: r.Arguments, Severity: lint.Severity(r.Severity)} |
| 159 | + } |
| 160 | + |
| 161 | + conf.ErrorCode = cfg.ErrorCode |
| 162 | + conf.WarningCode = cfg.WarningCode |
| 163 | + |
| 164 | + if len(cfg.Directives) != 0 { |
| 165 | + // Clear default Directives, only use Directives defined in config |
| 166 | + conf.Directives = make(map[string]lint.DirectiveConfig, len(cfg.Directives)) |
| 167 | + } |
| 168 | + for _, d := range cfg.Directives { |
| 169 | + conf.Directives[d.Name] = lint.DirectiveConfig{Severity: lint.Severity(d.Severity)} |
| 170 | + } |
| 171 | + |
| 172 | + return conf, nil |
| 173 | +} |
0 commit comments