all repos

clerk @ 3bf8ce6

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
linter: add omitted-precision, 1 month ago
1
package linter
2
3
import "olexsmir.xyz/clerk/journal/ast"
4
5
// OmittedPrecision flags amounts with insufficient decimal precision (<2 digits).
6
type OmittedPrecision struct{}
7
8
func (OmittedPrecision) ID() RuleID          { return "omitted-precision" }
9
func (OmittedPrecision) Severity() Severity  { return SeverityWarning }
10
func (OmittedPrecision) Description() string { return "amount has insufficient precision" }
11
func (o *OmittedPrecision) 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.QuantityFmt.Precision < 2 {
23
			finds = append(finds, Find{
24
				Code:     o.ID(),
25
				Severity: o.Severity(),
26
				Message:  o.Description(),
27
				Span:     posting.Amount.Span,
28
			})
29
		}
30
	}
31
	return finds
32
}