all repos

clerk @ ed6cfe6

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
ast: store postings as values instead of pointers, 1 month ago
1
package linter
2
3
import (
4
	"olexsmir.xyz/clerk/internal/analyzer"
5
	"olexsmir.xyz/clerk/journal/ast"
6
)
7
8
const MultipleOmittedAmountsID RuleID = "multiple-omitted-amounts"
9
10
// MultipleOmittedAmounts flags entries where more than one posting has an ommited amount.
11
type MultipleOmittedAmounts struct{}
12
13
func (MultipleOmittedAmounts) ID() RuleID { return MultipleOmittedAmountsID }
14
func (m *MultipleOmittedAmounts) CheckJournal(an *analyzer.Analysis) []Find {
15
	var finds []Find
16
	for _, txn := range an.Transactions {
17
		finds = append(finds, m.check(txn.Postings)...)
18
	}
19
	for _, ptx := range an.PeriodicTransactions {
20
		finds = append(finds, m.check(ptx.Postings)...)
21
	}
22
	for _, atx := range an.AutomatedTransactions {
23
		finds = append(finds, m.check(atx.Postings)...)
24
	}
25
	return finds
26
}
27
28
func (m *MultipleOmittedAmounts) check(postings []ast.Posting) []Find {
29
	var finds []Find
30
	for _, p := range postings {
31
		if p.Amount == nil && p.Balance == nil {
32
			finds = append(finds, Find{
33
				Code:    m.ID(),
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
}