Skip to content

Commit a46616a

Browse files
committed
WIP: Removal fixer
* Remove newline in end of block * Remove newline in start of block (and comments...) This is WIP because we need to preserve the comments so they don't get deleted when we remove beginning of blocks.
1 parent 07a9043 commit a46616a

7 files changed

+109
-72
lines changed

analyzer.go

+12-18
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package wsl
22

33
import (
44
"flag"
5-
"go/token"
65

76
"golang.org/x/tools/go/analysis"
87
)
@@ -55,40 +54,35 @@ func run(pass *analysis.Pass) (interface{}, error) {
5554

5655
for _, v := range processor.Result {
5756
var (
58-
pos token.Pos
59-
end token.Pos
6057
newText []byte
6158
textEdits []analysis.TextEdit
6259
)
6360

64-
//nolint:exhaustive // Not while TODO
6561
switch v.Type {
6662
case WhitespaceShouldAddBefore:
67-
pos = v.FixNode.Pos()
68-
end = v.FixNode.Pos()
6963
newText = []byte("\n")
7064
case WhitespaceShouldAddAfter:
71-
pos = v.FixNode.End()
72-
end = v.FixNode.End()
7365
newText = []byte("\n")
66+
case WhitespaceShouldRemoveEnd:
67+
newText = []byte("\n}")
68+
case WhitespaceShouldRemoveBeginning:
69+
newText = []byte("{\n")
7470
default:
75-
//nolint:gocritic // We need TODOs while iterating...
71+
//nolint:gocritic // Not while TODO
7672
// TODO
7773
continue
7874
}
7975

80-
if !v.NoFix {
81-
textEdits = []analysis.TextEdit{
82-
{
83-
Pos: pos,
84-
End: end,
85-
NewText: newText,
86-
},
87-
}
76+
textEdits = []analysis.TextEdit{
77+
{
78+
Pos: v.FixRangeStart,
79+
End: v.FixRangeEnd,
80+
NewText: newText,
81+
},
8882
}
8983

9084
d := analysis.Diagnostic{
91-
Pos: v.Node.Pos(),
85+
Pos: v.ReportAt,
9286
Category: "",
9387
Message: v.Reason,
9488
SuggestedFixes: []analysis.SuggestedFix{

go.mod

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ go 1.19
44

55
require github.com/stretchr/testify v1.8.1
66

7+
require (
8+
golang.org/x/mod v0.8.0 // indirect
9+
golang.org/x/sys v0.5.0 // indirect
10+
)
11+
712
require (
813
github.com/davecgh/go-spew v1.1.1 // indirect
914
github.com/kr/text v0.2.0 // indirect
1015
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
1116
github.com/pmezard/go-difflib v1.0.0 // indirect
12-
golang.org/x/tools v0.0.0-20200403190813-44a64ad78b9b
13-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
17+
golang.org/x/tools v0.6.0
1418
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
1519
gopkg.in/yaml.v3 v3.0.1 // indirect
1620
)

go.sum

+7-20
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,13 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
1717
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
1818
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
1919
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
20-
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
21-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
22-
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
23-
golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
24-
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
25-
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
26-
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
27-
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
28-
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
29-
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
30-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
31-
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
32-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
33-
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
34-
golang.org/x/tools v0.0.0-20200403190813-44a64ad78b9b h1:AFZdJUT7jJYXQEC29hYH/WZkoV7+KhwxQGmdZ19yYoY=
35-
golang.org/x/tools v0.0.0-20200403190813-44a64ad78b9b/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
36-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
37-
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
38-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
39-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
20+
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
21+
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
22+
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
23+
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
24+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
25+
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
26+
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
4027
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4128
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
4229
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

testdata/fix_advanced.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package testpkg
22

33
func Advanced() {
44
var foo = 1
5-
var bar = 2 // want "declarations should never be cuddled"
5+
var bar = 2 // want "declarations should never be cuddled"
66
var biz int // want "declarations should never be cuddled"
77

88
x := []string{}

testdata/remove_whitespace.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package testpkg
2+
3+
import "fmt"
4+
5+
func RemoveWhitespaceNoComments() { // want "block should not start with a whitespace"
6+
7+
a := 1
8+
if a < 2 { // want "block should not start with a whitespace"
9+
10+
a = 2
11+
12+
} // want "block should not end with a whitespace"
13+
14+
switch { // want "block should not start with a whitespace"
15+
16+
case true:
17+
fmt.Println("true")
18+
19+
default:
20+
fmt.Println("false")
21+
22+
} // want "block should not end with a whitespace"
23+
24+
_ = a
25+
26+
} // want "block should not end with a whitespace"

testdata/remove_whitespace.go.golden

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package testpkg
2+
3+
import "fmt"
4+
5+
func RemoveWhitespaceNoComments() {
6+
a := 1
7+
if a < 2 {
8+
a = 2
9+
} // want "block should not end with a whitespace"
10+
11+
switch {
12+
case true:
13+
fmt.Println("true")
14+
15+
default:
16+
fmt.Println("false")
17+
} // want "block should not end with a whitespace"
18+
19+
_ = a
20+
} // want "block should not end with a whitespace"

wsl.go

+37-31
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ func DefaultConfig() Configuration {
222222

223223
// Result represents the result of one error.
224224
type Result struct {
225+
ReportAt token.Pos
226+
FixRangeStart token.Pos
227+
FixRangeEnd token.Pos
228+
225229
Node ast.Node
226230
FixNode ast.Node
227231
Reason string
@@ -416,20 +420,20 @@ func (p *Processor) parseBlockStatements(statements []ast.Stmt) {
416420
// it was and use *that* statement's position
417421
if p.config.ForceExclusiveShortDeclarations && cuddledWithLastStmt {
418422
if p.isShortDecl(stmt) && !p.isShortDecl(previousStatement) {
419-
p.addError(
420-
stmt,
421-
nil,
423+
p.addErrorRange(
424+
stmt.Pos(),
425+
stmt.End(),
426+
stmt.End(),
422427
reasonShortDeclNotExclusive,
423428
WhitespaceShouldAddAfter,
424-
false,
425429
)
426430
} else if p.isShortDecl(previousStatement) && !p.isShortDecl(stmt) {
427-
p.addError(
428-
previousStatement,
429-
nil,
431+
p.addErrorRange(
432+
previousStatement.Pos(),
433+
previousStatement.Pos(),
434+
previousStatement.Pos(),
430435
reasonShortDeclNotExclusive,
431436
WhitespaceShouldAddBefore,
432-
false,
433437
)
434438
}
435439
}
@@ -449,7 +453,7 @@ func (p *Processor) parseBlockStatements(statements []ast.Stmt) {
449453
// If the variable on the line above is allowed to be
450454
// cuddled, break two lines above so we keep the proper
451455
// cuddling.
452-
p.addWhitespaceBeforeFixOtherNodeError(n1, n2, reason)
456+
p.addErrorRange(n1.Pos(), n2.Pos(), n2.Pos(), reason, WhitespaceShouldAddBefore)
453457
} else {
454458
// If not, break here so we separate the cuddled variable.
455459
p.addWhitespaceBeforeError(n1, reason)
@@ -1140,7 +1144,15 @@ func (p *Processor) findLeadingAndTrailingWhitespaces(ident *ast.Ident, stmt, ne
11401144
// And now if the first statement is passed the number of allowed lines,
11411145
// then we had extra WS, possibly before the first comment group.
11421146
if p.nodeStart(firstStatement) > blockStartLine+allowedLinesBeforeFirstStatement {
1143-
p.addError(stmt, nil, reasonBlockStartsWithWS, WhitespaceShouldRemoveBeginning, false)
1147+
// TODO: We need to pick a better start of the range so we don't delete
1148+
// potential comments.
1149+
p.addErrorRange(
1150+
blockStartPos,
1151+
blockStartPos,
1152+
firstStatement.Pos(),
1153+
reasonBlockStartsWithWS,
1154+
WhitespaceShouldRemoveBeginning,
1155+
)
11441156
}
11451157

11461158
// If the blockEndLine is not 0 we're a regular block (not case).
@@ -1163,7 +1175,13 @@ func (p *Processor) findLeadingAndTrailingWhitespaces(ident *ast.Ident, stmt, ne
11631175
}
11641176

11651177
if p.nodeEnd(lastStatement) != blockEndLine-1 && !isExampleFunc(ident) {
1166-
p.addError(stmt, nil, reasonBlockEndsWithWS, WhitespaceShouldRemoveEnd, false)
1178+
p.addErrorRange(
1179+
stmt.End(),
1180+
lastStatement.End(),
1181+
stmt.End(),
1182+
reasonBlockEndsWithWS,
1183+
WhitespaceShouldRemoveEnd,
1184+
)
11671185
}
11681186

11691187
return
@@ -1278,32 +1296,20 @@ func isEmptyLabeledStmt(node ast.Node) bool {
12781296
}
12791297

12801298
func (p *Processor) addWhitespaceBeforeError(node ast.Node, reason string) {
1281-
p.addError(node, nil, reason, WhitespaceShouldAddBefore, false)
1282-
}
1283-
1284-
func (p *Processor) addWhitespaceBeforeFixOtherNodeError(reportNode, fixNode ast.Node, reason string) {
1285-
p.addError(reportNode, fixNode, reason, WhitespaceShouldAddBefore, false)
1299+
p.addErrorRange(node.Pos(), node.Pos(), node.Pos(), reason, WhitespaceShouldAddBefore)
12861300
}
12871301

12881302
func (p *Processor) addWhitespaceAfterError(node ast.Node, reason string) {
1289-
p.addError(node, nil, reason, WhitespaceShouldAddAfter, false)
1303+
p.addErrorRange(node.Pos(), node.End(), node.End(), reason, WhitespaceShouldAddAfter)
12901304
}
12911305

1292-
// Add an error for the file and line number for the current token.Pos with the
1293-
// given reason.
1294-
//
1295-
//nolint:unparam // We will potentially use this in the future.
1296-
func (p *Processor) addError(reportNode, fixNode ast.Node, reason string, errType ErrorType, noFix bool) {
1297-
if fixNode == nil {
1298-
fixNode = reportNode
1299-
}
1300-
1306+
func (p *Processor) addErrorRange(reportAt, start, end token.Pos, reason string, errType ErrorType) {
13011307
p.Result = append(p.Result, Result{
1302-
Node: reportNode,
1303-
FixNode: fixNode,
1304-
Reason: reason,
1305-
NoFix: noFix,
1306-
Type: errType,
1308+
ReportAt: reportAt,
1309+
FixRangeStart: start,
1310+
FixRangeEnd: end,
1311+
Reason: reason,
1312+
Type: errType,
13071313
})
13081314
}
13091315

0 commit comments

Comments
 (0)