Skip to content

Commit

Permalink
handle some block comment to detect generated files (#1161)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksoichiro committed May 25, 2020
1 parent 6684c8b commit 71b2f04
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 30 deletions.
36 changes: 7 additions & 29 deletions pkg/result/processors/autogenerated_exclude.go
@@ -1,9 +1,9 @@
package processors

import (
"bufio"
"fmt"
"os"
"go/parser"
"go/token"
"path/filepath"
"strings"

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

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 {
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
for scanner.Scan() {
line := strings.TrimSpace(scanner.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")
for _, c := range syntax.Comments {
docLines = append(docLines, strings.TrimSpace(c.Text()))
}

return strings.Join(docLines, "\n"), nil
Expand Down
19 changes: 19 additions & 0 deletions pkg/result/processors/autogenerated_exclude_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/result/processors/testdata/autogen_exclude.go
Expand Up @@ -3,5 +3,5 @@

// third line

package testdata // no this text
package testdata // this text also
// and this text also
16 changes: 16 additions & 0 deletions pkg/result/processors/testdata/autogen_exclude_block_comment.go
@@ -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 changes: 2 additions & 0 deletions pkg/result/processors/testdata/autogen_exclude_long_line.go

Large diffs are not rendered by default.

0 comments on commit 71b2f04

Please sign in to comment.