Skip to content

Fix linting of goyacc files #505

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

Merged
merged 1 commit into from
Apr 20, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pkg/result/processors/autogenerated_exclude.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"go/ast"
"go/token"
"path/filepath"
"strings"

"github.com/golangci/golangci-lint/pkg/lint/astcache"
Expand Down Expand Up @@ -41,12 +42,22 @@ func (p *AutogeneratedExclude) Process(issues []result.Issue) ([]result.Issue, e
return filterIssuesErr(issues, p.shouldPassIssue)
}

func isSpecialAutogeneratedFile(filePath string) bool {
fileName := filepath.Base(filePath)
// fake files to which //line points to for goyacc generated files
return fileName == "yacctab" || fileName == "yaccpar" || fileName == "NONE"
}

func (p *AutogeneratedExclude) shouldPassIssue(i *result.Issue) (bool, error) {
if i.FromLinter == "typecheck" {
// don't hide typechecking errors in generated files: users expect to see why the project isn't compiling
return true, nil
}

if isSpecialAutogeneratedFile(i.FilePath()) {
return false, nil
}

fs, err := p.getOrCreateFileSummary(i)
if err != nil {
return false, err
Expand Down
19 changes: 12 additions & 7 deletions pkg/result/processors/filename_unadjuster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ type posMapper func(pos token.Position) token.Position
// to get filename. And they return adjusted filename (e.g. *.qtpl) for an issue. We need
// restore real .go filename to properly output it, parse it, etc.
type FilenameUnadjuster struct {
m map[string]posMapper // map from adjusted filename to position mapper: adjusted -> unadjusted position
log logutils.Log
m map[string]posMapper // map from adjusted filename to position mapper: adjusted -> unadjusted position
log logutils.Log
loggedUnadjustments map[string]bool
}

var _ Processor = FilenameUnadjuster{}
var _ Processor = &FilenameUnadjuster{}

func NewFilenameUnadjuster(cache *astcache.Cache, log logutils.Log) *FilenameUnadjuster {
m := map[string]posMapper{}
Expand Down Expand Up @@ -51,16 +52,17 @@ func NewFilenameUnadjuster(cache *astcache.Cache, log logutils.Log) *FilenameUna
}

return &FilenameUnadjuster{
m: m,
log: log,
m: m,
log: log,
loggedUnadjustments: map[string]bool{},
}
}

func (p FilenameUnadjuster) Name() string {
return "filename_unadjuster"
}

func (p FilenameUnadjuster) Process(issues []result.Issue) ([]result.Issue, error) {
func (p *FilenameUnadjuster) Process(issues []result.Issue) ([]result.Issue, error) {
return transformIssues(issues, func(i *result.Issue) *result.Issue {
issueFilePath := i.FilePath()
if !filepath.IsAbs(i.FilePath()) {
Expand All @@ -79,7 +81,10 @@ func (p FilenameUnadjuster) Process(issues []result.Issue) ([]result.Issue, erro

newI := *i
newI.Pos = mapper(i.Pos)
p.log.Infof("Unadjusted from %v to %v", i.Pos, newI.Pos)
if !p.loggedUnadjustments[i.Pos.Filename] {
p.log.Infof("Unadjusted from %v to %v", i.Pos, newI.Pos)
p.loggedUnadjustments[i.Pos.Filename] = true
}
return &newI
}), nil
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/result/processors/skip_dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ func (p *SkipDirs) Process(issues []result.Issue) ([]result.Issue, error) {

func (p *SkipDirs) shouldPassIssue(i *result.Issue) bool {
if filepath.IsAbs(i.FilePath()) {
p.log.Warnf("Got abs path in skip dirs processor, it should be relative")
if !isSpecialAutogeneratedFile(i.FilePath()) {
p.log.Warnf("Got abs path %s in skip dirs processor, it should be relative", i.FilePath())
}
return true
}

Expand Down