Skip to content

Commit

Permalink
Use go modules, analysis package and add test (#8)
Browse files Browse the repository at this point in the history
* 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
bombsimon authored Nov 2, 2023
1 parent cfcbc0a commit e527b27
Show file tree
Hide file tree
Showing 12 changed files with 852 additions and 163 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/go.yaml
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 ./...
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ Whitespace is a linter that checks for unnecessary newlines at the start and end

## Installation guide

Whitespace is included in [golangci-lint](https://github.com/golangci/golangci-lint/). Install it and enable whitespace.
To install as a standalone linter, run `go install github.com/ultraware/whitespace`.

Whitespace is also included in [golangci-lint](https://github.com/golangci/golangci-lint/). Install it and enable whitespace.
10 changes: 10 additions & 0 deletions cmd/whitespace/main.go
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))
}
10 changes: 10 additions & 0 deletions go.mod
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
)
8 changes: 8 additions & 0 deletions go.sum
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=
162 changes: 0 additions & 162 deletions main.go

This file was deleted.

85 changes: 85 additions & 0 deletions testdata/src/whitespace_multiline/multiline.go
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")
}
}
85 changes: 85 additions & 0 deletions testdata/src/whitespace_multiline/multiline.go.golden
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")
}
}
Loading

0 comments on commit e527b27

Please sign in to comment.