Skip to content

Commit 9c30b44

Browse files
authored
Run revive over a invalid go source file (#364) (#598)
1 parent 55e1594 commit 9c30b44

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

lint/file.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (f *File) ToPosition(pos token.Pos) token.Position {
4747
return f.Pkg.fset.Position(pos)
4848
}
4949

50-
// Render renters a node.
50+
// Render renders a node.
5151
func (f *File) Render(x interface{}) string {
5252
var buf bytes.Buffer
5353
if err := printer.Fprint(&buf, f.Pkg.fset, x); err != nil {
@@ -74,7 +74,7 @@ var basicTypeKinds = map[types.BasicKind]string{
7474
// and indicates what its default type is.
7575
// scope may be nil.
7676
func (f *File) IsUntypedConst(expr ast.Expr) (defType string, ok bool) {
77-
// Re-evaluate expr outside of its context to see if it's untyped.
77+
// Re-evaluate expr outside its context to see if it's untyped.
7878
// (An expr evaluated within, for example, an assignment context will get the type of the LHS.)
7979
exprStr := f.Render(expr)
8080
tv, err := types.Eval(f.Pkg.fset, f.Pkg.TypesPkg, expr.Pos(), exprStr)
@@ -206,9 +206,9 @@ func (f *File) disabledIntervals(rules []Rule, mustSpecifyDisableReason bool, fa
206206
if len(match) == 0 {
207207
continue
208208
}
209-
210209
ruleNames := []string{}
211210
tempNames := strings.Split(match[rulesPos], ",")
211+
212212
for _, name := range tempNames {
213213
name = strings.Trim(name, "\n")
214214
if len(name) > 0 {

lint/linter.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"go/token"
88
"os"
9+
"regexp"
10+
"strconv"
911
"sync"
1012
)
1113

@@ -64,13 +66,14 @@ func (l *Linter) lintPackage(filenames []string, ruleSet []Rule, config Config,
6466
if err != nil {
6567
return err
6668
}
67-
if isGenerated(content) && !config.IgnoreGeneratedHeader {
69+
if !config.IgnoreGeneratedHeader && isGenerated(content) {
6870
continue
6971
}
7072

7173
file, err := NewFile(filename, content, pkg)
7274
if err != nil {
73-
return err
75+
addInvalidFileFailure(filename, err.Error(), failures)
76+
continue
7477
}
7578
pkg.files[filename] = file
7679
}
@@ -97,3 +100,42 @@ func isGenerated(src []byte) bool {
97100
}
98101
return false
99102
}
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

Comments
 (0)