-
Notifications
You must be signed in to change notification settings - Fork 232
/
locations.go
71 lines (61 loc) · 1.71 KB
/
locations.go
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
package errors
import (
goAst "go/ast"
goToken "go/token"
)
// LocationType represents if the locaton is the source of the error, a warning or a helpful hint
type LocationType uint8
const (
LocError LocationType = iota
LocWarning
LocHelp
)
// LocationKind tells us what language and position markers we're using to identify the source
type LocationKind uint8
const (
LocFile LocationKind = iota
LocGoNode
LocGoPos
LocGoPositions
)
// SrcLocation tells us where in the code base caused the error, warning or is a hint to the end
// user.
type SrcLocation struct {
Kind LocationKind
LocType LocationType
Text string
Filepath string
GoNode goAst.Node
GoStartPos goToken.Pos
GoEndPos goToken.Pos
GoStartPosition goToken.Position
GoEndPosition goToken.Position
}
type LocationOption func(*SrcLocation)
// AsError allows you to set a SrcLocation's error text
//
// Pass this option in when you give the error [Template] a src location
func AsError(errorText string) func(loc *SrcLocation) {
return func(loc *SrcLocation) {
loc.Text = errorText
loc.LocType = LocError
}
}
// AsWarning allows you to set a SrcLocation's text and mark it as a warning
//
// Pass this option in when you give the error [Template] a src location
func AsWarning(warningText string) func(loc *SrcLocation) {
return func(loc *SrcLocation) {
loc.Text = warningText
loc.LocType = LocWarning
}
}
// AsHelp allows you to set a SrcLocation's text and mark it as a helpful hint
//
// Pass this option in when you give the error [Template] a src location
func AsHelp(helpText string) func(loc *SrcLocation) {
return func(loc *SrcLocation) {
loc.LocType = LocHelp
loc.Text = helpText
}
}