mirror of
https://github.com/mgechev/revive.git
synced 2025-02-03 13:11:25 +02:00
simpler implem of binEpr complexity computation
This commit is contained in:
parent
e842146006
commit
777e1b9e60
@ -134,65 +134,62 @@ func (v *cognitiveComplexityVisitor) walk(complexityIncrement int, targets ...as
|
||||
}
|
||||
|
||||
func (cognitiveComplexityVisitor) binExpComplexity(n *ast.BinaryExpr) int {
|
||||
calculator := binExprComplexityCalculator{currentOp: []token.Token{}}
|
||||
calculator := binExprComplexityCalculator{opsStack: []token.Token{}}
|
||||
|
||||
astutil.Apply(n, calculator.pre(), calculator.post())
|
||||
astutil.Apply(n, calculator.pre, calculator.post)
|
||||
|
||||
return calculator.complexity
|
||||
}
|
||||
|
||||
type binExprComplexityCalculator struct {
|
||||
complexity int
|
||||
currentOp []token.Token // stack of bool operators
|
||||
opsStack []token.Token // stack of bool operators
|
||||
subexpStarted bool
|
||||
}
|
||||
|
||||
func (becc *binExprComplexityCalculator) pre() astutil.ApplyFunc {
|
||||
return func(c *astutil.Cursor) bool {
|
||||
switch n := c.Node().(type) {
|
||||
case *ast.BinaryExpr:
|
||||
isBoolOp := n.Op == token.LAND || n.Op == token.LOR
|
||||
if !isBoolOp {
|
||||
break
|
||||
}
|
||||
|
||||
ops := len(becc.currentOp)
|
||||
// if
|
||||
// is the first boolop in the expression OR
|
||||
// is the first boolop inside a subexpression (...) OR
|
||||
// is not the same to the previous one
|
||||
// then
|
||||
// increment complexity
|
||||
if ops == 0 || becc.subexpStarted || n.Op != becc.currentOp[ops-1] {
|
||||
becc.complexity++
|
||||
becc.subexpStarted = false
|
||||
}
|
||||
|
||||
becc.currentOp = append(becc.currentOp, n.Op)
|
||||
case *ast.ParenExpr:
|
||||
becc.subexpStarted = true
|
||||
func (becc *binExprComplexityCalculator) pre(c *astutil.Cursor) bool {
|
||||
switch n := c.Node().(type) {
|
||||
case *ast.BinaryExpr:
|
||||
isBoolOp := n.Op == token.LAND || n.Op == token.LOR
|
||||
if !isBoolOp {
|
||||
break
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func (becc *binExprComplexityCalculator) post() astutil.ApplyFunc {
|
||||
return func(c *astutil.Cursor) bool {
|
||||
switch n := c.Node().(type) {
|
||||
case *ast.BinaryExpr:
|
||||
isBoolOp := n.Op == token.LAND || n.Op == token.LOR
|
||||
if !isBoolOp {
|
||||
break
|
||||
}
|
||||
|
||||
ops := len(becc.currentOp)
|
||||
if ops > 0 {
|
||||
becc.currentOp = becc.currentOp[:ops-1]
|
||||
}
|
||||
case *ast.ParenExpr:
|
||||
ops := len(becc.opsStack)
|
||||
// if
|
||||
// is the first boolop in the expression OR
|
||||
// is the first boolop inside a subexpression (...) OR
|
||||
// is not the same to the previous one
|
||||
// then
|
||||
// increment complexity
|
||||
if ops == 0 || becc.subexpStarted || n.Op != becc.opsStack[ops-1] {
|
||||
becc.complexity++
|
||||
becc.subexpStarted = false
|
||||
}
|
||||
return true
|
||||
|
||||
becc.opsStack = append(becc.opsStack, n.Op)
|
||||
case *ast.ParenExpr:
|
||||
becc.subexpStarted = true
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (becc *binExprComplexityCalculator) post(c *astutil.Cursor) bool {
|
||||
switch n := c.Node().(type) {
|
||||
case *ast.BinaryExpr:
|
||||
isBoolOp := n.Op == token.LAND || n.Op == token.LOR
|
||||
if !isBoolOp {
|
||||
break
|
||||
}
|
||||
|
||||
ops := len(becc.opsStack)
|
||||
if ops > 0 {
|
||||
becc.opsStack = becc.opsStack[:ops-1]
|
||||
}
|
||||
case *ast.ParenExpr:
|
||||
becc.subexpStarted = false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user