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