1
0
mirror of https://github.com/mgechev/revive.git synced 2025-01-10 03:17:11 +02:00
revive/rule/rule.go

94 lines
2.1 KiB
Go
Raw Normal View History

2017-08-29 19:47:29 +02:00
package rule
2017-08-28 01:57:16 +02:00
import (
"go/token"
2017-08-28 06:02:59 +02:00
"github.com/mgechev/revive/file"
2017-08-28 01:57:16 +02:00
)
const (
// FailureTypeWarning declares failures of type warning
FailureTypeWarning = "warning"
// FailureTypeError declares failures of type error.
FailureTypeError = "error"
)
// FailureType is the type for the failure types.
type FailureType string
2017-09-02 03:36:47 +02:00
// FailurePosition returns the failure position
type FailurePosition struct {
Start token.Position
End token.Position
}
2017-08-28 01:57:16 +02:00
// Failure defines a struct for a linting failure.
type Failure struct {
Failure string
2017-09-02 06:46:54 +02:00
RuleName string
2017-08-28 01:57:16 +02:00
Type FailureType
Position FailurePosition
file *file.File
}
// GetFilename returns the filename.
func (f *Failure) GetFilename() string {
return f.Position.Start.Filename
}
2017-09-02 03:36:47 +02:00
// DisabledInterval contains a single disabled interval and the associated rule name.
type DisabledInterval struct {
From token.Position
To token.Position
RuleName string
2017-08-28 01:57:16 +02:00
}
2017-09-02 03:36:47 +02:00
// Arguments is type used for the arguments of a rule.
type Arguments []string
2017-08-28 01:57:16 +02:00
2017-11-20 03:18:16 +02:00
// Config contains the rule configuration.
2017-09-02 03:36:47 +02:00
type Config struct {
2017-08-28 01:57:16 +02:00
Name string
2017-09-02 03:36:47 +02:00
Arguments Arguments
2017-08-28 01:57:16 +02:00
}
2017-11-20 03:18:16 +02:00
// Rule defines an abstract rule interaface
2017-08-28 01:57:16 +02:00
type Rule interface {
2017-11-20 03:18:16 +02:00
Name() string
Apply(*file.File, Arguments) []Failure
AddFailures(...Failure)
Failures() []Failure
Position(token.Pos, token.Pos) FailurePosition
}
// AbstractRule defines an abstract rule.
type AbstractRule struct {
failures []Failure
File *file.File
}
// Apply must be overridden by the successor struct.
func (r *AbstractRule) Apply(file *file.File, args Arguments) {
panic("Apply not implemented")
}
// AddFailures adds rule failures.
func (r *AbstractRule) AddFailures(failures ...Failure) {
r.failures = append(r.failures, failures...)
}
// Failures returns the rule failures.
func (r *AbstractRule) Failures() []Failure {
return r.failures
}
// Position returns position by given start and end token.Pos.
func (r *AbstractRule) Position(start token.Pos, end token.Pos) FailurePosition {
s := r.File.ToPosition(start)
e := r.File.ToPosition(end)
return FailurePosition{
Start: s,
End: e,
}
2017-08-28 01:57:16 +02:00
}