mirror of
https://github.com/mgechev/revive.git
synced 2025-07-15 01:04:40 +02:00
Refactoring: move pick function to rule/utils.go (#30)
* Adds rule superfluous-else (an extension of indent-error-flow) * Fix superfluous-else rule struct namming. * Adds superfuous-else rule to the rules table * Adds confusing-naming rule * adds multifile test * [WIP] fix multiple file test * draft solution for detecting confusing-names through multiple files * [WIP] confusing-name multiple files * clean-up * draft working version * cleaner version + more informative messages * adds check on struct field names * fix config.go * clean master * new ADS rule: newerr * ADS-print working version * ads-print final version * ads-lost-err working version * confusing-namming: fix tests * moves pick to rule/utils for better reusability
This commit is contained in:
@ -233,50 +233,6 @@ func markParamAsUsed(id *ast.Ident, v funcVisitor) { // TODO: constraint paramet
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type picker struct {
|
|
||||||
fselect func(n ast.Node) bool
|
|
||||||
onSelect func(n ast.Node)
|
|
||||||
}
|
|
||||||
|
|
||||||
func pick(n ast.Node, fselect func(n ast.Node) bool, f func(n ast.Node) []ast.Node) []ast.Node {
|
|
||||||
var result []ast.Node
|
|
||||||
|
|
||||||
if n == nil {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
if f == nil {
|
|
||||||
f = func(n ast.Node) []ast.Node { return []ast.Node{n} }
|
|
||||||
}
|
|
||||||
|
|
||||||
onSelect := func(n ast.Node) {
|
|
||||||
result = append(result, f(n)...)
|
|
||||||
}
|
|
||||||
p := picker{fselect: fselect, onSelect: onSelect}
|
|
||||||
ast.Walk(p, n)
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func pickFromExpList(l []ast.Expr, fselect func(n ast.Node) bool, f func(n ast.Node) []ast.Node) []ast.Node {
|
|
||||||
result := make([]ast.Node, 0)
|
|
||||||
for _, e := range l {
|
|
||||||
result = append(result, pick(e, fselect, f)...)
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p picker) Visit(node ast.Node) ast.Visitor {
|
|
||||||
if p.fselect == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if p.fselect(node) {
|
|
||||||
p.onSelect(node)
|
|
||||||
}
|
|
||||||
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
func isOpAssign(aTok token.Token) bool {
|
func isOpAssign(aTok token.Token) bool {
|
||||||
return aTok == token.ADD_ASSIGN || aTok == token.AND_ASSIGN ||
|
return aTok == token.ADD_ASSIGN || aTok == token.AND_ASSIGN ||
|
||||||
aTok == token.MUL_ASSIGN || aTok == token.OR_ASSIGN ||
|
aTok == token.MUL_ASSIGN || aTok == token.OR_ASSIGN ||
|
||||||
|
@ -146,3 +146,51 @@ func srcLine(src []byte, p token.Position) string {
|
|||||||
}
|
}
|
||||||
return string(src[lo:hi])
|
return string(src[lo:hi])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pick yields a list of nodes by picking them from a sub-ast with root node n.
|
||||||
|
// Nodes are selected by applying the fselect function
|
||||||
|
// f function is applied to each selected node before inseting it in the final result.
|
||||||
|
// If f==nil then it defaults to the identity function (ie it returns the node itself)
|
||||||
|
func pick(n ast.Node, fselect func(n ast.Node) bool, f func(n ast.Node) []ast.Node) []ast.Node {
|
||||||
|
var result []ast.Node
|
||||||
|
|
||||||
|
if n == nil {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
if f == nil {
|
||||||
|
f = func(n ast.Node) []ast.Node { return []ast.Node{n} }
|
||||||
|
}
|
||||||
|
|
||||||
|
onSelect := func(n ast.Node) {
|
||||||
|
result = append(result, f(n)...)
|
||||||
|
}
|
||||||
|
p := picker{fselect: fselect, onSelect: onSelect}
|
||||||
|
ast.Walk(p, n)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func pickFromExpList(l []ast.Expr, fselect func(n ast.Node) bool, f func(n ast.Node) []ast.Node) []ast.Node {
|
||||||
|
result := make([]ast.Node, 0)
|
||||||
|
for _, e := range l {
|
||||||
|
result = append(result, pick(e, fselect, f)...)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
type picker struct {
|
||||||
|
fselect func(n ast.Node) bool
|
||||||
|
onSelect func(n ast.Node)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p picker) Visit(node ast.Node) ast.Visitor {
|
||||||
|
if p.fselect == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.fselect(node) {
|
||||||
|
p.onSelect(node)
|
||||||
|
}
|
||||||
|
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user