Skip to content

Commit 9976bdc

Browse files
NiseVoidtpounds
authored andcommitted
Update whitespace
1 parent 3aade55 commit 9976bdc

File tree

10 files changed

+93
-32
lines changed

10 files changed

+93
-32
lines changed

.golangci.example.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ linters-settings:
205205
# checks assignments with too many blank identifiers; default is 2
206206
max-blank-identifiers: 2
207207

208+
whitespace:
209+
multi-if: false
210+
208211
linters:
209212
enable:
210213
- megacheck

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,9 @@ linters-settings:
790790
# checks assignments with too many blank identifiers; default is 2
791791
max-blank-identifiers: 2
792792
793+
whitespace:
794+
multi-if: false
795+
793796
linters:
794797
enable:
795798
- megacheck

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ require (
3737
github.com/stretchr/testify v1.4.0
3838
github.com/timakin/bodyclose v0.0.0-20190721030226-87058b9bfcec
3939
github.com/ultraware/funlen v0.0.2
40-
github.com/ultraware/whitespace v0.0.2
40+
github.com/ultraware/whitespace v0.0.3
4141
github.com/valyala/quicktemplate v1.2.0
4242
golang.org/x/tools v0.0.0-20190912215617-3720d1ec3678
4343
gopkg.in/yaml.v2 v2.2.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGr
233233
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
234234
github.com/ultraware/funlen v0.0.2 h1:Av96YVBwwNSe4MLR7iI/BIa3VyI7/djnto/pK3Uxbdo=
235235
github.com/ultraware/funlen v0.0.2/go.mod h1:Dp4UiAus7Wdb9KUZsYWZEWiRzGuM2kXM1lPbfaF6xhA=
236-
github.com/ultraware/whitespace v0.0.2 h1:iL4Un0C3VaMIBGfDogtcdBeSotjfSHYW8OdI1U9Vqas=
237-
github.com/ultraware/whitespace v0.0.2/go.mod h1:aVMh/gQve5Maj9hQ/hg+F75lr/X5A89uZnzAmWSineA=
236+
github.com/ultraware/whitespace v0.0.3 h1:S5BCRRB5sttNy0bSOhbpw+0mb+cHiCmWfrvxpEzuUk0=
237+
github.com/ultraware/whitespace v0.0.3/go.mod h1:aVMh/gQve5Maj9hQ/hg+F75lr/X5A89uZnzAmWSineA=
238238
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
239239
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
240240
github.com/valyala/fasthttp v1.2.0/go.mod h1:4vX61m6KN+xDduDNwXrhIAVZaZaZiQ1luJk8LWSxF3s=

pkg/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ type LintersSettings struct {
171171
Lines int
172172
Statements int
173173
}
174+
Whitespace struct {
175+
MultiIf bool `mapstructure:"multi-if"`
176+
}
174177

175178
Lll LllSettings
176179
Unparam UnparamSettings

pkg/golinters/whitespace.go

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ func (Whitespace) Desc() string {
2424
}
2525

2626
func (w Whitespace) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
27+
settings := whitespace.Settings{MultiIf: lintCtx.Cfg.LintersSettings.Whitespace.MultiIf}
28+
2729
var issues []whitespace.Message
2830
for _, file := range lintCtx.ASTCache.GetAllValidFiles() {
29-
issues = append(issues, whitespace.Run(file.F, file.Fset)...)
31+
issues = append(issues, whitespace.Run(file.F, file.Fset, settings)...)
3032
}
3133

3234
if len(issues) == 0 {
@@ -40,33 +42,27 @@ func (w Whitespace) Run(ctx context.Context, lintCtx *linter.Context) ([]result.
4042
Filename: i.Pos.Filename,
4143
Line: i.Pos.Line,
4244
},
45+
LineRange: &result.Range{From: i.Pos.Line, To: i.Pos.Line},
4346
Text: i.Message,
4447
FromLinter: w.Name(),
4548
Replacement: &result.Replacement{},
4649
}
4750

48-
// TODO(jirfag): return more information from Whitespace to get rid of string comparisons
49-
if i.Message == "unnecessary leading newline" {
50-
// cover two lines by the issue: opening bracket "{" (issue.Pos.Line) and following empty line
51-
issue.LineRange = &result.Range{From: issue.Pos.Line, To: issue.Pos.Line + 1}
52-
53-
bracketLine, err := lintCtx.LineCache.GetLine(issue.Pos.Filename, issue.Pos.Line)
54-
if err != nil {
55-
return nil, errors.Wrapf(err, "failed to get line %s:%d", issue.Pos.Filename, issue.Pos.Line)
56-
}
57-
issue.Replacement.NewLines = []string{bracketLine}
58-
} else {
59-
// cover two lines by the issue: closing bracket "}" (issue.Pos.Line) and preceding empty line
60-
issue.LineRange = &result.Range{From: issue.Pos.Line - 1, To: issue.Pos.Line}
61-
62-
bracketLine, err := lintCtx.LineCache.GetLine(issue.Pos.Filename, issue.Pos.Line)
63-
if err != nil {
64-
return nil, errors.Wrapf(err, "failed to get line %s:%d", issue.Pos.Filename, issue.Pos.Line)
65-
}
66-
issue.Replacement.NewLines = []string{bracketLine}
51+
bracketLine, err := lintCtx.LineCache.GetLine(issue.Pos.Filename, issue.Pos.Line)
52+
if err != nil {
53+
return nil, errors.Wrapf(err, "failed to get line %s:%d", issue.Pos.Filename, issue.Pos.Line)
54+
}
6755

68-
issue.Pos.Line-- // set in sync with LineRange.From to not break fixer and other code features
56+
switch i.Type {
57+
case whitespace.MessageTypeLeading:
58+
issue.LineRange.To++ // cover two lines by the issue: opening bracket "{" (issue.Pos.Line) and following empty line
59+
case whitespace.MessageTypeTrailing:
60+
issue.LineRange.From-- // cover two lines by the issue: closing bracket "}" (issue.Pos.Line) and preceding empty line
61+
issue.Pos.Line-- // set in sync with LineRange.From to not break fixer and other code features
62+
case whitespace.MessageTypeAddAfter:
63+
bracketLine += "\n"
6964
}
65+
issue.Replacement.NewLines = []string{bracketLine}
7066

7167
res[k] = issue
7268
}

test/testdata/fix/in/whitespace.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//args: -Ewhitespace
2+
//config: linters-settings.whitespace.multi-if=true
23
package p
34

45
import "fmt"
@@ -45,3 +46,10 @@ func twoLeadingNewlines() {
4546

4647
fmt.Println("Hello world")
4748
}
49+
50+
func multiIfFunc() {
51+
if 1 == 1 &&
52+
2 == 2 {
53+
fmt.Println(`Hello multi-line world`)
54+
}
55+
}

test/testdata/fix/out/whitespace.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//args: -Ewhitespace
2+
//config: linters-settings.whitespace.multi-if=true
23
package p
34

45
import "fmt"
@@ -41,3 +42,11 @@ func twoLeadingNewlines() {
4142

4243
fmt.Println("Hello world")
4344
}
45+
46+
func multiIfFunc() {
47+
if 1 == 1 &&
48+
2 == 2 {
49+
50+
fmt.Println(`Hello multi-line world`)
51+
}
52+
}

vendor/github.com/ultraware/whitespace/main.go

Lines changed: 46 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ github.com/stretchr/testify/require
167167
github.com/timakin/bodyclose/passes/bodyclose
168168
# github.com/ultraware/funlen v0.0.2
169169
github.com/ultraware/funlen
170-
# github.com/ultraware/whitespace v0.0.2
170+
# github.com/ultraware/whitespace v0.0.3
171171
github.com/ultraware/whitespace
172172
# github.com/valyala/bytebufferpool v1.0.0
173173
github.com/valyala/bytebufferpool

0 commit comments

Comments
 (0)