|
| 1 | +// Copyright (c) 2025 Felipe Pena. All rights reserved. |
| 2 | +// Use of this source code is governed by an MIT license that can be found in the LICENSE file. |
| 3 | +module main |
| 4 | + |
| 5 | +import v.ast |
| 6 | +import v.token |
| 7 | +import arrays |
| 8 | + |
| 9 | +// cutoffs |
| 10 | +const indexexpr_cutoff = 10 |
| 11 | +const infixexpr_cutoff = 10 |
| 12 | +const selectorexpr_cutoff = 10 |
| 13 | +const callexpr_cutoff = 10 |
| 14 | +const stringinterliteral_cutoff = 10 |
| 15 | +const stringliteral_cutoff = 10 |
| 16 | +const ascast_cutoff = 10 |
| 17 | +const stringconcat_cutoff = 10 |
| 18 | + |
| 19 | +// minimum size for string literals |
| 20 | +const stringliteral_min_size = 20 |
| 21 | + |
| 22 | +// long functions cutoff |
| 23 | +const long_fns_cutoff = 300 |
| 24 | + |
| 25 | +struct VetAnalyze { |
| 26 | +mut: |
| 27 | + repeated_expr_cutoff shared map[string]int // repeated code cutoff |
| 28 | + repeated_expr shared map[string]map[string]map[string][]token.Pos // repeated exprs in fn scope |
| 29 | + cur_fn ast.FnDecl // current fn declaration |
| 30 | +} |
| 31 | + |
| 32 | +// stmt checks for repeated code in statements |
| 33 | +fn (mut vt VetAnalyze) stmt(vet &Vet, stmt ast.Stmt) { |
| 34 | + match stmt { |
| 35 | + ast.AssignStmt { |
| 36 | + if stmt.op == .plus_assign { |
| 37 | + if stmt.right[0] in [ast.StringLiteral, ast.StringInterLiteral] { |
| 38 | + vt.save_expr(stringconcat_cutoff, '${stmt.left[0].str()} += ${stmt.right[0].str()}', |
| 39 | + vet.file, stmt.pos) |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + else {} |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// save_expr registers a repeated code occurrence |
| 48 | +fn (mut vt VetAnalyze) save_expr(cutoff int, expr string, file string, pos token.Pos) { |
| 49 | + lock vt.repeated_expr { |
| 50 | + vt.repeated_expr[vt.cur_fn.name][expr][file] << pos |
| 51 | + } |
| 52 | + lock vt.repeated_expr_cutoff { |
| 53 | + vt.repeated_expr_cutoff[expr] = cutoff |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// exprs checks for repeated code in expressions |
| 58 | +fn (mut vt VetAnalyze) exprs(vet &Vet, exprs []ast.Expr) { |
| 59 | + for expr in exprs { |
| 60 | + vt.expr(vet, expr) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// expr checks for repeated code |
| 65 | +fn (mut vt VetAnalyze) expr(vet &Vet, expr ast.Expr) { |
| 66 | + match expr { |
| 67 | + ast.InfixExpr { |
| 68 | + vt.save_expr(infixexpr_cutoff, '${expr.left} ${expr.op} ${expr.right}', vet.file, |
| 69 | + expr.pos) |
| 70 | + } |
| 71 | + ast.IndexExpr { |
| 72 | + vt.save_expr(indexexpr_cutoff, '${expr.left}[${expr.index}]', vet.file, expr.pos) |
| 73 | + } |
| 74 | + ast.SelectorExpr { |
| 75 | + // nested selectors |
| 76 | + if expr.expr !is ast.Ident { |
| 77 | + vt.save_expr(selectorexpr_cutoff, '${expr.expr.str()}.${expr.field_name}', |
| 78 | + vet.file, expr.pos) |
| 79 | + } |
| 80 | + } |
| 81 | + ast.CallExpr { |
| 82 | + if expr.is_static_method || expr.is_method { |
| 83 | + vt.save_expr(callexpr_cutoff, '${expr.left}.${expr.name}(${expr.args.map(it.str()).join(', ')})', |
| 84 | + vet.file, expr.pos) |
| 85 | + } else { |
| 86 | + vt.save_expr(callexpr_cutoff, '${expr.name}(${expr.args.map(it.str()).join(', ')})', |
| 87 | + vet.file, expr.pos) |
| 88 | + } |
| 89 | + } |
| 90 | + ast.AsCast { |
| 91 | + vt.save_expr(ascast_cutoff, ast.Expr(expr).str(), vet.file, expr.pos) |
| 92 | + } |
| 93 | + ast.StringLiteral { |
| 94 | + if expr.val.len > stringliteral_min_size { |
| 95 | + vt.save_expr(stringliteral_cutoff, ast.Expr(expr).str(), vet.file, expr.pos) |
| 96 | + } |
| 97 | + } |
| 98 | + ast.StringInterLiteral { |
| 99 | + vt.save_expr(stringinterliteral_cutoff, ast.Expr(expr).str(), vet.file, expr.pos) |
| 100 | + } |
| 101 | + else {} |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// long_or_empty_fns checks for long or empty functions |
| 106 | +fn (mut vt VetAnalyze) long_or_empty_fns(mut vet Vet, fn_decl ast.FnDecl) { |
| 107 | + nr_lines := if fn_decl.stmts.len == 0 { |
| 108 | + 0 |
| 109 | + } else { |
| 110 | + fn_decl.stmts.last().pos.line_nr - fn_decl.pos.line_nr |
| 111 | + } |
| 112 | + if nr_lines > long_fns_cutoff { |
| 113 | + vet.notice('Long function - ${nr_lines} lines long.', fn_decl.pos.line_nr, .long_fns) |
| 114 | + } else if nr_lines == 0 { |
| 115 | + vet.notice('Empty function.', fn_decl.pos.line_nr, .empty_fn) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// vet_fn_analysis reports repeated code by scope |
| 120 | +fn (mut vt VetAnalyze) vet_repeated_code(mut vet Vet) { |
| 121 | + rlock vt.repeated_expr { |
| 122 | + for fn_name, ref_expr in vt.repeated_expr { |
| 123 | + scope_name := if fn_name == '' { 'global scope' } else { 'function scope (${fn_name})' } |
| 124 | + for expr, info in ref_expr { |
| 125 | + occurrences := arrays.sum(info.values().map(it.len)) or { 0 } |
| 126 | + if occurrences < vt.repeated_expr_cutoff[expr] { |
| 127 | + continue |
| 128 | + } |
| 129 | + for file, info_pos in info { |
| 130 | + for k, pos in info_pos { |
| 131 | + vet.notice_with_file(file, '${expr} occurs ${k + 1}/${occurrences} times in ${scope_name}.', |
| 132 | + pos.line_nr, .repeated_code) |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +// vet_code_analyze performs code analysis |
| 141 | +fn (mut vt Vet) vet_code_analyze() { |
| 142 | + if vt.opt.repeated_code { |
| 143 | + vt.analyze.vet_repeated_code(mut vt) |
| 144 | + } |
| 145 | +} |
0 commit comments