-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidation.go
More file actions
160 lines (138 loc) · 4.25 KB
/
validation.go
File metadata and controls
160 lines (138 loc) · 4.25 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2026 TypeFox GmbH
// This program and the accompanying materials are made available under the
// terms of the MIT License, which is available in the project root.
package fastbelt
import "context"
// DiagnosticSeverity mirrors LSP DiagnosticSeverity values.
type DiagnosticSeverity int
const (
SeverityError DiagnosticSeverity = 1
SeverityWarning DiagnosticSeverity = 2
SeverityInfo DiagnosticSeverity = 3
SeverityHint DiagnosticSeverity = 4
)
func (s DiagnosticSeverity) String() string {
switch s {
case SeverityError:
return "Error"
case SeverityWarning:
return "Warning"
case SeverityInfo:
return "Info"
case SeverityHint:
return "Hint"
default:
return "Unknown"
}
}
// DiagnosticTag mirrors LSP DiagnosticTag values.
type DiagnosticTag int
const (
TagUnnecessary DiagnosticTag = 1
TagDeprecated DiagnosticTag = 2
)
// Diagnostic represents a diagnostic message such as an error or warning.
// The struct mirrors lsp.Diagnostic so the core package stays free of that dependency.
type Diagnostic struct {
Range TextRange
Severity DiagnosticSeverity
Message string
Source string
Code string
CodeDescription *DiagnosticCodeDescription
Tags []DiagnosticTag
Data any
}
type DiagnosticCodeDescription struct {
Href string
}
// ValidationAcceptor is a callback that collects diagnostics reported during validation.
type ValidationAcceptor func(diagnostic *Diagnostic)
// Validator can be implemented by AST node Impl structs to provide custom validation checks.
type Validator interface {
// Validate performs validation on the receiver node.
// The level parameter identifies when validation runs (e.g. "on-type", "on-save").
// The accept callback is used to collect diagnostics.
Validate(ctx context.Context, level string, accept ValidationAcceptor)
}
// DiagnosticOption configures optional fields of a [Diagnostic] created by [NewDiagnostic].
type DiagnosticOption func(d *Diagnostic)
// NewDiagnostic creates a [Diagnostic] anchored to the given node's text range.
func NewDiagnostic(severity DiagnosticSeverity, message string, node AstNode, opts ...DiagnosticOption) *Diagnostic {
d := &Diagnostic{
Severity: severity,
Message: message,
}
if seg := node.Segment(); seg != nil {
d.Range = seg.Range
}
for _, opt := range opts {
opt(d)
}
return d
}
// WithToken narrows the diagnostic range to the given token's text segment.
// NOTE: These options might clash with other options in this package. If that happens,
// we can either rename them to DiagnosticToken etc. or move them to a separate package.
func WithToken(token *Token) DiagnosticOption {
return func(d *Diagnostic) {
if token != nil {
d.Range = token.TextSegment.Range
}
}
}
func WithStringUnit(unit StringUnit) DiagnosticOption {
return func(d *Diagnostic) {
if unit != nil {
if seg := unit.Segment(); seg != nil {
d.Range = seg.Range
}
}
}
}
func WithReference(ref UntypedReference) DiagnosticOption {
return func(d *Diagnostic) {
if ref != nil {
if seg := ref.Segment(); seg != nil {
d.Range = seg.Range
}
}
}
}
// WithRange sets an explicit range on the diagnostic, overriding any node or token range.
func WithRange(r TextRange) DiagnosticOption {
return func(d *Diagnostic) {
d.Range = r
}
}
// WithCode sets the diagnostic code.
func WithCode(code string) DiagnosticOption {
return func(d *Diagnostic) {
d.Code = code
}
}
// WithTags sets diagnostic tags (e.g. [TagUnnecessary], [TagDeprecated]).
func WithTags(tags ...DiagnosticTag) DiagnosticOption {
return func(d *Diagnostic) {
d.Tags = tags
}
}
// WithData attaches arbitrary data to the diagnostic.
func WithData(data any) DiagnosticOption {
return func(d *Diagnostic) {
d.Data = data
}
}
// Attaches additional information to describe the error code.
// Currently only supports a hyperlink to documentation.
func WithCodeDescription(codeDescription *DiagnosticCodeDescription) DiagnosticOption {
return func(d *Diagnostic) {
d.CodeDescription = codeDescription
}
}
// WithCodeDescriptionHref sets a hyperlink for the error code description.
func WithCodeDescriptionHref(href string) DiagnosticOption {
return func(d *Diagnostic) {
d.CodeDescription = &DiagnosticCodeDescription{Href: href}
}
}