1
0
mirror of https://github.com/mgechev/revive.git synced 2025-07-15 01:04:40 +02:00

Refactoring

This commit is contained in:
mgechev
2018-01-24 15:44:03 -08:00
parent 4f31c1639f
commit f926547659
20 changed files with 118 additions and 118 deletions

View File

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/mgechev/revive/linter" "github.com/mgechev/revive/lint"
"github.com/olekukonko/tablewriter" "github.com/olekukonko/tablewriter"
) )
@ -17,35 +17,35 @@ const (
// CLIFormatter is an implementation of the Formatter interface // CLIFormatter is an implementation of the Formatter interface
// which formats the errors to JSON. // which formats the errors to JSON.
type CLIFormatter struct { type CLIFormatter struct {
Metadata linter.FormatterMetadata Metadata lint.FormatterMetadata
} }
func formatFailure(failure linter.Failure, severity linter.Severity) []string { func formatFailure(failure lint.Failure, severity lint.Severity) []string {
fString := color.BlueString(failure.Failure) fString := color.BlueString(failure.Failure)
fTypeStr := string(severity) fTypeStr := string(severity)
fType := color.RedString(fTypeStr) fType := color.RedString(fTypeStr)
lineColumn := failure.Position lineColumn := failure.Position
pos := fmt.Sprintf("(%d, %d)", lineColumn.Start.Line, lineColumn.Start.Column) pos := fmt.Sprintf("(%d, %d)", lineColumn.Start.Line, lineColumn.Start.Column)
if severity == linter.SeverityWarning { if severity == lint.SeverityWarning {
fType = color.YellowString(fTypeStr) fType = color.YellowString(fTypeStr)
} }
return []string{failure.GetFilename(), pos, fType, fString} return []string{failure.GetFilename(), pos, fType, fString}
} }
// Format formats the failures gotten from the linter. // Format formats the failures gotten from the lint.
func (f *CLIFormatter) Format(failures <-chan linter.Failure, config linter.RulesConfig) (string, error) { func (f *CLIFormatter) Format(failures <-chan lint.Failure, config lint.RulesConfig) (string, error) {
var result [][]string var result [][]string
var totalErrors = 0 var totalErrors = 0
var total = 0 var total = 0
for f := range failures { for f := range failures {
total++ total++
currentType := linter.SeverityWarning currentType := lint.SeverityWarning
if config, ok := config[f.RuleName]; ok && config.Severity == linter.SeverityError { if config, ok := config[f.RuleName]; ok && config.Severity == lint.SeverityError {
currentType = linter.SeverityError currentType = lint.SeverityError
totalErrors++ totalErrors++
} }
result = append(result, formatFailure(f, linter.Severity(currentType))) result = append(result, formatFailure(f, lint.Severity(currentType)))
} }
ps := "problems" ps := "problems"
if total == 1 { if total == 1 {

View File

@ -3,18 +3,18 @@ package formatter
import ( import (
"encoding/json" "encoding/json"
"github.com/mgechev/revive/linter" "github.com/mgechev/revive/lint"
) )
// JSONFormatter is an implementation of the Formatter interface // JSONFormatter is an implementation of the Formatter interface
// which formats the errors to JSON. // which formats the errors to JSON.
type JSONFormatter struct { type JSONFormatter struct {
Metadata linter.FormatterMetadata Metadata lint.FormatterMetadata
} }
// Format formats the failures gotten from the linter. // Format formats the failures gotten from the lint.
func (f *JSONFormatter) Format(failures <-chan linter.Failure, config linter.RulesConfig) (string, error) { func (f *JSONFormatter) Format(failures <-chan lint.Failure, config lint.RulesConfig) (string, error) {
var slice []linter.Failure var slice []lint.Failure
for failure := range failures { for failure := range failures {
slice = append(slice, failure) slice = append(slice, failure)
} }

View File

@ -1,4 +1,4 @@
package linter package lint
import ( import (
"bytes" "bytes"

View File

@ -1,4 +1,4 @@
package linter package lint
// FormatterMetadata configuration of a formatter // FormatterMetadata configuration of a formatter
type FormatterMetadata struct { type FormatterMetadata struct {

View File

@ -1,4 +1,4 @@
package linter package lint
import ( import (
"bufio" "bufio"
@ -29,7 +29,7 @@ var (
// isGenerated reports whether the source file is generated code // isGenerated reports whether the source file is generated code
// according the rules from https://golang.org/s/generatedcode. // according the rules from https://golang.org/s/generatedcode.
// This is inherited from the original go linter. // This is inherited from the original go lint.
func isGenerated(src []byte) bool { func isGenerated(src []byte) bool {
sc := bufio.NewScanner(bytes.NewReader(src)) sc := bufio.NewScanner(bytes.NewReader(src))
for sc.Scan() { for sc.Scan() {

View File

@ -1,4 +1,4 @@
package linter package lint
import ( import (
"go/ast" "go/ast"

View File

@ -1,4 +1,4 @@
package linter package lint
import ( import (
"go/ast" "go/ast"

View File

@ -1,12 +1,12 @@
package linter package lint
import ( import (
"strings" "strings"
"unicode" "unicode"
) )
// LintName returns a different name if it should be different. // Name returns a different name if it should be different.
func LintName(name string) (should string) { func Name(name string) (should string) {
// Fast path for simple cases: "_" and all lowercase. // Fast path for simple cases: "_" and all lowercase.
if name == "_" { if name == "_" {
return name return name

16
main.go
View File

@ -5,7 +5,7 @@ import (
"os" "os"
"github.com/mgechev/revive/formatter" "github.com/mgechev/revive/formatter"
"github.com/mgechev/revive/linter" "github.com/mgechev/revive/lint"
"github.com/mgechev/revive/rule" "github.com/mgechev/revive/rule"
) )
@ -26,16 +26,16 @@ func main() {
} }
` `
revive := linter.New(func(file string) ([]byte, error) { revive := lint.New(func(file string) ([]byte, error) {
return []byte(src), nil return []byte(src), nil
}) })
var result []linter.Rule var result []lint.Rule
result = append(result, &rule.LintElseRule{}, &rule.ArgumentsLimitRule{}, &rule.NamesRule{}) result = append(result, &rule.LintElseRule{}, &rule.ArgumentsLimitRule{}, &rule.NamesRule{})
var config = linter.RulesConfig{ var config = lint.RulesConfig{
"argument-limit": linter.RuleConfig{ "argument-limit": lint.RuleConfig{
Arguments: []string{"3"}, Arguments: []string{"3"},
Severity: linter.SeverityWarning, Severity: lint.SeverityWarning,
}, },
} }
@ -44,7 +44,7 @@ func main() {
panic(err) panic(err)
} }
formatChan := make(chan linter.Failure) formatChan := make(chan lint.Failure)
exitChan := make(chan bool) exitChan := make(chan bool)
var output string var output string
@ -59,7 +59,7 @@ func main() {
exitCode := 0 exitCode := 0
for f := range failures { for f := range failures {
if c, ok := config[f.RuleName]; ok && c.Severity == linter.SeverityError { if c, ok := config[f.RuleName]; ok && c.Severity == lint.SeverityError {
exitCode = 1 exitCode = 1
} }
formatChan <- f formatChan <- f

View File

@ -5,14 +5,14 @@ import (
"go/ast" "go/ast"
"strconv" "strconv"
"github.com/mgechev/revive/linter" "github.com/mgechev/revive/lint"
) )
// ArgumentsLimitRule lints given else constructs. // ArgumentsLimitRule lints given else constructs.
type ArgumentsLimitRule struct{} type ArgumentsLimitRule struct{}
// Apply applies the rule to given file. // Apply applies the rule to given file.
func (r *ArgumentsLimitRule) Apply(file *linter.File, arguments linter.Arguments) []linter.Failure { func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
if len(arguments) != 1 { if len(arguments) != 1 {
panic(`invalid configuration for "argument-limit"`) panic(`invalid configuration for "argument-limit"`)
} }
@ -21,11 +21,11 @@ func (r *ArgumentsLimitRule) Apply(file *linter.File, arguments linter.Arguments
panic(`invalid configuration for "argument-limit"`) panic(`invalid configuration for "argument-limit"`)
} }
var failures []linter.Failure var failures []lint.Failure
walker := lintArgsNum{ walker := lintArgsNum{
total: total, total: total,
onFailure: func(failure linter.Failure) { onFailure: func(failure lint.Failure) {
failures = append(failures, failure) failures = append(failures, failure)
}, },
} }
@ -42,7 +42,7 @@ func (r *ArgumentsLimitRule) Name() string {
type lintArgsNum struct { type lintArgsNum struct {
total int64 total int64
onFailure func(linter.Failure) onFailure func(lint.Failure)
} }
func (w lintArgsNum) Visit(n ast.Node) ast.Visitor { func (w lintArgsNum) Visit(n ast.Node) ast.Visitor {
@ -50,7 +50,7 @@ func (w lintArgsNum) Visit(n ast.Node) ast.Visitor {
if ok { if ok {
num := int64(len(node.Type.Params.List)) num := int64(len(node.Type.Params.List))
if num > w.total { if num > w.total {
w.onFailure(linter.Failure{ w.onFailure(lint.Failure{
Failure: fmt.Sprintf("maximum number of arguments per function exceeded; max %d but got %d", w.total, num), Failure: fmt.Sprintf("maximum number of arguments per function exceeded; max %d but got %d", w.total, num),
Node: node.Type, Node: node.Type,
}) })

View File

@ -3,21 +3,21 @@ package rule
import ( import (
"go/ast" "go/ast"
"github.com/mgechev/revive/linter" "github.com/mgechev/revive/lint"
) )
// BlankImportsRule lints given else constructs. // BlankImportsRule lints given else constructs.
type BlankImportsRule struct{} type BlankImportsRule struct{}
// Apply applies the rule to given file. // Apply applies the rule to given file.
func (r *BlankImportsRule) Apply(file *linter.File, arguments linter.Arguments) []linter.Failure { func (r *BlankImportsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
var failures []linter.Failure var failures []lint.Failure
fileAst := file.AST fileAst := file.AST
walker := lintBlankImports{ walker := lintBlankImports{
file: file, file: file,
fileAst: fileAst, fileAst: fileAst,
onFailure: func(failure linter.Failure) { onFailure: func(failure lint.Failure) {
failures = append(failures, failure) failures = append(failures, failure)
}, },
} }
@ -34,8 +34,8 @@ func (r *BlankImportsRule) Name() string {
type lintBlankImports struct { type lintBlankImports struct {
fileAst *ast.File fileAst *ast.File
file *linter.File file *lint.File
onFailure func(linter.Failure) onFailure func(lint.Failure)
} }
func (w lintBlankImports) Visit(n ast.Node) ast.Visitor { func (w lintBlankImports) Visit(n ast.Node) ast.Visitor {
@ -62,7 +62,7 @@ func (w lintBlankImports) Visit(n ast.Node) ast.Visitor {
// This is the first blank import of a group. // This is the first blank import of a group.
if imp.Doc == nil && imp.Comment == nil { if imp.Doc == nil && imp.Comment == nil {
w.onFailure(linter.Failure{ w.onFailure(lint.Failure{
Node: imp, Node: imp,
Failure: "a blank import should be only in a main or test package, or have a comment justifying it", Failure: "a blank import should be only in a main or test package, or have a comment justifying it",
Confidence: 1, Confidence: 1,

View File

@ -3,21 +3,21 @@ package rule
import ( import (
"go/ast" "go/ast"
"github.com/mgechev/revive/linter" "github.com/mgechev/revive/lint"
) )
// DotImportsRule lints given else constructs. // DotImportsRule lints given else constructs.
type DotImportsRule struct{} type DotImportsRule struct{}
// Apply applies the rule to given file. // Apply applies the rule to given file.
func (r *DotImportsRule) Apply(file *linter.File, arguments linter.Arguments) []linter.Failure { func (r *DotImportsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
var failures []linter.Failure var failures []lint.Failure
fileAst := file.AST fileAst := file.AST
walker := lintImports{ walker := lintImports{
file: file, file: file,
fileAst: fileAst, fileAst: fileAst,
onFailure: func(failure linter.Failure) { onFailure: func(failure lint.Failure) {
failures = append(failures, failure) failures = append(failures, failure)
}, },
} }
@ -33,16 +33,16 @@ func (r *DotImportsRule) Name() string {
} }
type lintImports struct { type lintImports struct {
file *linter.File file *lint.File
fileAst *ast.File fileAst *ast.File
onFailure func(linter.Failure) onFailure func(lint.Failure)
} }
func (w lintImports) Visit(n ast.Node) ast.Visitor { func (w lintImports) Visit(n ast.Node) ast.Visitor {
for i, is := range w.fileAst.Imports { for i, is := range w.fileAst.Imports {
_ = i _ = i
if is.Name != nil && is.Name.Name == "." && !w.file.IsTest() { if is.Name != nil && is.Name.Name == "." && !w.file.IsTest() {
w.onFailure(linter.Failure{ w.onFailure(lint.Failure{
Confidence: 1, Confidence: 1,
Failure: "should not use dot imports", Failure: "should not use dot imports",
Node: is, Node: is,

View File

@ -8,15 +8,15 @@ import (
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
"github.com/mgechev/revive/linter" "github.com/mgechev/revive/lint"
) )
// ExportedRule lints given else constructs. // ExportedRule lints given else constructs.
type ExportedRule struct{} type ExportedRule struct{}
// Apply applies the rule to given file. // Apply applies the rule to given file.
func (r *ExportedRule) Apply(file *linter.File, arguments linter.Arguments) []linter.Failure { func (r *ExportedRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
var failures []linter.Failure var failures []lint.Failure
if isTest(file) { if isTest(file) {
return failures return failures
@ -26,7 +26,7 @@ func (r *ExportedRule) Apply(file *linter.File, arguments linter.Arguments) []li
walker := lintExported{ walker := lintExported{
file: file, file: file,
fileAst: fileAst, fileAst: fileAst,
onFailure: func(failure linter.Failure) { onFailure: func(failure lint.Failure) {
failures = append(failures, failure) failures = append(failures, failure)
}, },
genDeclMissingComments: make(map[*ast.GenDecl]bool), genDeclMissingComments: make(map[*ast.GenDecl]bool),
@ -43,11 +43,11 @@ func (r *ExportedRule) Name() string {
} }
type lintExported struct { type lintExported struct {
file *linter.File file *lint.File
fileAst *ast.File fileAst *ast.File
lastGen *ast.GenDecl lastGen *ast.GenDecl
genDeclMissingComments map[*ast.GenDecl]bool genDeclMissingComments map[*ast.GenDecl]bool
onFailure func(linter.Failure) onFailure func(lint.Failure)
} }
func (w *lintExported) lintFuncDoc(fn *ast.FuncDecl) { func (w *lintExported) lintFuncDoc(fn *ast.FuncDecl) {
@ -77,7 +77,7 @@ func (w *lintExported) lintFuncDoc(fn *ast.FuncDecl) {
name = recv + "." + name name = recv + "." + name
} }
if fn.Doc == nil { if fn.Doc == nil {
w.onFailure(linter.Failure{ w.onFailure(lint.Failure{
Node: fn, Node: fn,
Confidence: 1, Confidence: 1,
URL: "#doc-comments", URL: "#doc-comments",
@ -89,7 +89,7 @@ func (w *lintExported) lintFuncDoc(fn *ast.FuncDecl) {
s := fn.Doc.Text() s := fn.Doc.Text()
prefix := fn.Name.Name + " " prefix := fn.Name.Name + " "
if !strings.HasPrefix(s, prefix) { if !strings.HasPrefix(s, prefix) {
w.onFailure(linter.Failure{ w.onFailure(lint.Failure{
Node: fn.Doc, Node: fn.Doc,
Confidence: 0.8, Confidence: 0.8,
URL: "#doc-comments", URL: "#doc-comments",
@ -120,7 +120,7 @@ func (w *lintExported) checkStutter(id *ast.Ident, thing string) {
// the it's starting a new word and thus this name stutters. // the it's starting a new word and thus this name stutters.
rem := name[len(pkg):] rem := name[len(pkg):]
if next, _ := utf8.DecodeRuneInString(rem); next == '_' || unicode.IsUpper(next) { if next, _ := utf8.DecodeRuneInString(rem); next == '_' || unicode.IsUpper(next) {
w.onFailure(linter.Failure{ w.onFailure(lint.Failure{
Node: id, Node: id,
Confidence: 0.8, Confidence: 0.8,
URL: "#package-names", URL: "#package-names",
@ -135,7 +135,7 @@ func (w *lintExported) lintTypeDoc(t *ast.TypeSpec, doc *ast.CommentGroup) {
return return
} }
if doc == nil { if doc == nil {
w.onFailure(linter.Failure{ w.onFailure(lint.Failure{
Node: t, Node: t,
Confidence: 1, Confidence: 1,
URL: "#doc-comments", URL: "#doc-comments",
@ -154,7 +154,7 @@ func (w *lintExported) lintTypeDoc(t *ast.TypeSpec, doc *ast.CommentGroup) {
} }
} }
if !strings.HasPrefix(s, t.Name.Name+" ") { if !strings.HasPrefix(s, t.Name.Name+" ") {
w.onFailure(linter.Failure{ w.onFailure(lint.Failure{
Node: doc, Node: doc,
Confidence: 1, Confidence: 1,
URL: "#doc-comments", URL: "#doc-comments",
@ -174,7 +174,7 @@ func (w *lintExported) lintValueSpecDoc(vs *ast.ValueSpec, gd *ast.GenDecl, genD
// Check that none are exported except for the first. // Check that none are exported except for the first.
for _, n := range vs.Names[1:] { for _, n := range vs.Names[1:] {
if ast.IsExported(n.Name) { if ast.IsExported(n.Name) {
w.onFailure(linter.Failure{ w.onFailure(lint.Failure{
Category: "comments", Category: "comments",
Confidence: 1, Confidence: 1,
Failure: fmt.Sprintf("exported %s %s should have its own declaration", kind, n.Name), Failure: fmt.Sprintf("exported %s %s should have its own declaration", kind, n.Name),
@ -199,7 +199,7 @@ func (w *lintExported) lintValueSpecDoc(vs *ast.ValueSpec, gd *ast.GenDecl, genD
if kind == "const" && gd.Lparen.IsValid() { if kind == "const" && gd.Lparen.IsValid() {
block = " (or a comment on this block)" block = " (or a comment on this block)"
} }
w.onFailure(linter.Failure{ w.onFailure(lint.Failure{
Confidence: 1, Confidence: 1,
Node: vs, Node: vs,
URL: "#doc-comments", URL: "#doc-comments",
@ -221,7 +221,7 @@ func (w *lintExported) lintValueSpecDoc(vs *ast.ValueSpec, gd *ast.GenDecl, genD
} }
prefix := name + " " prefix := name + " "
if !strings.HasPrefix(doc.Text(), prefix) { if !strings.HasPrefix(doc.Text(), prefix) {
w.onFailure(linter.Failure{ w.onFailure(lint.Failure{
Confidence: 1, Confidence: 1,
Node: doc, Node: doc,
URL: "#doc-comments", URL: "#doc-comments",

View File

@ -6,15 +6,15 @@ import (
"go/token" "go/token"
"strings" "strings"
"github.com/mgechev/revive/linter" "github.com/mgechev/revive/lint"
) )
// NamesRule lints given else constructs. // NamesRule lints given else constructs.
type NamesRule struct{} type NamesRule struct{}
// Apply applies the rule to given file. // Apply applies the rule to given file.
func (r *NamesRule) Apply(file *linter.File, arguments linter.Arguments) []linter.Failure { func (r *NamesRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
var failures []linter.Failure var failures []lint.Failure
if isTest(file) { if isTest(file) {
return failures return failures
@ -24,14 +24,14 @@ func (r *NamesRule) Apply(file *linter.File, arguments linter.Arguments) []linte
walker := lintNames{ walker := lintNames{
file: file, file: file,
fileAst: fileAst, fileAst: fileAst,
onFailure: func(failure linter.Failure) { onFailure: func(failure lint.Failure) {
failures = append(failures, failure) failures = append(failures, failure)
}, },
} }
// Package names need slightly different handling than other names. // Package names need slightly different handling than other names.
if strings.Contains(walker.fileAst.Name.Name, "_") && !strings.HasSuffix(walker.fileAst.Name.Name, "_test") { if strings.Contains(walker.fileAst.Name.Name, "_") && !strings.HasSuffix(walker.fileAst.Name.Name, "_test") {
walker.onFailure(linter.Failure{ walker.onFailure(lint.Failure{
Failure: "don't use an underscore in package name", Failure: "don't use an underscore in package name",
Confidence: 1, Confidence: 1,
Node: walker.fileAst, Node: walker.fileAst,
@ -71,7 +71,7 @@ func check(id *ast.Ident, thing string, w *lintNames) {
// Handle two common styles from other languages that don't belong in Go. // Handle two common styles from other languages that don't belong in Go.
if len(id.Name) >= 5 && allCapsRE.MatchString(id.Name) && strings.Contains(id.Name, "_") { if len(id.Name) >= 5 && allCapsRE.MatchString(id.Name) && strings.Contains(id.Name, "_") {
w.onFailure(linter.Failure{ w.onFailure(lint.Failure{
Failure: "don't use ALL_CAPS in Go names; use CamelCase", Failure: "don't use ALL_CAPS in Go names; use CamelCase",
Confidence: 0.8, Confidence: 0.8,
Node: id, Node: id,
@ -82,7 +82,7 @@ func check(id *ast.Ident, thing string, w *lintNames) {
} }
if len(id.Name) > 2 && id.Name[0] == 'k' && id.Name[1] >= 'A' && id.Name[1] <= 'Z' { if len(id.Name) > 2 && id.Name[0] == 'k' && id.Name[1] >= 'A' && id.Name[1] <= 'Z' {
should := string(id.Name[1]+'a'-'A') + id.Name[2:] should := string(id.Name[1]+'a'-'A') + id.Name[2:]
w.onFailure(linter.Failure{ w.onFailure(lint.Failure{
Failure: fmt.Sprintf("don't use leading k in Go names; %s %s should be %s", thing, id.Name, should), Failure: fmt.Sprintf("don't use leading k in Go names; %s %s should be %s", thing, id.Name, should),
Confidence: 0.8, Confidence: 0.8,
Node: id, Node: id,
@ -91,13 +91,13 @@ func check(id *ast.Ident, thing string, w *lintNames) {
}) })
} }
should := linter.LintName(id.Name) should := lint.Name(id.Name)
if id.Name == should { if id.Name == should {
return return
} }
if len(id.Name) > 2 && strings.Contains(id.Name[1:], "_") { if len(id.Name) > 2 && strings.Contains(id.Name[1:], "_") {
w.onFailure(linter.Failure{ w.onFailure(lint.Failure{
Failure: fmt.Sprintf("don't use underscores in Go names; %s %s should be %s", thing, id.Name, should), Failure: fmt.Sprintf("don't use underscores in Go names; %s %s should be %s", thing, id.Name, should),
Confidence: 0.9, Confidence: 0.9,
Node: id, Node: id,
@ -106,7 +106,7 @@ func check(id *ast.Ident, thing string, w *lintNames) {
}) })
return return
} }
w.onFailure(linter.Failure{ w.onFailure(lint.Failure{
Failure: fmt.Sprintf("%s %s should be %s", thing, id.Name, should), Failure: fmt.Sprintf("%s %s should be %s", thing, id.Name, should),
Confidence: 0.8, Confidence: 0.8,
Node: id, Node: id,
@ -116,11 +116,11 @@ func check(id *ast.Ident, thing string, w *lintNames) {
} }
type lintNames struct { type lintNames struct {
file *linter.File file *lint.File
fileAst *ast.File fileAst *ast.File
lastGen *ast.GenDecl lastGen *ast.GenDecl
genDeclMissingComments map[*ast.GenDecl]bool genDeclMissingComments map[*ast.GenDecl]bool
onFailure func(linter.Failure) onFailure func(lint.Failure)
} }
func (w *lintNames) Visit(n ast.Node) ast.Visitor { func (w *lintNames) Visit(n ast.Node) ast.Visitor {

View File

@ -4,17 +4,17 @@ import (
"go/ast" "go/ast"
"go/token" "go/token"
"github.com/mgechev/revive/linter" "github.com/mgechev/revive/lint"
) )
// LintElseRule lints given else constructs. // LintElseRule lints given else constructs.
type LintElseRule struct{} type LintElseRule struct{}
// Apply applies the rule to given file. // Apply applies the rule to given file.
func (r *LintElseRule) Apply(file *linter.File, arguments linter.Arguments) []linter.Failure { func (r *LintElseRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
var failures []linter.Failure var failures []lint.Failure
onFailure := func(failure linter.Failure) { onFailure := func(failure lint.Failure) {
failures = append(failures, failure) failures = append(failures, failure)
} }
@ -30,7 +30,7 @@ func (r *LintElseRule) Name() string {
type lintElse struct { type lintElse struct {
ignore map[*ast.IfStmt]bool ignore map[*ast.IfStmt]bool
onFailure func(linter.Failure) onFailure func(lint.Failure)
} }
func (w lintElse) Visit(node ast.Node) ast.Visitor { func (w lintElse) Visit(node ast.Node) ast.Visitor {
@ -64,7 +64,7 @@ func (w lintElse) Visit(node ast.Node) ast.Visitor {
if shortDecl { if shortDecl {
extra = " (move short variable declaration to its own line if necessary)" extra = " (move short variable declaration to its own line if necessary)"
} }
w.onFailure(linter.Failure{ w.onFailure(lint.Failure{
Failure: "if block ends with a return statement, so drop this else and outdent its block" + extra, Failure: "if block ends with a return statement, so drop this else and outdent its block" + extra,
Node: ifStmt.Else, Node: ifStmt.Else,
}) })

View File

@ -6,7 +6,7 @@ import (
"go/token" "go/token"
"strings" "strings"
"github.com/mgechev/revive/linter" "github.com/mgechev/revive/lint"
) )
// PackageCommentsRule lints the package comments. It complains if // PackageCommentsRule lints the package comments. It complains if
@ -17,14 +17,14 @@ import (
type PackageCommentsRule struct{} type PackageCommentsRule struct{}
// Apply applies the rule to given file. // Apply applies the rule to given file.
func (r *PackageCommentsRule) Apply(file *linter.File, arguments linter.Arguments) []linter.Failure { func (r *PackageCommentsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
var failures []linter.Failure var failures []lint.Failure
if isTest(file) { if isTest(file) {
return failures return failures
} }
onFailure := func(failure linter.Failure) { onFailure := func(failure lint.Failure) {
failures = append(failures, failure) failures = append(failures, failure)
} }
@ -41,8 +41,8 @@ func (r *PackageCommentsRule) Name() string {
type lintPackageComments struct { type lintPackageComments struct {
fileAst *ast.File fileAst *ast.File
file *linter.File file *lint.File
onFailure func(linter.Failure) onFailure func(lint.Failure)
} }
func (l *lintPackageComments) Visit(n ast.Node) ast.Visitor { func (l *lintPackageComments) Visit(n ast.Node) ast.Visitor {
@ -76,9 +76,9 @@ func (l *lintPackageComments) Visit(n ast.Node) ast.Visitor {
Line: endPos.Line + 1, Line: endPos.Line + 1,
Column: 1, Column: 1,
} }
l.onFailure(linter.Failure{ l.onFailure(lint.Failure{
Category: "comments", Category: "comments",
Position: linter.FailurePosition{ Position: lint.FailurePosition{
Start: pos, Start: pos,
End: pos, End: pos,
}, },
@ -91,7 +91,7 @@ func (l *lintPackageComments) Visit(n ast.Node) ast.Visitor {
} }
if l.fileAst.Doc == nil { if l.fileAst.Doc == nil {
l.onFailure(linter.Failure{ l.onFailure(lint.Failure{
Category: "comments", Category: "comments",
Node: l.fileAst, Node: l.fileAst,
Confidence: 0.2, Confidence: 0.2,
@ -102,7 +102,7 @@ func (l *lintPackageComments) Visit(n ast.Node) ast.Visitor {
} }
s := l.fileAst.Doc.Text() s := l.fileAst.Doc.Text()
if ts := strings.TrimLeft(s, " \t"); ts != s { if ts := strings.TrimLeft(s, " \t"); ts != s {
l.onFailure(linter.Failure{ l.onFailure(lint.Failure{
Category: "comments", Category: "comments",
Node: l.fileAst.Doc, Node: l.fileAst.Doc,
Confidence: 1, Confidence: 1,
@ -113,7 +113,7 @@ func (l *lintPackageComments) Visit(n ast.Node) ast.Visitor {
} }
// Only non-main packages need to keep to this form. // Only non-main packages need to keep to this form.
if !l.file.Pkg.IsMain() && !strings.HasPrefix(s, prefix) { if !l.file.Pkg.IsMain() && !strings.HasPrefix(s, prefix) {
l.onFailure(linter.Failure{ l.onFailure(lint.Failure{
Category: "comments", Category: "comments",
Node: l.fileAst.Doc, Node: l.fileAst.Doc,
Confidence: 1, Confidence: 1,

View File

@ -4,17 +4,17 @@ import (
"fmt" "fmt"
"go/ast" "go/ast"
"github.com/mgechev/revive/linter" "github.com/mgechev/revive/lint"
) )
// LintRangesRule lints given else constructs. // LintRangesRule lints given else constructs.
type LintRangesRule struct{} type LintRangesRule struct{}
// Apply applies the rule to given file. // Apply applies the rule to given file.
func (r *LintRangesRule) Apply(file *linter.File, arguments linter.Arguments) []linter.Failure { func (r *LintRangesRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
var failures []linter.Failure var failures []lint.Failure
onFailure := func(failure linter.Failure) { onFailure := func(failure lint.Failure) {
failures = append(failures, failure) failures = append(failures, failure)
} }
@ -29,8 +29,8 @@ func (r *LintRangesRule) Name() string {
} }
type lintRanges struct { type lintRanges struct {
file *linter.File file *lint.File
onFailure func(linter.Failure) onFailure func(lint.Failure)
} }
func (w *lintRanges) Visit(node ast.Node) ast.Visitor { func (w *lintRanges) Visit(node ast.Node) ast.Visitor {
@ -47,7 +47,7 @@ func (w *lintRanges) Visit(node ast.Node) ast.Visitor {
return w return w
} }
w.onFailure(linter.Failure{ w.onFailure(lint.Failure{
Failure: fmt.Sprintf("should omit 2nd value from range; this loop is equivalent to `for %s %s range ...`", w.file.Render(rs.Key), rs.Tok), Failure: fmt.Sprintf("should omit 2nd value from range; this loop is equivalent to `for %s %s range ...`", w.file.Render(rs.Key), rs.Tok),
Confidence: 1, Confidence: 1,
Node: rs.Value, Node: rs.Value,

View File

@ -7,7 +7,7 @@ import (
"regexp" "regexp"
"strings" "strings"
"github.com/mgechev/revive/linter" "github.com/mgechev/revive/lint"
) )
const styleGuideBase = "https://golang.org/wiki/CodeReviewComments" const styleGuideBase = "https://golang.org/wiki/CodeReviewComments"
@ -16,7 +16,7 @@ const styleGuideBase = "https://golang.org/wiki/CodeReviewComments"
// If id == nil, the answer is false. // If id == nil, the answer is false.
func isBlank(id *ast.Ident) bool { return id != nil && id.Name == "_" } func isBlank(id *ast.Ident) bool { return id != nil && id.Name == "_" }
func isTest(f *linter.File) bool { func isTest(f *lint.File) bool {
return strings.HasSuffix(f.Name, "_test.go") return strings.HasSuffix(f.Name, "_test.go")
} }

View File

@ -6,21 +6,21 @@ import (
"go/token" "go/token"
"go/types" "go/types"
"github.com/mgechev/revive/linter" "github.com/mgechev/revive/lint"
) )
// VarDeclarationsRule lints given else constructs. // VarDeclarationsRule lints given else constructs.
type VarDeclarationsRule struct{} type VarDeclarationsRule struct{}
// Apply applies the rule to given file. // Apply applies the rule to given file.
func (r *VarDeclarationsRule) Apply(file *linter.File, arguments linter.Arguments) []linter.Failure { func (r *VarDeclarationsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
var failures []linter.Failure var failures []lint.Failure
fileAst := file.AST fileAst := file.AST
walker := &lintVarDeclarations{ walker := &lintVarDeclarations{
file: file, file: file,
fileAst: fileAst, fileAst: fileAst,
onFailure: func(failure linter.Failure) { onFailure: func(failure lint.Failure) {
failures = append(failures, failure) failures = append(failures, failure)
}, },
} }
@ -37,9 +37,9 @@ func (r *VarDeclarationsRule) Name() string {
type lintVarDeclarations struct { type lintVarDeclarations struct {
fileAst *ast.File fileAst *ast.File
file *linter.File file *lint.File
lastGen *ast.GenDecl lastGen *ast.GenDecl
onFailure func(linter.Failure) onFailure func(lint.Failure)
} }
func (w *lintVarDeclarations) Visit(node ast.Node) ast.Visitor { func (w *lintVarDeclarations) Visit(node ast.Node) ast.Visitor {
@ -71,7 +71,7 @@ func (w *lintVarDeclarations) Visit(node ast.Node) ast.Visitor {
zero = true zero = true
} }
if zero { if zero {
w.onFailure(linter.Failure{ w.onFailure(lint.Failure{
Confidence: 0.9, Confidence: 0.9,
Node: rhs, Node: rhs,
Failure: fmt.Sprintf("should drop = %s from declaration of var %s; it is the zero value", w.file.Render(rhs), v.Names[0]), Failure: fmt.Sprintf("should drop = %s from declaration of var %s; it is the zero value", w.file.Render(rhs), v.Names[0]),
@ -106,7 +106,7 @@ func (w *lintVarDeclarations) Visit(node ast.Node) ast.Visitor {
return nil return nil
} }
w.onFailure(linter.Failure{ w.onFailure(lint.Failure{
Category: "type-inference", Category: "type-inference",
Confidence: 0.8, Confidence: 0.8,
Node: v.Type, Node: v.Type,

View File

@ -27,12 +27,12 @@ import (
"github.com/mgechev/revive/rule" "github.com/mgechev/revive/rule"
"github.com/mgechev/revive/linter" "github.com/mgechev/revive/lint"
) )
var lintMatch = flag.String("lint.match", "", "restrict fixtures matches to this pattern") var lintMatch = flag.String("lint.match", "", "restrict fixtures matches to this pattern")
var rules = []linter.Rule{ var rules = []lint.Rule{
&rule.VarDeclarationsRule{}, &rule.VarDeclarationsRule{},
&rule.PackageCommentsRule{}, &rule.PackageCommentsRule{},
&rule.DotImportsRule{}, &rule.DotImportsRule{},
@ -43,7 +43,7 @@ var rules = []linter.Rule{
func TestAll(t *testing.T) { func TestAll(t *testing.T) {
baseDir := "../fixtures/" baseDir := "../fixtures/"
l := linter.New(func(file string) ([]byte, error) { l := lint.New(func(file string) ([]byte, error) {
return ioutil.ReadFile(baseDir + file) return ioutil.ReadFile(baseDir + file)
}) })
rx, err := regexp.Compile(*lintMatch) rx, err := regexp.Compile(*lintMatch)
@ -74,13 +74,13 @@ func TestAll(t *testing.T) {
continue continue
} }
ps, err := l.Lint([]string{fi.Name()}, rules, map[string]linter.Arguments{}) ps, err := l.Lint([]string{fi.Name()}, rules, map[string]lint.Arguments{})
if err != nil { if err != nil {
t.Errorf("Linting %s: %v", fi.Name(), err) t.Errorf("Linting %s: %v", fi.Name(), err)
continue continue
} }
failures := []linter.Failure{} failures := []lint.Failure{}
for f := range ps { for f := range ps {
failures = append(failures, f) failures = append(failures, f)
} }
@ -275,7 +275,7 @@ func TestLintName(t *testing.T) {
{"IEEE802_16Bit", "IEEE802_16Bit"}, {"IEEE802_16Bit", "IEEE802_16Bit"},
} }
for _, test := range tests { for _, test := range tests {
got := linter.LintName(test.name) got := lint.Name(test.name)
if got != test.want { if got != test.want {
t.Errorf("lintName(%q) = %q, want %q", test.name, got, test.want) t.Errorf("lintName(%q) = %q, want %q", test.name, got, test.want)
} }