all repos

clerk @ 42ed6f19392789706708b1171eb08e16905f6b8e

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
linter: add missing-commodity, 1 month ago
1
package linter
2
3
import "olexsmir.xyz/clerk/journal/ast"
4
5
// MissingCommodity flags amounts with a missing commodity.
6
type MissingCommodity struct{}
7
8
func (MissingCommodity) ID() RuleID          { return "missing-commodity" }
9
func (MissingCommodity) Severity() Severity  { return SeverityWarning }
10
func (MissingCommodity) Description() string { return "amount missing commodity" }
11
func (m *MissingCommodity) CheckEntry(entry ast.Entry) []Find {
12
	txn, ok := entry.(*ast.Transaction)
13
	if !ok || (txn.Postings != nil && len(txn.Postings) == 0) {
14
		return nil
15
	}
16
17
	var finds []Find
18
	for _, posting := range txn.Postings {
19
		if posting.Amount == nil {
20
			continue
21
		}
22
		if posting.Amount.Commodity == "" {
23
			finds = append(finds, Find{
24
				Code:     m.ID(),
25
				Severity: m.Severity(),
26
				Message:  m.Description(),
27
				Span:     posting.Amount.Span,
28
			})
29
		}
30
	}
31
	return finds
32
}