|
| 1 | +package rule |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "go/ast" |
| 6 | + "strings" |
| 7 | + "sync" |
| 8 | + |
| 9 | + "github.com/mgechev/revive/lint" |
| 10 | +) |
| 11 | + |
| 12 | +// CommentsDensityRule lints given else constructs. |
| 13 | +type CommentsDensityRule struct { |
| 14 | + minumumCommentsDensity int64 |
| 15 | + configured bool |
| 16 | + sync.Mutex |
| 17 | +} |
| 18 | + |
| 19 | +const defaultMinimumCommentsPercentage = 0 |
| 20 | + |
| 21 | +func (r *CommentsDensityRule) configure(arguments lint.Arguments) { |
| 22 | + r.Lock() |
| 23 | + defer r.Unlock() |
| 24 | + |
| 25 | + if !r.configured { |
| 26 | + r.configured = true |
| 27 | + if len(arguments) < 1 { |
| 28 | + r.minumumCommentsDensity = defaultMinimumCommentsPercentage |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + var ok bool |
| 33 | + r.minumumCommentsDensity, ok = arguments[0].(int64) |
| 34 | + if !ok { |
| 35 | + panic(fmt.Sprintf("invalid argument for %q rule: argument should be an int, got %T", r.Name(), arguments[0])) |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// Apply applies the rule to given file. |
| 41 | +func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { |
| 42 | + r.configure(arguments) |
| 43 | + |
| 44 | + commentsLines := countDocLines(file.AST.Comments) |
| 45 | + statementsCount := countStatements(file.AST) |
| 46 | + density := (float32(commentsLines) / float32(statementsCount+commentsLines)) * 100 |
| 47 | + |
| 48 | + if density < float32(r.minumumCommentsDensity) { |
| 49 | + return []lint.Failure{ |
| 50 | + { |
| 51 | + Node: file.AST, |
| 52 | + Confidence: 1, |
| 53 | + 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.minumumCommentsDensity), |
| 54 | + }, |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return nil |
| 59 | +} |
| 60 | + |
| 61 | +// Name returns the rule name. |
| 62 | +func (*CommentsDensityRule) Name() string { |
| 63 | + return "comments-density" |
| 64 | +} |
| 65 | + |
| 66 | +// countStatements counts the number of program statements in the given AST. |
| 67 | +func countStatements(node ast.Node) int { |
| 68 | + counter := 0 |
| 69 | + |
| 70 | + ast.Inspect(node, func(n ast.Node) bool { |
| 71 | + switch n.(type) { |
| 72 | + case *ast.ExprStmt, *ast.AssignStmt, *ast.ReturnStmt, *ast.GoStmt, *ast.DeferStmt, |
| 73 | + *ast.BranchStmt, *ast.IfStmt, *ast.SwitchStmt, *ast.TypeSwitchStmt, |
| 74 | + *ast.SelectStmt, *ast.ForStmt, *ast.RangeStmt, *ast.CaseClause, *ast.CommClause, |
| 75 | + *ast.DeclStmt, *ast.FuncDecl: |
| 76 | + counter++ |
| 77 | + } |
| 78 | + return true |
| 79 | + }) |
| 80 | + |
| 81 | + return counter |
| 82 | +} |
| 83 | + |
| 84 | +func countDocLines(comments []*ast.CommentGroup) int { |
| 85 | + acc := 0 |
| 86 | + for _, c := range comments { |
| 87 | + lines := strings.Split(c.Text(), "\n") |
| 88 | + acc += len(lines) - 1 |
| 89 | + } |
| 90 | + |
| 91 | + return acc |
| 92 | +} |
0 commit comments