1
0
mirror of https://github.com/mgechev/revive.git synced 2025-11-23 22:04:49 +02:00
Files
revive/rule/utils.go

97 lines
2.9 KiB
Go
Raw Normal View History

2018-01-21 18:04:41 -08:00
package rule
2017-11-26 18:48:07 -08:00
import (
2017-11-26 19:28:18 -08:00
"fmt"
2018-01-25 10:42:39 -08:00
"go/token"
2017-11-26 19:28:18 -08:00
"regexp"
"strings"
2017-11-26 18:48:07 -08:00
2018-01-24 15:44:03 -08:00
"github.com/mgechev/revive/lint"
2017-11-26 18:48:07 -08:00
)
// exitFunctions is a map of std packages and functions that are considered as exit functions.
var exitFunctions = map[string]map[string]bool{
"os": {"Exit": true},
"syscall": {"Exit": true},
"log": {
"Fatal": true,
"Fatalf": true,
"Fatalln": true,
"Panic": true,
"Panicf": true,
"Panicln": true,
},
}
2018-01-25 10:42:39 -08:00
func srcLine(src []byte, p token.Position) string {
// Run to end of line in both directions if not at line start/end.
lo, hi := p.Offset, p.Offset+1
for lo > 0 && src[lo-1] != '\n' {
lo--
}
for hi < len(src) && src[hi-1] != '\n' {
hi++
}
return string(src[lo:hi])
}
// checkNumberOfArguments fails if the given number of arguments is not, at least, the expected one.
func checkNumberOfArguments(expected int, args lint.Arguments, ruleName string) error {
if len(args) < expected {
return fmt.Errorf("not enough arguments for %s rule, expected %d, got %d. Please check the rule's documentation", ruleName, expected, len(args))
}
return nil
}
// isRuleOption returns true if arg and name are the same after normalization.
func isRuleOption(arg, name string) bool {
return normalizeRuleOption(arg) == normalizeRuleOption(name)
}
// normalizeRuleOption returns an option name from the argument. It is lowercased and without hyphens.
//
// Example: normalizeRuleOption("allowTypesBefore"), normalizeRuleOption("allow-types-before") -> "allowtypesbefore".
func normalizeRuleOption(arg string) string {
return strings.ToLower(strings.ReplaceAll(arg, "-", ""))
}
var normalizePathReplacer = strings.NewReplacer("-", "", "_", "", ".", "")
// normalizePath removes hyphens, underscores, and dots from the name
//
// Example: normalizePath("foo.bar-_buz") -> "foobarbuz".
func normalizePath(name string) string {
return normalizePathReplacer.Replace(name)
}
// isVersionPath checks if a directory name is a version directory (v1, V2, etc.)
func isVersionPath(name string) bool {
if len(name) < 2 || (name[0] != 'v' && name[0] != 'V') {
return false
}
for i := 1; i < len(name); i++ {
if name[i] < '0' || name[i] > '9' {
return false
}
}
return true
}
var directiveCommentRE = regexp.MustCompile("^//(line |extern |export |[a-z0-9]+:[a-z0-9])") // see https://go-review.googlesource.com/c/website/+/442516/1..2/_content/doc/comment.md#494
func isDirectiveComment(line string) bool {
return directiveCommentRE.MatchString(line)
}
// isCallToExitFunction checks if the function call is a call to an exit function.
func isCallToExitFunction(pkgName, functionName string) bool {
return exitFunctions[pkgName] != nil && exitFunctions[pkgName][functionName]
}
// newInternalFailureError returns a slice of Failure with a single internal failure in it.
func newInternalFailureError(e error) []lint.Failure {
return []lint.Failure{lint.NewInternalFailure(e.Error())}
}