all repos

clerk @ 3fa39df

missing tooling for ledger/hledger

clerk/internal/linter/rule_account_depth.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
linter: remove Description() method, 1 month ago
1
package linter
2
3
import (
4
	"fmt"
5
6
	"olexsmir.xyz/clerk/journal/ast"
7
)
8
9
// AccountDepthLimit checks that account names don't exceed MaxDepth
10
type AccountDepthLimit struct {
11
	MaxDepth int
12
}
13
14
func (AccountDepthLimit) ID() RuleID          { return "account-depth" }
15
func (AccountDepthLimit) Severity() Severity  { return SeverityWarning }
16
func (a *AccountDepthLimit) CheckEntry(entry ast.Entry) []Find {
17
	var finds []Find
18
	switch e := entry.(type) {
19
	case *ast.Transaction:
20
		for _, p := range e.Postings {
21
			a.check(&finds, p.Account)
22
		}
23
	case *ast.PeriodicTransaction:
24
		for _, p := range e.Postings {
25
			a.check(&finds, p.Account)
26
		}
27
	case *ast.AutomatedTransaction:
28
		for _, p := range e.Postings {
29
			a.check(&finds, p.Account)
30
		}
31
	case *ast.AccountDirective:
32
		a.check(&finds, e.Account)
33
	case *ast.AliasDirective:
34
		a.check(&finds, e.From)
35
		a.check(&finds, e.To)
36
	}
37
	return finds
38
}
39
40
func (a *AccountDepthLimit) check(finds *[]Find, acc ast.Account) {
41
	if depth := len(acc.Name); depth > a.MaxDepth {
42
		*finds = append(*finds, Find{
43
			Code:     a.ID(),
44
			Severity: a.Severity(),
45
			Message: fmt.Sprintf("account %q depth (%d) exceeds max allowed depth (%d)",
46
				acc.String(), depth, a.MaxDepth),
47
			Span: acc.Span,
48
		})
49
	}
50
}