all repos

clerk @ e2e9e2b27783c07fe5724268bf7ae828b3aacac7

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
improve analyzer, 1 day ago
1
package linter
2
3
import (
4
	"olexsmir.xyz/clerk/internal/analyzer"
5
	"olexsmir.xyz/clerk/journal/ast"
6
)
7
8
// MultipleOmittedAmounts flags entries where more than one posting has an ommited amount.
9
type MultipleOmittedAmounts struct{}
10
11
func (MultipleOmittedAmounts) ID() RuleID         { return "multiple-omitted-amounts" }
12
func (MultipleOmittedAmounts) Severity() Severity { return SeverityError }
13
func (m *MultipleOmittedAmounts) CheckJournal(an *analyzer.Analysis) []Find {
14
	var finds []Find
15
	for _, txn := range an.Transactions {
16
		finds = append(finds, m.check(txn.Postings)...)
17
	}
18
	for _, ptx := range an.PeriodicTransactions {
19
		finds = append(finds, m.check(ptx.Postings)...)
20
	}
21
	for _, atx := range an.AutomatedTransactions {
22
		finds = append(finds, m.check(atx.Postings)...)
23
	}
24
	return finds
25
}
26
27
func (m *MultipleOmittedAmounts) check(postings []*ast.Posting) []Find {
28
	var finds []Find
29
	for _, p := range postings {
30
		if p.Amount == nil && p.Balance == nil {
31
			finds = append(finds, Find{
32
				Code:     m.ID(),
33
				Severity: m.Severity(),
34
				Message:  "more than one posting has omitted amount",
35
				Span:     p.Span,
36
			})
37
		}
38
	}
39
	if len(finds) > 1 {
40
		return finds
41
	}
42
	return nil
43
}