-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use go modules, analysis package and add test (#8)
* Use go modules, analysis package and add test * Use go modules * Use `analysis.Analyzer` for the linter * Introduce running mode to use from `golangci-lint` * Add tests using the testing package and assert fixer * Add standalone linter (`cmd/whitespace`) * Fix comments and typos, add another test * Add GitHub actions * Make linter work on non formatted files Instead of relying on the file being `gofmt`:ed and only containing one empty line at most this commit will keep track of comments after the opening bracket of block statements and set the fix start to where this comment ends. This means that we will fix all the way from the left bracket or comment til the first statement no matter how many empty lines this is. This will also keep a list of lines affected by the linter to support fixing multiple empty lines in `golangci-lint`. * Fix typos, add tests from PR * Multiline comments ending after opening pos is ok * Fix typo
- Loading branch information
Showing
12 changed files
with
852 additions
and
163 deletions.
There are no files selected for viewing
This file contains 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,22 @@ | ||
--- | ||
name: Build and test | ||
on: | ||
push: | ||
pull_request: | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '1.21' | ||
|
||
- name: Build | ||
run: go build -v ./... | ||
|
||
- name: Test | ||
run: go test -v ./... |
This file contains 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 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,10 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/ultraware/whitespace" | ||
"golang.org/x/tools/go/analysis/singlechecker" | ||
) | ||
|
||
func main() { | ||
singlechecker.Main(whitespace.NewAnalyzer(nil)) | ||
} |
This file contains 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,10 @@ | ||
module github.com/ultraware/whitespace | ||
|
||
go 1.20 | ||
|
||
require golang.org/x/tools v0.12.0 | ||
|
||
require ( | ||
golang.org/x/mod v0.12.0 // indirect | ||
golang.org/x/sys v0.11.0 // indirect | ||
) |
This file contains 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,8 @@ | ||
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= | ||
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= | ||
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= | ||
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/tools v0.11.0 h1:EMCa6U9S2LtZXLAMoWiR/R8dAQFRqbAitmbJ2UKhoi8= | ||
golang.org/x/tools v0.11.0/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8= | ||
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss= | ||
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM= |
This file contains 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,85 @@ | ||
package whitespace | ||
|
||
import "fmt" | ||
|
||
// No comments, only whitespace. | ||
func fn1() { // want "unnecessary leading newline" | ||
|
||
fmt.Println("Hello, World") | ||
|
||
} // want "unnecessary trailing newline" | ||
|
||
// Whitespace before leading and after trailing comment. | ||
func fn2() { // want "unnecessary leading newline" | ||
|
||
// Some leading comments - this should yield error. | ||
fmt.Println("Hello, World") | ||
// And some trailing comments as well! | ||
|
||
} // want "unnecessary trailing newline" | ||
|
||
// Whitespace after leading and before trailing comment - no diagnostic. | ||
func fn3() { | ||
// This is fine, newline is after comment | ||
|
||
fmt.Println("Hello, World") | ||
|
||
// This is fine, newline before comment | ||
} | ||
|
||
// MultiFunc (FuncDecl), MultiIf and MultiFunc(FuncLit) without newlinew. | ||
func fn4( | ||
arg1 int, | ||
arg2 int, | ||
) { // want "multi-line statement should be followed by a newline" | ||
fmt.Println("Hello, World") | ||
|
||
if true && | ||
false { // want "multi-line statement should be followed by a newline" | ||
fmt.Println("Hello, World") | ||
} | ||
|
||
_ = func( | ||
arg1 int, | ||
arg2 int, | ||
) { // want "multi-line statement should be followed by a newline" | ||
fmt.Println("Hello, World") | ||
} | ||
|
||
_ = func( | ||
arg1 int, | ||
arg2 int, | ||
) { // want "multi-line statement should be followed by a newline" | ||
_ = func( | ||
arg1 int, | ||
arg2 int, | ||
) { // want "multi-line statement should be followed by a newline" | ||
fmt.Println("Hello, World") | ||
} | ||
} | ||
} | ||
|
||
|
||
// MultiFunc (FuncDecl), MultiIf and MultiFunc(FuncLit) with comments counting | ||
// as newlines. | ||
func fn5( | ||
arg1 int, | ||
arg2 int, | ||
) { | ||
// Comment count as newline. | ||
fmt.Println("Hello, World") | ||
|
||
if true && | ||
false { | ||
// Comment count as newline. | ||
fmt.Println("Hello, World") | ||
} | ||
|
||
_ = func( | ||
arg1 int, | ||
arg2 int, | ||
) { | ||
// Comment count as newline. | ||
fmt.Println("Hello, World") | ||
} | ||
} |
This file contains 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,85 @@ | ||
package whitespace | ||
|
||
import "fmt" | ||
|
||
// No comments, only whitespace. | ||
func fn1() { // want "unnecessary leading newline" | ||
fmt.Println("Hello, World") | ||
} // want "unnecessary trailing newline" | ||
|
||
// Whitespace before leading and after trailing comment. | ||
func fn2() { // want "unnecessary leading newline" | ||
// Some leading comments - this should yield error. | ||
fmt.Println("Hello, World") | ||
// And some trailing comments as well! | ||
} // want "unnecessary trailing newline" | ||
|
||
// Whitespace after leading and before trailing comment - no diagnostic. | ||
func fn3() { | ||
// This is fine, newline is after comment | ||
|
||
fmt.Println("Hello, World") | ||
|
||
// This is fine, newline before comment | ||
} | ||
|
||
// MultiFunc (FuncDecl), MultiIf and MultiFunc(FuncLit) without newlinew. | ||
func fn4( | ||
arg1 int, | ||
arg2 int, | ||
) { // want "multi-line statement should be followed by a newline" | ||
|
||
fmt.Println("Hello, World") | ||
|
||
if true && | ||
false { // want "multi-line statement should be followed by a newline" | ||
|
||
fmt.Println("Hello, World") | ||
} | ||
|
||
_ = func( | ||
arg1 int, | ||
arg2 int, | ||
) { // want "multi-line statement should be followed by a newline" | ||
|
||
fmt.Println("Hello, World") | ||
} | ||
|
||
_ = func( | ||
arg1 int, | ||
arg2 int, | ||
) { // want "multi-line statement should be followed by a newline" | ||
|
||
_ = func( | ||
arg1 int, | ||
arg2 int, | ||
) { // want "multi-line statement should be followed by a newline" | ||
|
||
fmt.Println("Hello, World") | ||
} | ||
} | ||
} | ||
|
||
// MultiFunc (FuncDecl), MultiIf and MultiFunc(FuncLit) with comments counting | ||
// as newlines. | ||
func fn5( | ||
arg1 int, | ||
arg2 int, | ||
) { | ||
// Comment count as newline. | ||
fmt.Println("Hello, World") | ||
|
||
if true && | ||
false { | ||
// Comment count as newline. | ||
fmt.Println("Hello, World") | ||
} | ||
|
||
_ = func( | ||
arg1 int, | ||
arg2 int, | ||
) { | ||
// Comment count as newline. | ||
fmt.Println("Hello, World") | ||
} | ||
} |
Oops, something went wrong.