-
Notifications
You must be signed in to change notification settings - Fork 2
/
validators.go
259 lines (216 loc) · 5.73 KB
/
validators.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package eclint
import (
"bytes"
"errors"
"fmt"
"github.com/editorconfig/editorconfig-core-go/v2"
)
const (
cr = '\r'
lf = '\n'
tab = '\t'
space = ' '
)
var (
utf8Bom = []byte{0xef, 0xbb, 0xbf} //nolint:gochecknoglobals
utf16leBom = []byte{0xff, 0xfe} //nolint:gochecknoglobals
utf16beBom = []byte{0xfe, 0xff} //nolint:gochecknoglobals
utf32leBom = []byte{0xff, 0xfe, 0, 0} //nolint:gochecknoglobals
utf32beBom = []byte{0, 0, 0xfe, 0xff} //nolint:gochecknoglobals
)
// ErrConfiguration represents an error in the editorconfig value.
var ErrConfiguration = errors.New("configuration error")
// ValidationError is a rich type containing information about the error.
type ValidationError struct {
Message string
Filename string
Line []byte
Index int
Position int
}
func (e ValidationError) String() string {
return e.Error()
}
// Error builds the error string.
func (e ValidationError) Error() string {
return fmt.Sprintf("%s:%d:%d: %s", e.Filename, e.Index+1, e.Position+1, e.Message)
}
// endOfLines checks the line ending.
func endOfLine(eol string, data []byte) error {
switch eol {
case editorconfig.EndOfLineLf:
if !bytes.HasSuffix(data, []byte{lf}) || bytes.HasSuffix(data, []byte{cr, lf}) {
return ValidationError{
Message: "line does not end with lf (`\\n`)",
Position: len(data),
}
}
case editorconfig.EndOfLineCrLf:
if !bytes.HasSuffix(data, []byte{cr, lf}) && !bytes.HasSuffix(data, []byte{0x00, cr, 0x00, lf}) {
return ValidationError{
Message: "line does not end with crlf (`\\r\\n`)",
Position: len(data),
}
}
case editorconfig.EndOfLineCr:
if !bytes.HasSuffix(data, []byte{cr}) {
return ValidationError{
Message: "line does not end with cr (`\\r`)",
Position: len(data),
}
}
default:
return fmt.Errorf("%w: %q is an invalid value for eol, want cr, crlf, or lf", ErrConfiguration, eol)
}
return nil
}
// indentStyle checks that the line beginnings are either space or tabs.
func indentStyle(style string, size int, data []byte) error {
var c byte
var x byte
switch style {
case SpaceValue:
c = space
x = tab
case TabValue:
c = tab
x = space
size = 1
case UnsetValue:
return nil
default:
return fmt.Errorf("%w: %q is an invalid value of indent_style, want tab or space", ErrConfiguration, style)
}
if size == 0 {
return nil
}
if size < 0 {
return fmt.Errorf("%w: %d is an invalid value of indent_size, want a number or unset", ErrConfiguration, size)
}
for i := 0; i < len(data); i++ {
if data[i] == c {
continue
}
if data[i] == x {
return ValidationError{
Message: fmt.Sprintf("indentation style mismatch expected %q (%s) got %q", c, style, x),
Position: i,
}
}
if data[i] == cr || data[i] == lf || (size > 0 && i%size == 0) {
break
}
return ValidationError{
Message: fmt.Sprintf("indentation size doesn't match expected %d, got %d", size, i),
Position: i,
}
}
return nil
}
// checkInsertFinalNewline checks whenever the final line contains a newline or not.
func checkInsertFinalNewline(data []byte, insertFinalNewline bool) error {
if len(data) == 0 {
return nil
}
lastChar := data[len(data)-1]
if lastChar != cr && lastChar != lf {
if insertFinalNewline {
return ValidationError{
Message: "the final newline is missing",
Position: len(data),
}
}
} else {
if !insertFinalNewline {
return ValidationError{
Message: "an extraneous final newline was found",
Position: len(data),
}
}
}
return nil
}
// checkTrimTrailingWhitespace lints any spaces before the final newline.
func checkTrimTrailingWhitespace(data []byte) error {
for i := len(data) - 1; i >= 0; i-- {
if data[i] == cr || data[i] == lf {
continue
}
if data[i] == space || data[i] == tab {
return ValidationError{
Message: "line has some trailing whitespaces",
Position: i,
}
}
break
}
return nil
}
// isBlockCommentStart tells you when a block comment started on this line.
func isBlockCommentStart(start []byte, data []byte) bool {
for i := 0; i < len(data); i++ {
if data[i] == space || data[i] == tab {
continue
}
return bytes.HasPrefix(data[i:], start)
}
return false
}
// checkBlockComment checks the line is a valid block comment.
func checkBlockComment(i int, prefix []byte, data []byte) error {
for ; i < len(data); i++ {
if data[i] == space || data[i] == tab {
continue
}
if !bytes.HasPrefix(data[i:], prefix) {
return ValidationError{
Message: fmt.Sprintf("block_comment prefix %q was expected inside a block comment", string(prefix)),
Position: i,
}
}
break
}
return nil
}
// isBlockCommentEnd tells you when a block comment end on this line.
func isBlockCommentEnd(end []byte, data []byte) bool {
for i := len(data) - 1; i > 0; i-- {
if data[i] == cr || data[i] == lf {
continue
}
return bytes.HasSuffix(data[:i+1], end)
}
return false
}
// MaxLineLength checks the length of a given line.
//
// It assumes UTF-8 and will count as one runes. The first byte has no prefix
// 0xxxxxxx, 110xxxxx, 1110xxxx, 11110xxx, 111110xx, etc. and the following byte
// the 10xxxxxx prefix which are skipped.
func MaxLineLength(maxLength int, tabWidth int, data []byte) error {
length := 0
breakingPosition := 0
for i := 0; i < len(data); i++ {
if data[i] == cr || data[i] == lf {
break
}
switch {
case data[i] == tab:
length += tabWidth
case (data[i] >> 6) == 0b10:
// skip 0x10xxxxxx that are UTF-8 continuation markers
default:
length++
}
if length > maxLength && breakingPosition == 0 {
breakingPosition = i
}
}
if length > maxLength {
return ValidationError{
Message: fmt.Sprintf("line is too long (%d > %d)", length, maxLength),
Position: breakingPosition,
}
}
return nil
}