all repos

clerk @ ba9be60442c067fdfb83f50a85a2f6d4cc6d3150

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
linter: add "more than one posting has omitted amount" rule, 1 month ago
1
package linter
2
3
import "olexsmir.xyz/clerk/journal/ast"
4
5
// MultipleOmittedAmounts flags entries where more than one posting has an ommited amount.
6
type MultipleOmittedAmounts struct{}
7
8
func (MultipleOmittedAmounts) ID() RuleID         { return "multiple-omitted-amounts" }
9
func (MultipleOmittedAmounts) Severity() Severity { return SeverityError }
10
func (m *MultipleOmittedAmounts) CheckEntry(entry ast.Entry) []Find {
11
	switch e := entry.(type) {
12
	case *ast.Transaction:
13
		return m.check(e.Postings)
14
	case *ast.PeriodicTransaction:
15
		return m.check(e.Postings)
16
	case *ast.AutomatedTransaction:
17
		return m.check(e.Postings)
18
	default:
19
		return nil
20
	}
21
}
22
23
func (m *MultipleOmittedAmounts) check(postings []*ast.Posting) []Find {
24
	var finds []Find
25
	for _, p := range postings {
26
		// skipping postings with balance assertion, since those are often used as reconciliation entries
27
		if p.Amount == nil && p.Balance == nil {
28
			finds = append(finds, Find{
29
				Code:     m.ID(),
30
				Severity: m.Severity(),
31
				Message:  "more than one posting has omitted amount",
32
				Span:     p.Span,
33
			})
34
		}
35
	}
36
	if len(finds) > 1 {
37
		return finds
38
	}
39
	return nil
40
}