-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Adding gofumpt #1177
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
Adding gofumpt #1177
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0368987
Adding gofumpt
b44db2b
Updating changelog
d16d013
Bytes comparison simplification
dffb7b4
Adding test
d77761d
Merge branch 'master' into gofumpt
a5fcffd
Updating go.sum
2fa559f
Fixing magic number
badf526
Using strings.count
108fb8c
Fixing linting issue
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,78 @@ | ||
package golinters | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"go/token" | ||
"io/ioutil" | ||
"strings" | ||
"sync" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
"mvdan.cc/gofumpt/format" | ||
|
||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
"github.com/golangci/golangci-lint/pkg/lint/linter" | ||
"github.com/golangci/golangci-lint/pkg/result" | ||
) | ||
|
||
const gofumptName = "gofumpt" | ||
|
||
func NewGofumpt() *goanalysis.Linter { | ||
var mu sync.Mutex | ||
var resIssues []goanalysis.Issue | ||
|
||
analyzer := &analysis.Analyzer{ | ||
Name: gofumptName, | ||
Doc: goanalysis.TheOnlyanalyzerDoc, | ||
} | ||
return goanalysis.NewLinter( | ||
gofumptName, | ||
"Gofumpt checks whether code was gofumpt-ed.", | ||
[]*analysis.Analyzer{analyzer}, | ||
nil, | ||
).WithContextSetter(func(lintCtx *linter.Context) { | ||
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { | ||
var fileNames []string | ||
for _, f := range pass.Files { | ||
pos := pass.Fset.PositionFor(f.Pos(), false) | ||
fileNames = append(fileNames, pos.Filename) | ||
} | ||
|
||
var issues []goanalysis.Issue | ||
|
||
for _, f := range fileNames { | ||
input, err := ioutil.ReadFile(f) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to open file %s: %w", f, err) | ||
} | ||
output, err := format.Source(input, "") | ||
if err != nil { | ||
return nil, fmt.Errorf("error while running gofumpt: %w", err) | ||
} | ||
if !bytes.Equal(input, output) { | ||
issues = append(issues, goanalysis.NewIssue(&result.Issue{ | ||
FromLinter: gofumptName, | ||
Text: "File is not `gofumpt`-ed", | ||
Pos: token.Position{ | ||
Filename: f, | ||
Line: strings.Count(string(input), "\n"), | ||
}, | ||
}, pass)) | ||
} | ||
} | ||
|
||
if len(issues) == 0 { | ||
return nil, nil | ||
} | ||
|
||
mu.Lock() | ||
resIssues = append(resIssues, issues...) | ||
mu.Unlock() | ||
|
||
return nil, nil | ||
} | ||
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { | ||
return resIssues | ||
}).WithLoadMode(goanalysis.LoadModeSyntax) | ||
} |
This file contains hidden or 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 hidden or 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,11 @@ | ||
//args: -Egofumpt | ||
package testdata | ||
|
||
import "fmt" | ||
|
||
func GofumptNewLine() { | ||
|
||
fmt.Println("foo") | ||
} | ||
|
||
// ERROR "File is not `gofumpt`-ed" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a
Replacement
parameter to automatically fix issues, which is very useful for gofumpt.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm not mistaken, gofumpt provides only as a public function a way to get the gofumpt-ed version of the file (as a
[]byte
).With
Replacement
you mean I should include the gofumpt-ed version of the whole file in the issue?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tetafro Am I correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, sorry, I didn't think of it.
I'm not sure if it's a right thing to do. Someone more experienced with golangci-lint architecture should answer.
Anyway, if there's no easy way to do this, just skip my first comment.