all repos

clerk @ fd46af1

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
linter: add duplicated-commodity rule, 1 month ago
1
package linter
2
3
import "olexsmir.xyz/clerk/journal/ast"
4
5
// DuplicatedCommodity flags commodity declarations that appear more than once.
6
type DuplicatedCommodity struct{}
7
8
func (DuplicatedCommodity) ID() RuleID         { return "duplicated-commodity" }
9
func (DuplicatedCommodity) Severity() Severity { return SeverityWarning }
10
func (d *DuplicatedCommodity) CheckJournal(j *ast.Journal) []Find {
11
	var finds []Find
12
	seen := make(map[string]bool)
13
14
	for _, entry := range j.Entries {
15
		cd, ok := entry.(*ast.CommodityDirective)
16
		if !ok {
17
			continue
18
		}
19
20
		name := cd.Commodity
21
		if seen[name] {
22
			finds = append(finds, Find{
23
				Code:     d.ID(),
24
				Severity: d.Severity(),
25
				Message:  "duplicated commodity declaration: " + name,
26
				Span:     cd.Span,
27
			})
28
		}
29
30
		seen[name] = true
31
	}
32
33
	return finds
34
}