handle some block comment to detect generated files (#1161)
This commit is contained in:
parent
6684c8b25c
commit
71b2f04e88
@ -1,9 +1,9 @@
|
|||||||
package processors
|
package processors
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"go/parser"
|
||||||
|
"go/token"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -113,37 +113,15 @@ func (p *AutogeneratedExclude) getOrCreateFileSummary(i *result.Issue) (*ageFile
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getDoc(filePath string) (string, error) {
|
func getDoc(filePath string) (string, error) {
|
||||||
file, err := os.Open(filePath)
|
fset := token.NewFileSet()
|
||||||
|
syntax, err := parser.ParseFile(fset, filePath, nil, parser.PackageClauseOnly|parser.ParseComments)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.Wrap(err, "failed to open file")
|
return "", errors.Wrap(err, "failed to parse file")
|
||||||
}
|
}
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(file)
|
|
||||||
|
|
||||||
// Issue 954: Some lines can be very long, e.g. auto-generated
|
|
||||||
// embedded resources. Reported on file of 86.2KB.
|
|
||||||
const (
|
|
||||||
maxSize = 10 * 1024 * 1024 // 10MB should be enough
|
|
||||||
initialSize = 4096 // same as startBufSize in bufio
|
|
||||||
)
|
|
||||||
scanner.Buffer(make([]byte, initialSize), maxSize)
|
|
||||||
|
|
||||||
var docLines []string
|
var docLines []string
|
||||||
for scanner.Scan() {
|
for _, c := range syntax.Comments {
|
||||||
line := strings.TrimSpace(scanner.Text())
|
docLines = append(docLines, strings.TrimSpace(c.Text()))
|
||||||
if strings.HasPrefix(line, "//") {
|
|
||||||
text := strings.TrimSpace(strings.TrimPrefix(line, "//"))
|
|
||||||
docLines = append(docLines, text)
|
|
||||||
} else if line == "" || strings.HasPrefix(line, "package") {
|
|
||||||
// go to next line
|
|
||||||
} else {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := scanner.Err(); err != nil {
|
|
||||||
return "", errors.Wrap(err, "failed to scan file")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.Join(docLines, "\n"), nil
|
return strings.Join(docLines, "\n"), nil
|
||||||
|
@ -78,12 +78,23 @@ func TestGetDoc(t *testing.T) {
|
|||||||
doc: `first line
|
doc: `first line
|
||||||
second line
|
second line
|
||||||
third line
|
third line
|
||||||
|
this text also
|
||||||
and this text also`,
|
and this text also`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
fpath: filepath.Join("testdata", "autogen_exclude_doc.go"),
|
fpath: filepath.Join("testdata", "autogen_exclude_doc.go"),
|
||||||
doc: `DO NOT EDIT`,
|
doc: `DO NOT EDIT`,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
fpath: filepath.Join("testdata", "autogen_exclude_block_comment.go"),
|
||||||
|
doc: `* first line
|
||||||
|
*
|
||||||
|
* second line
|
||||||
|
* third line
|
||||||
|
and this text also
|
||||||
|
this type of block comment also
|
||||||
|
this one line comment also`,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
@ -92,3 +103,11 @@ and this text also`,
|
|||||||
assert.Equal(t, tc.doc, doc)
|
assert.Equal(t, tc.doc, doc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Issue 954: Some lines can be very long, e.g. auto-generated
|
||||||
|
// embedded resources. Reported on file of 86.2KB.
|
||||||
|
func TestGetDocFileWithLongLine(t *testing.T) {
|
||||||
|
fpath := filepath.Join("testdata", "autogen_exclude_long_line.go")
|
||||||
|
_, err := getDoc(fpath)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
@ -3,5 +3,5 @@
|
|||||||
|
|
||||||
// third line
|
// third line
|
||||||
|
|
||||||
package testdata // no this text
|
package testdata // this text also
|
||||||
// and this text also
|
// and this text also
|
||||||
|
16
pkg/result/processors/testdata/autogen_exclude_block_comment.go
vendored
Normal file
16
pkg/result/processors/testdata/autogen_exclude_block_comment.go
vendored
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
/*
|
||||||
|
* first line
|
||||||
|
*
|
||||||
|
* second line
|
||||||
|
* third line
|
||||||
|
*/
|
||||||
|
|
||||||
|
// and this text also
|
||||||
|
|
||||||
|
/*
|
||||||
|
this type of block comment also
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* this one line comment also */
|
||||||
|
|
||||||
|
package testdata
|
2
pkg/result/processors/testdata/autogen_exclude_long_line.go
vendored
Normal file
2
pkg/result/processors/testdata/autogen_exclude_long_line.go
vendored
Normal file
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user