-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preprocessed files like .qtpl.go quicktemplate Go files can have //line directives. They map to a source .qtpl file. This commit fixes linting of such files: 1. don't fail on AST cache loading 2. output Go filename not .qtpl or similar Also, here we update golint to the upstream version. Relates: #316, #466, #467, #468
- Loading branch information
Showing
17 changed files
with
274 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package processors | ||
|
||
import ( | ||
"go/token" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/golangci/golangci-lint/pkg/logutils" | ||
|
||
"github.com/golangci/golangci-lint/pkg/lint/astcache" | ||
|
||
"github.com/golangci/golangci-lint/pkg/result" | ||
) | ||
|
||
type posMapper func(pos token.Position) token.Position | ||
|
||
// FilenameUnadjuster is needed because a lot of linters use fset.Position(f.Pos()) | ||
// 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 | ||
} | ||
|
||
var _ Processor = FilenameUnadjuster{} | ||
|
||
func NewFilenameUnadjuster(cache *astcache.Cache, log logutils.Log) *FilenameUnadjuster { | ||
m := map[string]posMapper{} | ||
for _, f := range cache.GetAllValidFiles() { | ||
adjustedFilename := f.Fset.PositionFor(f.F.Pos(), true).Filename | ||
if adjustedFilename == "" { | ||
continue | ||
} | ||
unadjustedFilename := f.Fset.PositionFor(f.F.Pos(), false).Filename | ||
if unadjustedFilename == "" || unadjustedFilename == adjustedFilename { | ||
continue | ||
} | ||
if !strings.HasSuffix(unadjustedFilename, ".go") { | ||
continue // file.go -> /caches/cgo-xxx | ||
} | ||
|
||
f := f | ||
m[adjustedFilename] = func(adjustedPos token.Position) token.Position { | ||
tokenFile := f.Fset.File(f.F.Pos()) | ||
if tokenFile == nil { | ||
log.Warnf("Failed to get token file for %s", adjustedFilename) | ||
return adjustedPos | ||
} | ||
return f.Fset.PositionFor(tokenFile.Pos(adjustedPos.Offset), false) | ||
} | ||
} | ||
|
||
return &FilenameUnadjuster{ | ||
m: m, | ||
log: log, | ||
} | ||
} | ||
|
||
func (p FilenameUnadjuster) Name() string { | ||
return "filename_unadjuster" | ||
} | ||
|
||
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()) { | ||
absPath, err := filepath.Abs(i.FilePath()) | ||
if err != nil { | ||
p.log.Warnf("failed to build abs path for %q: %s", i.FilePath(), err) | ||
return i | ||
} | ||
issueFilePath = absPath | ||
} | ||
|
||
mapper := p.m[issueFilePath] | ||
if mapper == nil { | ||
return i | ||
} | ||
|
||
newI := *i | ||
newI.Pos = mapper(i.Pos) | ||
p.log.Infof("Unadjusted from %v to %v", i.Pos, newI.Pos) | ||
return &newI | ||
}), nil | ||
} | ||
|
||
func (FilenameUnadjuster) Finish() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
All text outside function templates is treated as comments, | ||
i.e. it is just ignored by quicktemplate compiler (`qtc`). It is for humans. | ||
|
||
Hello is a simple template function. | ||
{% func Hello(name string) %} | ||
Hello, {%s name %}! | ||
{% endfunc %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// This file is automatically generated by qtc from "hello.qtpl". | ||
// See https://github.com/valyala/quicktemplate for details. | ||
|
||
// All text outside function templates is treated as comments, | ||
// i.e. it is just ignored by quicktemplate compiler (`qtc`). It is for humans. | ||
// | ||
// Hello is a simple template function. | ||
|
||
//line hello.qtpl:5 | ||
package quicktemplate | ||
|
||
//line hello.qtpl:5 | ||
import ( | ||
qtio422016 "io" | ||
|
||
qt422016 "github.com/valyala/quicktemplate" | ||
) | ||
|
||
//line hello.qtpl:5 | ||
var ( | ||
_ = qtio422016.Copy | ||
_ = qt422016.AcquireByteBuffer | ||
) | ||
|
||
//line hello.qtpl:5 | ||
func StreamHello(qw422016 *qt422016.Writer, name string) { | ||
//line hello.qtpl:5 | ||
qw422016.N().S(` | ||
Hello, `) | ||
//line hello.qtpl:6 | ||
qw422016.E().S(name) | ||
//line hello.qtpl:6 | ||
qw422016.N().S(`! | ||
`) | ||
//line hello.qtpl:7 | ||
} | ||
|
||
//line hello.qtpl:7 | ||
func WriteHello(qq422016 qtio422016.Writer, name string) { | ||
//line hello.qtpl:7 | ||
qw422016 := qt422016.AcquireWriter(qq422016) | ||
//line hello.qtpl:7 | ||
StreamHello(qw422016, name) | ||
//line hello.qtpl:7 | ||
qt422016.ReleaseWriter(qw422016) | ||
//line hello.qtpl:7 | ||
} | ||
|
||
//line hello.qtpl:7 | ||
func Hello(name string) string { | ||
//line hello.qtpl:7 | ||
qb422016 := qt422016.AcquireByteBuffer() | ||
//line hello.qtpl:7 | ||
WriteHello(qb422016, name) | ||
//line hello.qtpl:7 | ||
qs422016 := string(qb422016.B) | ||
//line hello.qtpl:7 | ||
qt422016.ReleaseByteBuffer(qb422016) | ||
//line hello.qtpl:7 | ||
return qs422016 | ||
//line hello.qtpl:7 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.