all repos

clerk @ f9a2588

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
linter: refactor to use semantic context, 1 month ago
1
package linter
2
3
import (
4
	"fmt"
5
6
	"olexsmir.xyz/clerk/journal/semantic"
7
)
8
9
// DuplicatedCommodity flags commodity declarations that appear more than once.
10
type DuplicatedCommodity struct{}
11
12
func (DuplicatedCommodity) ID() RuleID         { return "duplicated-commodity" }
13
func (DuplicatedCommodity) Severity() Severity { return SeverityWarning }
14
func (d *DuplicatedCommodity) CheckJournal(ctx *semantic.Context) []Find {
15
	var finds []Find
16
	for sym, info := range ctx.Commodities {
17
		if len(info.Directives) <= 1 {
18
			continue
19
		}
20
		for _, cd := range info.Directives {
21
			finds = append(finds, Find{
22
				Code:     d.ID(),
23
				Severity: d.Severity(),
24
				Message:  fmt.Sprintf("duplicated commodity declaration: %s", sym),
25
				Span:     cd.Span,
26
			})
27
		}
28
	}
29
	return finds
30
}