Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,22 @@ Whitespace is a linter that checks for unnecessary newlines at the start and end
To install as a standalone linter, run `go install github.com/ultraware/whitespace/cmd/whitespace@latest`.

Whitespace is also included in [golangci-lint](https://github.com/golangci/golangci-lint/). Install it and enable whitespace.

## Usage

```

Example: ./whitespace ./...

Usage: whitespace [-flag] [package]

Configuration flags:
-ignore-leading
Do not check leading newlines
-ignore-trailing
Do not check trailing newlines
-multi-func
Check that multi line functions have a leading newline
-multi-if
Check that multi line if-statements have a leading newline
```
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/ultraware/whitespace

go 1.20
go 1.24.0

require golang.org/x/tools v0.20.0
require golang.org/x/tools v0.38.0

require (
golang.org/x/mod v0.17.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/mod v0.29.0 // indirect
golang.org/x/sync v0.17.0 // indirect
)
14 changes: 8 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/tools v0.20.0 h1:hz/CVckiOxybQvFw6h7b/q80NTr9IUQb4s1IIzW7KNY=
golang.org/x/tools v0.20.0/go.mod h1:WvitBU7JJf6A4jOdg4S1tviW9bhUxkgeCui/0JHctQg=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
golang.org/x/mod v0.29.0 h1:HV8lRxZC4l2cr3Zq1LvtOsi/ThTgWnUk/y64QSs8GwA=
golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w=
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/tools v0.38.0 h1:Hx2Xv8hISq8Lm16jvBZ2VQf+RLmbd7wVUsALibYI/IQ=
golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs=
168 changes: 168 additions & 0 deletions testdata/src/whitespace_no_leading/no_leading.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package whitespace

import "fmt"

func fn1(
arg1 int,
arg2 int,
) {

fmt.Println("Hello, World")

if true &&
false {

fmt.Println("Hello, World")
}

_ = func(
arg1 int,
arg2 int,
) {

fmt.Println("Hello, World")
}

_ = func(
arg1 int,
arg2 int,
) {
_ = func(
arg1 int,
arg2 int,
) {

fmt.Println("Hello, World")
}
}
}

func fn2(
arg1 int,
arg2 int,
) {

// A comment.
fmt.Println("Hello, World")
}

func fn3(
arg1 int,
arg2 int,
) {



// A comment.
fmt.Println("Hello, World")

if true {



fmt.Println("No comments")


} // want "unnecessary trailing newline"
// Also at end



} // want "unnecessary trailing newline"

// Regular func (FuncDecl) that's not `gofmt`:ed.
func fn4() {




fmt.Println("Hello, World")

if true {



fmt.Println("No comments")


} // want "unnecessary trailing newline"




} // want "unnecessary trailing newline"

// Regular func (FuncDecl) that's not `gofmt`:ed. with comments
func fn5() {
// A comment that should still exist after this
// This one should also still exist



fmt.Println("Hello, World")

if true {
// A comment that should still exist after this
// This one should also still exist



fmt.Println("No comments")


} // want "unnecessary trailing newline"




} // want "unnecessary trailing newline"

// Regular func (FuncDecl) that's not `gofmt`:ed. with comment blocks
func fn6() {
/*
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Curabitur ornare dolor at nulla ultrices cursus.
Mauris pharetra metus ac condimentum sodales.
Fusce viverra libero vitae tellus dictum, sed congue risus sodales.
*/



fmt.Println("Hello, World")

if true {
/*
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Curabitur ornare dolor at nulla ultrices cursus.
Mauris pharetra metus ac condimentum sodales.
Fusce viverra libero vitae tellus dictum, sed congue risus sodales.
*/


fmt.Println("No comments")


} // want "unnecessary trailing newline"




} // want "unnecessary trailing newline"

func fn7() { /* Multiline spaning 1 line */

fmt.Println("No comments")
}

func fn8() { /* Multiline spaning
over several lines */

fmt.Println("No comments")
}

func fn9() { /* Multiline spaning

over several lines */

fmt.Println("No comments")
}
138 changes: 138 additions & 0 deletions testdata/src/whitespace_no_leading/no_leading.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package whitespace

import "fmt"

func fn1(
arg1 int,
arg2 int,
) {

fmt.Println("Hello, World")

if true &&
false {

fmt.Println("Hello, World")
}

_ = func(
arg1 int,
arg2 int,
) {

fmt.Println("Hello, World")
}

_ = func(
arg1 int,
arg2 int,
) {
_ = func(
arg1 int,
arg2 int,
) {

fmt.Println("Hello, World")
}
}
}

func fn2(
arg1 int,
arg2 int,
) {

// A comment.
fmt.Println("Hello, World")
}

func fn3(
arg1 int,
arg2 int,
) {



// A comment.
fmt.Println("Hello, World")

if true {



fmt.Println("No comments")
} // want "unnecessary trailing newline"
// Also at end
} // want "unnecessary trailing newline"

// Regular func (FuncDecl) that's not `gofmt`:ed.
func fn4() {




fmt.Println("Hello, World")

if true {



fmt.Println("No comments")
} // want "unnecessary trailing newline"
} // want "unnecessary trailing newline"

// Regular func (FuncDecl) that's not `gofmt`:ed. with comments
func fn5() {
// A comment that should still exist after this
// This one should also still exist

fmt.Println("Hello, World")

if true {
// A comment that should still exist after this
// This one should also still exist

fmt.Println("No comments")
} // want "unnecessary trailing newline"
} // want "unnecessary trailing newline"

// Regular func (FuncDecl) that's not `gofmt`:ed. with comment blocks
func fn6() {
/*
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Curabitur ornare dolor at nulla ultrices cursus.
Mauris pharetra metus ac condimentum sodales.
Fusce viverra libero vitae tellus dictum, sed congue risus sodales.
*/

fmt.Println("Hello, World")

if true {
/*
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Curabitur ornare dolor at nulla ultrices cursus.
Mauris pharetra metus ac condimentum sodales.
Fusce viverra libero vitae tellus dictum, sed congue risus sodales.
*/

fmt.Println("No comments")
} // want "unnecessary trailing newline"
} // want "unnecessary trailing newline"

func fn7() { /* Multiline spaning 1 line */

fmt.Println("No comments")
}

func fn8() { /* Multiline spaning
over several lines */

fmt.Println("No comments")
}

func fn9() { /* Multiline spaning

over several lines */

fmt.Println("No comments")
}
Loading