clerk/internal/linter/rule_orderdate.go (view raw)
Oleksandr Smirnov
Oleksandr Smirnov
olexsmir@gmail.com extact all ast.Date to string code into .String() method, 1 month ago
olexsmir@gmail.com extact all ast.Date to string code into .String() method, 1 month 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)", txn.Date, *anchor), |
| 29 | Span: txn.Date.Span, |
| 30 | }) |
| 31 | continue |
| 32 | } |
| 33 | anchor = &txn.Date |
| 34 | } |
| 35 | } |
| 36 | return finds |
| 37 | } |