Repository navigation
Expand file tree
/
Copy pathlint.go
More file actions
78 lines (71 loc) · 2.98 KB
/
Copy pathlint.go
File metadata and controls
78 lines (71 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package gatekit
import "sort"
// LintKind classifies a lifecycle lint violation (core-spec §8).
type LintKind string
const (
// StaleSpec: a later spec has ActiveFrom <= Min, so this spec can never be the
// effective spec for any permitted active version. Delete it and its dead path.
StaleSpec LintKind = "stale-spec"
// PastRemoveBy: the feature declares RemoveBy and Min >= RemoveBy. Delete it.
PastRemoveBy LintKind = "past-remove-by"
// DeprecatedDue: the feature's latest spec is Deprecated and due for removal.
DeprecatedDue LintKind = "deprecated"
)
// Violation is one lint finding, shared by the lifecycle lint ([Lint]) and the baseline
// lint ([Baseline]).
type Violation struct {
Feature Feature
Kind LintKind
// ActiveFrom is the activation version the finding points at. It is meaningful for
// StaleSpec (the stale spec) and RewroteHistory (the rewritten spec), and is zero for
// the other kinds (PastRemoveBy, DeprecatedDue, VersionJump), so do not print it for them.
ActiveFrom Activation
}
// kindOrder defines the ordering of kinds within a feature (core-spec §8).
var kindOrder = map[LintKind]int{StaleSpec: 0, PastRemoveBy: 1, DeprecatedDue: 2}
// Lint enforces the contract step (activation-spec §4, core-spec §8). It is a pure
// function of the registry and the support window: it needs no version source, so a
// consumer can run it from a test or CI. Violations are ordered by feature, then
// kind, then ActiveFrom; a clean registry returns none.
//
// window is the support window W, with the same meaning as WithSupportWindow; pass
// the value your binary gives New, because Min depends on it. Lint takes it
// explicitly rather than as an Option, so it never reads the environment or any
// other process state.
//
// Lint validates the registry as New does and returns that error for an invalid
// one. It does not scan source code: deleting the dead branches a stale spec
// guarded is the consumer's job.
func Lint(reg Registry, window int) ([]Violation, error) {
g, err := New(reg, StaticVersion(0), WithSupportWindow(window))
if err != nil {
return nil, err
}
min := g.EffectiveVersion().Min
var out []Violation
for _, f := range g.Features() {
def := g.specs[f]
for i := 0; i+1 < len(def.Specs); i++ {
if def.Specs[i+1].ActiveFrom <= min {
out = append(out, Violation{Feature: f, Kind: StaleSpec, ActiveFrom: def.Specs[i].ActiveFrom})
}
}
if def.RemoveBy != 0 && min >= def.RemoveBy {
out = append(out, Violation{Feature: f, Kind: PastRemoveBy})
}
if def.Specs[len(def.Specs)-1].Maturity == Deprecated {
out = append(out, Violation{Feature: f, Kind: DeprecatedDue})
}
}
// Features are already sorted and stale specs ascend, so this only orders kinds.
sort.SliceStable(out, func(i, j int) bool {
if out[i].Feature != out[j].Feature {
return out[i].Feature < out[j].Feature
}
if out[i].Kind != out[j].Kind {
return kindOrder[out[i].Kind] < kindOrder[out[j].Kind]
}
return out[i].ActiveFrom < out[j].ActiveFrom
})
return out, nil
}