all repos

clerk @ f9e2eff3a36f0c8cd7ff7e49aee6cec9e9a09c7c

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
lsp: completion, 2 months ago
1
package linter
2
3
import (
4
	"fmt"
5
6
	"olexsmir.xyz/clerk/internal/analyzer"
7
	"olexsmir.xyz/clerk/journal/ast"
8
)
9
10
// OrderDate checks that transactions are in chronological order by date.
11
type OrderDate struct{}
12
13
func (OrderDate) ID() RuleID         { return "orderdate" }
14
func (OrderDate) Severity() Severity { return SeverityWarning }
15
func (o *OrderDate) CheckJournal(an *analyzer.Analysis) []Find {
16
	var finds []Find
17
	var anchor *ast.Date
18
	for _, pf := range an.Files {
19
		for _, entry := range pf.Ast.Entries {
20
			txn, ok := entry.(*ast.Transaction)
21
			if !ok {
22
				continue
23
			}
24
			if anchor != nil && txn.Date.Compare(*anchor) < 0 {
25
				finds = append(finds, Find{
26
					Code:     o.ID(),
27
					Severity: o.Severity(),
28
					Message:  fmt.Sprintf("transaction is out of chronological order (date %s before %s)", o.dateString(txn.Date), o.dateString(*anchor)),
29
					Span:     txn.Date.Span,
30
				})
31
				continue
32
			}
33
			anchor = &txn.Date
34
		}
35
	}
36
	return finds
37
}
38
39
func (o OrderDate) dateString(d ast.Date) string {
40
	return fmt.Sprintf("%d-%02d-%02d", d.Year, d.Month, d.Day)
41
}