Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle some block comment to detect generated files #1161

Merged
merged 3 commits into from May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.