Skip to content

Commit 71b2f04

Browse files
authored
handle some block comment to detect generated files (#1161)
1 parent 6684c8b commit 71b2f04

5 files changed

+45
-30
lines changed

pkg/result/processors/autogenerated_exclude.go

+7-29
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package processors
22

33
import (
4-
"bufio"
54
"fmt"
6-
"os"
5+
"go/parser"
6+
"go/token"
77
"path/filepath"
88
"strings"
99

@@ -113,37 +113,15 @@ func (p *AutogeneratedExclude) getOrCreateFileSummary(i *result.Issue) (*ageFile
113113
}
114114

115115
func getDoc(filePath string) (string, error) {
116-
file, err := os.Open(filePath)
116+
fset := token.NewFileSet()
117+
syntax, err := parser.ParseFile(fset, filePath, nil, parser.PackageClauseOnly|parser.ParseComments)
117118
if err != nil {
118-
return "", errors.Wrap(err, "failed to open file")
119+
return "", errors.Wrap(err, "failed to parse file")
119120
}
120-
defer file.Close()
121-
122-
scanner := bufio.NewScanner(file)
123-
124-
// Issue 954: Some lines can be very long, e.g. auto-generated
125-
// embedded resources. Reported on file of 86.2KB.
126-
const (
127-
maxSize = 10 * 1024 * 1024 // 10MB should be enough
128-
initialSize = 4096 // same as startBufSize in bufio
129-
)
130-
scanner.Buffer(make([]byte, initialSize), maxSize)
131121

132122
var docLines []string
133-
for scanner.Scan() {
134-
line := strings.TrimSpace(scanner.Text())
135-
if strings.HasPrefix(line, "//") {
136-
text := strings.TrimSpace(strings.TrimPrefix(line, "//"))
137-
docLines = append(docLines, text)
138-
} else if line == "" || strings.HasPrefix(line, "package") {
139-
// go to next line
140-
} else {
141-
break
142-
}
143-
}
144-
145-
if err := scanner.Err(); err != nil {
146-
return "", errors.Wrap(err, "failed to scan file")
123+
for _, c := range syntax.Comments {
124+
docLines = append(docLines, strings.TrimSpace(c.Text()))
147125
}
148126

149127
return strings.Join(docLines, "\n"), nil

pkg/result/processors/autogenerated_exclude_test.go

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/result/processors/testdata/autogen_exclude.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33

44
// third line
55

6-
package testdata // no this text
6+
package testdata // this text also
77
// and this text also
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* first line
3+
*
4+
* second line
5+
* third line
6+
*/
7+
8+
// and this text also
9+
10+
/*
11+
this type of block comment also
12+
*/
13+
14+
/* this one line comment also */
15+
16+
package testdata

pkg/result/processors/testdata/autogen_exclude_long_line.go

+2
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)