1
0
mirror of https://github.com/mgechev/revive.git synced 2025-01-08 03:13:27 +02:00

Fix review comments

This commit is contained in:
dshemin 2019-11-28 10:14:21 +07:00
parent 3c93b9c1a4
commit 9bb943735e
5 changed files with 39 additions and 66 deletions

1
.gitignore vendored
View File

@ -1,4 +1,3 @@
.idea
golinter golinter
revive revive
vendor vendor

View File

@ -1,7 +1,6 @@
package rule package rule
import ( import (
"go/ast"
"regexp" "regexp"
"github.com/mgechev/revive/lint" "github.com/mgechev/revive/lint"
@ -10,68 +9,33 @@ import (
// FileHeaderRule lints given else constructs. // FileHeaderRule lints given else constructs.
type FileHeaderRule struct{} type FileHeaderRule struct{}
// Apply applies the rule to given file.
func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
var failures []lint.Failure
header, ok := arguments[0].(string)
if !ok {
panic("Invalid argument to the FileHeaderRule")
}
regex, err := regexp.Compile(header)
if err != nil {
panic(err.Error())
}
fileAst := file.AST
walker := lintFileHeader{
file: file,
fileAst: fileAst,
regex: regex,
onFailure: func(failure lint.Failure) {
failures = append(failures, failure)
},
}
ast.Walk(walker, fileAst)
return failures
}
// Name returns the rule name.
func (r *FileHeaderRule) Name() string {
return "file-header"
}
type lintFileHeader struct {
file *lint.File
fileAst *ast.File
regex *regexp.Regexp
onFailure func(lint.Failure)
}
var ( var (
multiRegexp = regexp.MustCompile("^/\\*") multiRegexp = regexp.MustCompile("^/\\*")
singleRegexp = regexp.MustCompile("^//") singleRegexp = regexp.MustCompile("^//")
) )
func (w lintFileHeader) Visit(_ ast.Node) ast.Visitor { // Apply applies the rule to given file.
failure := lint.Failure{ func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
Node: w.fileAst, header, ok := arguments[0].(string)
Confidence: 1, if !ok {
Failure: "the file doesn't have an appropriate header", panic("Invalid argument to the FileHeaderRule")
} }
if len(w.fileAst.Comments) == 0 { failure := []lint.Failure{
w.onFailure(failure) {
return nil Node: file.AST,
Confidence: 1,
Failure: "the file doesn't have an appropriate header",
},
} }
g := w.fileAst.Comments[0] if len(file.AST.Comments) == 0 {
return failure
}
g := file.AST.Comments[0]
if g == nil { if g == nil {
w.onFailure(failure) return failure
return nil
} }
comment := "" comment := ""
for _, c := range g.List { for _, c := range g.List {
@ -84,8 +48,18 @@ func (w lintFileHeader) Visit(_ ast.Node) ast.Visitor {
comment += text comment += text
} }
if !w.regex.Match([]byte(comment)) { regex, err := regexp.Compile(header)
w.onFailure(failure) if err != nil {
panic(err.Error())
}
if !regex.Match([]byte(comment)) {
return failure
} }
return nil return nil
} }
// Name returns the rule name.
func (r *FileHeaderRule) Name() string {
return "file-header"
}

View File

@ -37,10 +37,4 @@ func BenchmarkLintFileHeader(b *testing.B) {
benchRule(b, "lint-file-header1", &rule.FileHeaderRule{}, &lint.RuleConfig{ benchRule(b, "lint-file-header1", &rule.FileHeaderRule{}, &lint.RuleConfig{
Arguments: []interface{}{"foobar"}, Arguments: []interface{}{"foobar"},
}) })
//var t *testing.T
//for i := 0; i <= b.N; i++ {
// testRule(t, "lint-file-header1", &rule.FileHeaderRule{}, &lint.RuleConfig{
// Arguments: []interface{}{"foobar"},
// })
//}
} }

View File

@ -17,7 +17,10 @@ func TestImportsBlacklist(t *testing.T) {
func BenchmarkImportsBlacklist(b *testing.B) { func BenchmarkImportsBlacklist(b *testing.B) {
args := []interface{}{"crypto/md5", "crypto/sha1"} args := []interface{}{"crypto/md5", "crypto/sha1"}
benchRule(b, "imports-blacklist", &rule.ImportsBlacklistRule{}, &lint.RuleConfig{ var t *testing.T
Arguments: args, for i := 0; i <= b.N; i++ {
}) testRule(t, "imports-blacklist", &rule.ImportsBlacklistRule{}, &lint.RuleConfig{
Arguments: args,
})
}
} }

View File

@ -11,5 +11,8 @@ func TestUnusedParam(t *testing.T) {
} }
func BenchmarkUnusedParam(b *testing.B) { func BenchmarkUnusedParam(b *testing.B) {
benchRule(b, "unused-param", &rule.UnusedParamRule{}) var t *testing.T
for i := 0; i <= b.N; i++ {
testRule(t, "unused-param", &rule.UnusedParamRule{})
}
} }