Skip to content

Commit 31d7813

Browse files
bombsimonldez
authored andcommitted
whitespace: update after moving to the analysis package (#4003)
Co-authored-by: Fernandez Ludovic <[email protected]>
1 parent c4b90b5 commit 31d7813

File tree

6 files changed

+84
-51
lines changed

6 files changed

+84
-51
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ require (
112112
github.com/tomarrell/wrapcheck/v2 v2.8.1
113113
github.com/tommy-muehle/go-mnd/v2 v2.5.1
114114
github.com/ultraware/funlen v0.1.0
115-
github.com/ultraware/whitespace v0.0.5
115+
github.com/ultraware/whitespace v0.1.0
116116
github.com/uudashr/gocognit v1.1.2
117117
github.com/valyala/quicktemplate v1.7.0
118118
github.com/xen0n/gosmopolitan v1.2.2

go.sum

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/revive.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type jsonObject struct {
3434

3535
// NewRevive returns a new Revive linter.
3636
//
37-
//nolint:dupl
37+
3838
func NewRevive(settings *config.ReviveSettings) *goanalysis.Linter {
3939
var mu sync.Mutex
4040
var resIssues []goanalysis.Issue

pkg/golinters/whitespace.go

+40-46
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package golinters
22

33
import (
44
"fmt"
5-
"go/token"
65
"sync"
76

87
"github.com/ultraware/whitespace"
@@ -16,33 +15,29 @@ import (
1615

1716
const whitespaceName = "whitespace"
1817

19-
//nolint:dupl
2018
func NewWhitespace(settings *config.WhitespaceSettings) *goanalysis.Linter {
2119
var mu sync.Mutex
2220
var resIssues []goanalysis.Issue
2321

2422
var wsSettings whitespace.Settings
2523
if settings != nil {
2624
wsSettings = whitespace.Settings{
25+
Mode: whitespace.RunningModeGolangCI,
2726
MultiIf: settings.MultiIf,
2827
MultiFunc: settings.MultiFunc,
2928
}
3029
}
3130

32-
analyzer := &analysis.Analyzer{
33-
Name: whitespaceName,
34-
Doc: goanalysis.TheOnlyanalyzerDoc,
35-
Run: goanalysis.DummyRun,
36-
}
31+
a := whitespace.NewAnalyzer(&wsSettings)
3732

3833
return goanalysis.NewLinter(
39-
whitespaceName,
40-
"Tool for detection of leading and trailing whitespace",
41-
[]*analysis.Analyzer{analyzer},
34+
a.Name,
35+
a.Doc,
36+
[]*analysis.Analyzer{a},
4237
nil,
4338
).WithContextSetter(func(lintCtx *linter.Context) {
44-
analyzer.Run = func(pass *analysis.Pass) (any, error) {
45-
issues, err := runWhitespace(lintCtx, pass, wsSettings)
39+
a.Run = func(pass *analysis.Pass) (any, error) {
40+
issues, err := runWhitespace(pass, wsSettings)
4641
if err != nil {
4742
return nil, err
4843
}
@@ -62,46 +57,45 @@ func NewWhitespace(settings *config.WhitespaceSettings) *goanalysis.Linter {
6257
}).WithLoadMode(goanalysis.LoadModeSyntax)
6358
}
6459

65-
func runWhitespace(lintCtx *linter.Context, pass *analysis.Pass, wsSettings whitespace.Settings) ([]goanalysis.Issue, error) {
66-
var messages []whitespace.Message
67-
for _, file := range pass.Files {
68-
messages = append(messages, whitespace.Run(file, pass.Fset, wsSettings)...)
69-
}
70-
71-
if len(messages) == 0 {
72-
return nil, nil
73-
}
60+
func runWhitespace(pass *analysis.Pass, wsSettings whitespace.Settings) ([]goanalysis.Issue, error) {
61+
lintIssues := whitespace.Run(pass, &wsSettings)
7462

75-
issues := make([]goanalysis.Issue, len(messages))
76-
for k, i := range messages {
77-
issue := result.Issue{
78-
Pos: token.Position{
79-
Filename: i.Pos.Filename,
80-
Line: i.Pos.Line,
81-
},
82-
LineRange: &result.Range{From: i.Pos.Line, To: i.Pos.Line},
83-
Text: i.Message,
84-
FromLinter: whitespaceName,
85-
Replacement: &result.Replacement{},
63+
issues := make([]goanalysis.Issue, len(lintIssues))
64+
for i, issue := range lintIssues {
65+
report := &result.Issue{
66+
FromLinter: whitespaceName,
67+
Pos: pass.Fset.PositionFor(issue.Diagnostic, false),
68+
Text: issue.Message,
8669
}
8770

88-
bracketLine, err := lintCtx.LineCache.GetLine(issue.Pos.Filename, issue.Pos.Line)
89-
if err != nil {
90-
return nil, fmt.Errorf("failed to get line %s:%d: %w", issue.Pos.Filename, issue.Pos.Line, err)
91-
}
71+
switch issue.MessageType {
72+
case whitespace.MessageTypeRemove:
73+
if len(issue.LineNumbers) == 0 {
74+
continue
75+
}
76+
77+
report.LineRange = &result.Range{
78+
From: issue.LineNumbers[0],
79+
To: issue.LineNumbers[len(issue.LineNumbers)-1],
80+
}
81+
82+
report.Replacement = &result.Replacement{NeedOnlyDelete: true}
83+
84+
case whitespace.MessageTypeAdd:
85+
report.Pos = pass.Fset.PositionFor(issue.FixStart, false)
86+
report.Replacement = &result.Replacement{
87+
Inline: &result.InlineFix{
88+
StartCol: 0,
89+
Length: 1,
90+
NewString: "\n\t",
91+
},
92+
}
9293

93-
switch i.Type {
94-
case whitespace.MessageTypeLeading:
95-
issue.LineRange.To++ // cover two lines by the issue: opening bracket "{" (issue.Pos.Line) and following empty line
96-
case whitespace.MessageTypeTrailing:
97-
issue.LineRange.From-- // cover two lines by the issue: closing bracket "}" (issue.Pos.Line) and preceding empty line
98-
issue.Pos.Line-- // set in sync with LineRange.From to not break fixer and other code features
99-
case whitespace.MessageTypeAddAfter:
100-
bracketLine += "\n"
94+
default:
95+
return nil, fmt.Errorf("unknown message type: %v", issue.MessageType)
10196
}
102-
issue.Replacement.NewLines = []string{bracketLine}
10397

104-
issues[k] = goanalysis.NewIssue(&issue, pass)
98+
issues[i] = goanalysis.NewIssue(report, pass)
10599
}
106100

107101
return issues, nil

test/testdata/fix/in/whitespace.go

+23
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package p
55

66
import "fmt"
77

8+
//line yaccpar:1
89
func oneLeadingNewline() {
910

1011
fmt.Println("Hello world")
@@ -58,4 +59,26 @@ func multiIfFunc() {
5859
2 == 2 {
5960
fmt.Println("Hello multi-line world")
6061
}
62+
63+
if true {
64+
if true {
65+
if true {
66+
if 1 == 1 &&
67+
2 == 2 {
68+
fmt.Println("Hello nested multi-line world")
69+
}
70+
}
71+
}
72+
}
73+
}
74+
75+
func notGoFmted() {
76+
77+
78+
79+
80+
fmt.Println("Hello world")
81+
82+
83+
6184
}

test/testdata/fix/out/whitespace.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package p
55

66
import "fmt"
77

8+
//line yaccpar:1
89
func oneLeadingNewline() {
910
fmt.Println("Hello world")
1011
}
@@ -40,7 +41,6 @@ func oneLeadingNewlineWithCommentFunc() {
4041
}
4142

4243
func twoLeadingNewlines() {
43-
4444
fmt.Println("Hello world")
4545
}
4646

@@ -56,4 +56,20 @@ func multiIfFunc() {
5656

5757
fmt.Println("Hello multi-line world")
5858
}
59+
60+
if true {
61+
if true {
62+
if true {
63+
if 1 == 1 &&
64+
2 == 2 {
65+
66+
fmt.Println("Hello nested multi-line world")
67+
}
68+
}
69+
}
70+
}
71+
}
72+
73+
func notGoFmted() {
74+
fmt.Println("Hello world")
5975
}

0 commit comments

Comments
 (0)