mirror of
				https://github.com/mgechev/revive.git
				synced 2025-10-30 23:37:49 +02:00 
			
		
		
		
	Refactoring
This commit is contained in:
		| @@ -1,12 +1,12 @@ | ||||
| package defaultrules | ||||
| package defaultrule | ||||
| 
 | ||||
| import ( | ||||
| 	"go/ast" | ||||
| 	"go/token" | ||||
| 
 | ||||
| 	"github.com/mgechev/revive/file" | ||||
| 	"github.com/mgechev/revive/rules" | ||||
| 	"github.com/mgechev/revive/visitors" | ||||
| 	"github.com/mgechev/revive/rule" | ||||
| 	"github.com/mgechev/revive/visitor" | ||||
| ) | ||||
| 
 | ||||
| const ( | ||||
| @@ -16,19 +16,19 @@ const ( | ||||
| 
 | ||||
| // LintElseRule lints given else constructs. | ||||
| type LintElseRule struct { | ||||
| 	rules.Rule | ||||
| 	rule.Rule | ||||
| } | ||||
| 
 | ||||
| // Apply applies the rule to given file. | ||||
| func (r *LintElseRule) Apply(file *file.File, arguments rules.RuleArguments) []rules.Failure { | ||||
| func (r *LintElseRule) Apply(file *file.File, arguments rule.RuleArguments) []rule.Failure { | ||||
| 	res := &lintElseVisitor{} | ||||
| 	visitors.Setup(res, rules.RuleConfig{Name: ruleName, Arguments: arguments}, file) | ||||
| 	visitor.Setup(res, rule.RuleConfig{Name: ruleName, Arguments: arguments}, file) | ||||
| 	res.Visit(file.GetAST()) | ||||
| 	return res.GetFailures() | ||||
| } | ||||
| 
 | ||||
| type lintElseVisitor struct { | ||||
| 	visitors.RuleVisitor | ||||
| 	visitor.RuleVisitor | ||||
| } | ||||
| 
 | ||||
| func (w *lintElseVisitor) VisitIfStmt(node *ast.IfStmt) { | ||||
| @@ -50,9 +50,9 @@ func (w *lintElseVisitor) VisitIfStmt(node *ast.IfStmt) { | ||||
| 	} | ||||
| 	lastStmt := node.Body.List[len(node.Body.List)-1] | ||||
| 	if _, ok := lastStmt.(*ast.ReturnStmt); ok { | ||||
| 		w.AddFailure(rules.Failure{ | ||||
| 		w.AddFailure(rule.Failure{ | ||||
| 			Failure:  failure, | ||||
| 			Type:     rules.FailureTypeWarning, | ||||
| 			Type:     rule.FailureTypeWarning, | ||||
| 			Position: w.GetPosition(node.Else.Pos(), node.Else.End()), | ||||
| 		}) | ||||
| 	} | ||||
| @@ -1,10 +1,10 @@ | ||||
| package formatters | ||||
| package formatter | ||||
| 
 | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"fmt" | ||||
| 
 | ||||
| 	"github.com/mgechev/revive/rules" | ||||
| 	"github.com/mgechev/revive/rule" | ||||
| 	"github.com/olekukonko/tablewriter" | ||||
| 	"github.com/ttacon/chalk" | ||||
| ) | ||||
| @@ -20,25 +20,25 @@ type CLIFormatter struct { | ||||
| 	Metadata FormatterMetadata | ||||
| } | ||||
| 
 | ||||
| func formatFailure(failure rules.Failure) []string { | ||||
| func formatFailure(failure rule.Failure) []string { | ||||
| 	fString := chalk.Blue.Color(failure.Failure) | ||||
| 	fTypeStr := string(failure.Type) | ||||
| 	fType := chalk.Red.Color(fTypeStr) | ||||
| 	lineColumn := failure.Position | ||||
| 	pos := chalk.Dim.TextStyle(fmt.Sprintf("(%d, %d)", lineColumn.Start.Line, lineColumn.Start.Column)) | ||||
| 	if failure.Type == rules.FailureTypeWarning { | ||||
| 	if failure.Type == rule.FailureTypeWarning { | ||||
| 		fType = chalk.Yellow.Color(fTypeStr) | ||||
| 	} | ||||
| 	return []string{failure.GetFilename(), pos, fType, fString} | ||||
| } | ||||
| 
 | ||||
| // Format formats the failures gotten from the linter. | ||||
| func (f *CLIFormatter) Format(failures []rules.Failure) (string, error) { | ||||
| func (f *CLIFormatter) Format(failures []rule.Failure) (string, error) { | ||||
| 	var result [][]string | ||||
| 	var totalErrors = 0 | ||||
| 	for _, f := range failures { | ||||
| 		result = append(result, formatFailure(f)) | ||||
| 		if f.Type == rules.FailureTypeError { | ||||
| 		if f.Type == rule.FailureTypeError { | ||||
| 			totalErrors++ | ||||
| 		} | ||||
| 	} | ||||
| @@ -1,6 +1,6 @@ | ||||
| package formatters | ||||
| package formatter | ||||
| 
 | ||||
| import "github.com/mgechev/revive/rules" | ||||
| import "github.com/mgechev/revive/rule" | ||||
| 
 | ||||
| // FormatterMetadata configuration of a formatter | ||||
| type FormatterMetadata struct { | ||||
| @@ -11,5 +11,5 @@ type FormatterMetadata struct { | ||||
| 
 | ||||
| // Formatter defines an interface for failure formatters | ||||
| type Formatter interface { | ||||
| 	Format([]rules.Failure) string | ||||
| 	Format([]rule.Failure) string | ||||
| } | ||||
| @@ -1,9 +1,9 @@ | ||||
| package formatters | ||||
| package formatter | ||||
| 
 | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| 
 | ||||
| 	"github.com/mgechev/revive/rules" | ||||
| 	"github.com/mgechev/revive/rule" | ||||
| ) | ||||
| 
 | ||||
| // JSONFormatter is an implementation of the Formatter interface | ||||
| @@ -13,7 +13,7 @@ type JSONFormatter struct { | ||||
| } | ||||
| 
 | ||||
| // Format formats the failures gotten from the linter. | ||||
| func (f *JSONFormatter) Format(failures []rules.Failure) (string, error) { | ||||
| func (f *JSONFormatter) Format(failures []rule.Failure) (string, error) { | ||||
| 	result, error := json.Marshal(failures) | ||||
| 	if error != nil { | ||||
| 		return "", error | ||||
| @@ -4,7 +4,7 @@ import ( | ||||
| 	"go/token" | ||||
|  | ||||
| 	"github.com/mgechev/revive/file" | ||||
| 	"github.com/mgechev/revive/rules" | ||||
| 	"github.com/mgechev/revive/rule" | ||||
| ) | ||||
|  | ||||
| // ReadFile defines an abstraction for reading files. | ||||
| @@ -20,10 +20,10 @@ func New(reader ReadFile) Linter { | ||||
| 	return Linter{reader: reader} | ||||
| } | ||||
|  | ||||
| // Lint lints a set of files with the specified rules. | ||||
| func (l *Linter) Lint(filenames []string, ruleSet []rules.Rule) ([]rules.Failure, error) { | ||||
| // Lint lints a set of files with the specified rule. | ||||
| func (l *Linter) Lint(filenames []string, ruleSet []rule.Rule) ([]rule.Failure, error) { | ||||
| 	var fileSet token.FileSet | ||||
| 	var failures []rules.Failure | ||||
| 	var failures []rule.Failure | ||||
| 	for _, filename := range filenames { | ||||
| 		content, err := l.reader(filename) | ||||
| 		if err != nil { | ||||
|   | ||||
							
								
								
									
										12
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								main.go
									
									
									
									
									
								
							| @@ -3,10 +3,10 @@ package main | ||||
| import ( | ||||
| 	"fmt" | ||||
|  | ||||
| 	"github.com/mgechev/revive/defaultrules" | ||||
| 	"github.com/mgechev/revive/formatters" | ||||
| 	"github.com/mgechev/revive/defaultrule" | ||||
| 	"github.com/mgechev/revive/formatter" | ||||
| 	"github.com/mgechev/revive/linter" | ||||
| 	"github.com/mgechev/revive/rules" | ||||
| 	"github.com/mgechev/revive/rule" | ||||
| ) | ||||
|  | ||||
| func main() { | ||||
| @@ -25,15 +25,15 @@ func main() { | ||||
| 	linter := linter.New(func(file string) ([]byte, error) { | ||||
| 		return []byte(src), nil | ||||
| 	}) | ||||
| 	var result []rules.Rule | ||||
| 	result = append(result, &defaultrules.LintElseRule{}) | ||||
| 	var result []rule.Rule | ||||
| 	result = append(result, &defaultrule.LintElseRule{}) | ||||
|  | ||||
| 	failures, err := linter.Lint([]string{"foo.go", "bar.go", "baz.go"}, result) | ||||
| 	if err != nil { | ||||
| 		panic(err) | ||||
| 	} | ||||
|  | ||||
| 	var formatter formatters.CLIFormatter | ||||
| 	var formatter formatter.CLIFormatter | ||||
| 	output, err := formatter.Format(failures) | ||||
| 	if err != nil { | ||||
| 		panic(err) | ||||
|   | ||||
| @@ -1,4 +1,4 @@ | ||||
| package rules | ||||
| package rule | ||||
| 
 | ||||
| import ( | ||||
| 	"go/token" | ||||
| @@ -1,36 +1,36 @@ | ||||
| package visitors | ||||
| package visitor | ||||
| 
 | ||||
| import ( | ||||
| 	"go/token" | ||||
| 
 | ||||
| 	"github.com/mgechev/revive/file" | ||||
| 	"github.com/mgechev/revive/rules" | ||||
| 	"github.com/mgechev/revive/rule" | ||||
| ) | ||||
| 
 | ||||
| // RuleVisitor defines a struct for a visitor. | ||||
| type RuleVisitor struct { | ||||
| 	SyntaxVisitor | ||||
| 	RuleName      string | ||||
| 	RuleArguments rules.RuleArguments | ||||
| 	failures      []rules.Failure | ||||
| 	RuleArguments rule.RuleArguments | ||||
| 	failures      []rule.Failure | ||||
| 	File          *file.File | ||||
| } | ||||
| 
 | ||||
| // AddFailure adds a failure to the ist of failures. | ||||
| func (w *RuleVisitor) AddFailure(failure rules.Failure) { | ||||
| func (w *RuleVisitor) AddFailure(failure rule.Failure) { | ||||
| 	w.failures = append(w.failures, failure) | ||||
| } | ||||
| 
 | ||||
| // GetFailures returns the list of failures. | ||||
| func (w *RuleVisitor) GetFailures() []rules.Failure { | ||||
| func (w *RuleVisitor) GetFailures() []rule.Failure { | ||||
| 	return w.failures | ||||
| } | ||||
| 
 | ||||
| // GetPosition returns position by given start and end token.Pos. | ||||
| func (w *RuleVisitor) GetPosition(start token.Pos, end token.Pos) rules.FailurePosition { | ||||
| func (w *RuleVisitor) GetPosition(start token.Pos, end token.Pos) rule.FailurePosition { | ||||
| 	s := w.File.ToPosition(start) | ||||
| 	e := w.File.ToPosition(end) | ||||
| 	return rules.FailurePosition{ | ||||
| 	return rule.FailurePosition{ | ||||
| 		Start: s, | ||||
| 		End:   e, | ||||
| 	} | ||||
| @@ -1,15 +1,15 @@ | ||||
| package visitors | ||||
| package visitor | ||||
| 
 | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"reflect" | ||||
| 
 | ||||
| 	"github.com/mgechev/revive/file" | ||||
| 	"github.com/mgechev/revive/rules" | ||||
| 	"github.com/mgechev/revive/rule" | ||||
| ) | ||||
| 
 | ||||
| // Setup sets the proper pointers of given visitor. | ||||
| func Setup(v interface{}, conf rules.RuleConfig, file *file.File) error { | ||||
| func Setup(v interface{}, conf rule.RuleConfig, file *file.File) error { | ||||
| 	val := reflect.ValueOf(v).Elem() | ||||
| 	field := val.FieldByName("RuleVisitor") | ||||
| 	if !field.IsValid() { | ||||
| @@ -1,4 +1,4 @@ | ||||
| package visitors | ||||
| package visitor | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| @@ -1,4 +1,4 @@ | ||||
| package visitors | ||||
| package visitor | ||||
| 
 | ||||
| import "go/ast" | ||||
| 
 | ||||
		Reference in New Issue
	
	Block a user