clerk/internal/linter/rule_orderdate.go (view raw)
| 1 | package linter |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "olexsmir.xyz/clerk/internal/analyzer" |
| 7 | "olexsmir.xyz/clerk/journal/ast" |
| 8 | ) |
| 9 | |
| 10 | const OrderDateID RuleID = "orderdate" |
| 11 | |
| 12 | // OrderDate checks that transactions are in chronological order by date. |
| 13 | type OrderDate struct{} |
| 14 | |
| 15 | func (OrderDate) ID() RuleID { return OrderDateID } |
| 16 | func (o *OrderDate) CheckJournal(an *analyzer.Analysis) []Find { |
| 17 | var finds []Find |
| 18 | var anchor *ast.Date |
| 19 | for _, pf := range an.Files { |
| 20 | for _, entry := range pf.Ast.Entries { |
| 21 | txn, ok := entry.(*ast.Transaction) |
| 22 | if !ok { |
| 23 | continue |
| 24 | } |
| 25 | if anchor != nil && txn.Date.Compare(*anchor) < 0 { |
| 26 | finds = append(finds, Find{ |
| 27 | Code: o.ID(), |
| 28 | Message: fmt.Sprintf("transaction is out of chronological order (date %s before %s)", txn.Date, *anchor), |
| 29 | Span: txn.Date.Span, |
| 30 | }) |
| 31 | continue |
| 32 | } |
| 33 | anchor = &txn.Date |
| 34 | } |
| 35 | } |
| 36 | return finds |
| 37 | } |