6
6
"fmt"
7
7
"go/token"
8
8
"os"
9
+ "regexp"
10
+ "strconv"
9
11
"sync"
10
12
)
11
13
@@ -64,13 +66,14 @@ func (l *Linter) lintPackage(filenames []string, ruleSet []Rule, config Config,
64
66
if err != nil {
65
67
return err
66
68
}
67
- if isGenerated ( content ) && ! config .IgnoreGeneratedHeader {
69
+ if ! config .IgnoreGeneratedHeader && isGenerated ( content ) {
68
70
continue
69
71
}
70
72
71
73
file , err := NewFile (filename , content , pkg )
72
74
if err != nil {
73
- return err
75
+ addInvalidFileFailure (filename , err .Error (), failures )
76
+ continue
74
77
}
75
78
pkg .files [filename ] = file
76
79
}
@@ -97,3 +100,42 @@ func isGenerated(src []byte) bool {
97
100
}
98
101
return false
99
102
}
103
+
104
+ // addInvalidFileFailure adds a failure for an invalid formatted file
105
+ func addInvalidFileFailure (filename , errStr string , failures chan Failure ) {
106
+ position := getPositionInvalidFile (filename , errStr )
107
+ failures <- Failure {
108
+ Confidence : 1 ,
109
+ Failure : fmt .Sprintf ("invalid file %s: %v" , filename , errStr ),
110
+ Category : "validity" ,
111
+ Position : position ,
112
+ }
113
+ }
114
+
115
+ // errPosRegexp matches with an NewFile error message
116
+ // i.e. : corrupted.go:10:4: expected '}', found 'EOF
117
+ // first group matches the line and the second group, the column
118
+ var errPosRegexp = regexp .MustCompile (".*:(\\ d*):(\\ d*):.*$" )
119
+
120
+ // getPositionInvalidFile gets the position of the error in an invalid file
121
+ func getPositionInvalidFile (filename , s string ) FailurePosition {
122
+ pos := errPosRegexp .FindStringSubmatch (s )
123
+ if len (pos ) < 3 {
124
+ return FailurePosition {}
125
+ }
126
+ line , err := strconv .Atoi (pos [1 ])
127
+ if err != nil {
128
+ return FailurePosition {}
129
+ }
130
+ column , err := strconv .Atoi (pos [2 ])
131
+ if err != nil {
132
+ return FailurePosition {}
133
+ }
134
+
135
+ return FailurePosition {
136
+ Start : token.Position {
137
+ Filename : filename ,
138
+ Line : line ,
139
+ Column : column ,
140
+ }}
141
+ }
0 commit comments