2024-04-20 10:20:56 +02:00
|
|
|
package rule
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/mgechev/revive/lint"
|
|
|
|
)
|
|
|
|
|
2024-12-01 17:44:41 +02:00
|
|
|
// CommentsDensityRule enforces a minimum comment / code relation.
|
2024-04-20 10:20:56 +02:00
|
|
|
type CommentsDensityRule struct {
|
2024-07-11 09:56:42 +02:00
|
|
|
minimumCommentsDensity int64
|
2024-11-15 13:03:59 +02:00
|
|
|
|
|
|
|
configureOnce sync.Once
|
2024-04-20 10:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const defaultMinimumCommentsPercentage = 0
|
|
|
|
|
|
|
|
func (r *CommentsDensityRule) configure(arguments lint.Arguments) {
|
|
|
|
if len(arguments) < 1 {
|
2024-07-11 09:56:42 +02:00
|
|
|
r.minimumCommentsDensity = defaultMinimumCommentsPercentage
|
2024-04-20 10:20:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var ok bool
|
2024-07-11 09:56:42 +02:00
|
|
|
r.minimumCommentsDensity, ok = arguments[0].(int64)
|
2024-04-20 10:20:56 +02:00
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("invalid argument for %q rule: argument should be an int, got %T", r.Name(), arguments[0]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply applies the rule to given file.
|
|
|
|
func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
|
2024-11-15 13:03:59 +02:00
|
|
|
r.configureOnce.Do(func() { r.configure(arguments) })
|
2024-04-20 10:20:56 +02:00
|
|
|
|
|
|
|
commentsLines := countDocLines(file.AST.Comments)
|
|
|
|
statementsCount := countStatements(file.AST)
|
|
|
|
density := (float32(commentsLines) / float32(statementsCount+commentsLines)) * 100
|
|
|
|
|
2024-07-11 09:56:42 +02:00
|
|
|
if density < float32(r.minimumCommentsDensity) {
|
2024-04-20 10:20:56 +02:00
|
|
|
return []lint.Failure{
|
|
|
|
{
|
|
|
|
Node: file.AST,
|
|
|
|
Confidence: 1,
|
2024-11-04 14:18:17 +02:00
|
|
|
Failure: fmt.Sprintf("the file has a comment density of %2.f%% (%d comment lines for %d code lines) but expected a minimum of %d%%",
|
|
|
|
density, commentsLines, statementsCount, r.minimumCommentsDensity),
|
2024-04-20 10:20:56 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the rule name.
|
|
|
|
func (*CommentsDensityRule) Name() string {
|
|
|
|
return "comments-density"
|
|
|
|
}
|
|
|
|
|
|
|
|
// countStatements counts the number of program statements in the given AST.
|
|
|
|
func countStatements(node ast.Node) int {
|
|
|
|
counter := 0
|
|
|
|
|
|
|
|
ast.Inspect(node, func(n ast.Node) bool {
|
|
|
|
switch n.(type) {
|
|
|
|
case *ast.ExprStmt, *ast.AssignStmt, *ast.ReturnStmt, *ast.GoStmt, *ast.DeferStmt,
|
|
|
|
*ast.BranchStmt, *ast.IfStmt, *ast.SwitchStmt, *ast.TypeSwitchStmt,
|
|
|
|
*ast.SelectStmt, *ast.ForStmt, *ast.RangeStmt, *ast.CaseClause, *ast.CommClause,
|
|
|
|
*ast.DeclStmt, *ast.FuncDecl:
|
|
|
|
counter++
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return counter
|
|
|
|
}
|
|
|
|
|
|
|
|
func countDocLines(comments []*ast.CommentGroup) int {
|
|
|
|
acc := 0
|
|
|
|
for _, c := range comments {
|
|
|
|
lines := strings.Split(c.Text(), "\n")
|
|
|
|
acc += len(lines) - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc
|
|
|
|
}
|