clerk/internal/linter/rule_account_depth.go (view raw)
| 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 (AccountDepthLimit) Description() string { return "account name depth exceeds limit" } |
| 17 | func (a *AccountDepthLimit) CheckEntry(entry ast.Entry) []Find { |
| 18 | var finds []Find |
| 19 | switch e := entry.(type) { |
| 20 | case *ast.Transaction: |
| 21 | for _, p := range e.Postings { |
| 22 | a.check(&finds, p.Account) |
| 23 | } |
| 24 | case *ast.PeriodicTransaction: |
| 25 | for _, p := range e.Postings { |
| 26 | a.check(&finds, p.Account) |
| 27 | } |
| 28 | case *ast.AutomatedTransaction: |
| 29 | for _, p := range e.Postings { |
| 30 | a.check(&finds, p.Account) |
| 31 | } |
| 32 | case *ast.AccountDirective: |
| 33 | a.check(&finds, e.Account) |
| 34 | case *ast.AliasDirective: |
| 35 | a.check(&finds, e.From) |
| 36 | a.check(&finds, e.To) |
| 37 | } |
| 38 | return finds |
| 39 | } |
| 40 | |
| 41 | func (a *AccountDepthLimit) check(finds *[]Find, acc ast.Account) { |
| 42 | if depth := len(acc.Name); depth > a.MaxDepth { |
| 43 | *finds = append(*finds, Find{ |
| 44 | Code: a.ID(), |
| 45 | Severity: a.Severity(), |
| 46 | Message: fmt.Sprintf("account %q depth (%d) exceeds max allowed depth (%d)", |
| 47 | acc.String(), depth, a.MaxDepth), |
| 48 | Span: acc.Span, |
| 49 | }) |
| 50 | } |
| 51 | } |