Skip to content

Commit

Permalink
For, range, switch
Browse files Browse the repository at this point in the history
  • Loading branch information
bombsimon committed Sep 26, 2024
1 parent d8f161e commit 1471bab
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 12 deletions.
26 changes: 26 additions & 0 deletions testdata/src/default_config/for.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package testpkg

func fn1() {
one := 1
two := 2 // want "missing whitespace decreases readability"
for i := 0; i < two; i++ {
panic(err)
}
}

func fn2() {
two := 2
one := 1
for i := 0; i < two; i++ { // want "missing whitespace decreases readability"
panic(err)
}
}

func fn3() {
for i := 0; i < 1; i++ {
panic(err)
}
for i := 0; i < 1; i++ { // want "missing whitespace decreases readability"
panic(err)
}
}
30 changes: 30 additions & 0 deletions testdata/src/default_config/for.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package testpkg

func fn1() {
one := 1

two := 2 // want "missing whitespace decreases readability"
for i := 0; i < two; i++ {
panic(err)
}
}

func fn2() {
two := 2
one := 1

for i := 0; i < two; i++ { // want "missing whitespace decreases readability"
panic(err)
}
}

func fn3() {
for i := 0; i < 1; i++ {
panic(err)
}

for i := 0; i < 1; i++ { // want "missing whitespace decreases readability"
panic(err)
}
}

26 changes: 26 additions & 0 deletions testdata/src/default_config/range.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package testpkg

func fn1() {
one := []int{}
two := []int{} // want "missing whitespace decreases readability"
for range two {
panic(err)
}
}

func fn2() {
two := []int{}
one := []int{}
for range two { // want "missing whitespace decreases readability"
panic(err)
}
}

func fn3() {
for range make([]int{}, 0) {
panic(foo)
}
for range make([]int{}, 0) { // want "missing whitespace decreases readability"
panic(bar)
}
}
30 changes: 30 additions & 0 deletions testdata/src/default_config/range.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package testpkg

func fn1() {
one := []int{}

two := []int{} // want "missing whitespace decreases readability"
for range two {
panic(err)
}
}

func fn2() {
two := []int{}
one := []int{}

for range two { // want "missing whitespace decreases readability"
panic(err)
}
}

func fn3() {
for range make([]int{}, 0) {
panic(foo)
}

for range make([]int{}, 0) { // want "missing whitespace decreases readability"
panic(bar)
}
}

35 changes: 35 additions & 0 deletions testdata/src/default_config/switch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package testpkg

func fn1() {
one := 1
two := 2 // want "missing whitespace decreases readability"
switch two {
case 1:
case 2:
case 3:
panic(err)
}
}

func fn2() {
two := 2
one := 1
switch two { // want "missing whitespace decreases readability"
case 1:
case 2:
case 3:
panic(err)
}
}

func fn3() {
switch true {
case true:
case false:
}
switch true { // want "missing whitespace decreases readability"
case true:
case false:
panic(err)
}
}
39 changes: 39 additions & 0 deletions testdata/src/default_config/switch.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package testpkg

func fn1() {
one := 1

two := 2 // want "missing whitespace decreases readability"
switch two {
case 1:
case 2:
case 3:
panic(err)
}
}

func fn2() {
two := 2
one := 1

switch two { // want "missing whitespace decreases readability"
case 1:
case 2:
case 3:
panic(err)
}
}

func fn3() {
switch true {
case true:
case false:
}

switch true { // want "missing whitespace decreases readability"
case true:
case false:
panic(err)
}
}

71 changes: 59 additions & 12 deletions wsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,9 @@ func (w *WSL) Run() {
}
}

func (w *WSL) CheckFunc(funcDecl *ast.FuncDecl) {
if funcDecl.Body == nil {
return
}

w.CheckUnnecessaryBlockLeadingNewline(funcDecl.Body)
w.CheckBlock(funcDecl.Body)
}

func (w *WSL) CheckIf(stmt *ast.IfStmt, cursor *Cursor) {
func (w *WSL) CheckCuddling(stmt ast.Node, cursor *Cursor) {
reset := cursor.Save()
defer reset()

currentIdents := allIdents(cursor.Stmt())
previousIdents := []*ast.Ident{}
Expand Down Expand Up @@ -125,6 +117,10 @@ func (w *WSL) CheckIf(stmt *ast.IfStmt, cursor *Cursor) {
}
}

if _, ok := stmt.(*ast.IfStmt); !ok {
return
}

if w.Config.Errcheck && n == 0 && len(previousIdents) > 0 {
if slices.ContainsFunc(previousIdents, func(ident *ast.Ident) bool {
return w.implementsErr(ident)
Expand Down Expand Up @@ -162,9 +158,19 @@ func (w *WSL) CheckIf(stmt *ast.IfStmt, cursor *Cursor) {
}
}
}
}

func (w *WSL) CheckFunc(funcDecl *ast.FuncDecl) {
if funcDecl.Body == nil {
return
}

reset()
w.CheckUnnecessaryBlockLeadingNewline(funcDecl.Body)
w.CheckBlock(funcDecl.Body)
}

func (w *WSL) CheckIf(stmt *ast.IfStmt, cursor *Cursor) {
w.CheckCuddling(stmt, cursor)
w.CheckUnnecessaryBlockLeadingNewline(stmt.Body)

// if
Expand All @@ -181,6 +187,21 @@ func (w *WSL) CheckIf(stmt *ast.IfStmt, cursor *Cursor) {
}
}

func (w *WSL) CheckFor(stmt *ast.ForStmt, cursor *Cursor) {
w.CheckCuddling(stmt, cursor)
w.CheckBlock(stmt.Body)
}

func (w *WSL) CheckRange(stmt *ast.RangeStmt, cursor *Cursor) {
w.CheckCuddling(stmt, cursor)
w.CheckBlock(stmt.Body)
}

func (w *WSL) CheckSwitch(stmt *ast.SwitchStmt, cursor *Cursor) {
w.CheckCuddling(stmt, cursor)
w.CheckBlock(stmt.Body)
}

func (w *WSL) CheckBlock(block *ast.BlockStmt) {
cursor := NewCursor(-1, block.List)
for cursor.Next() {
Expand Down Expand Up @@ -219,10 +240,13 @@ func (w *WSL) CheckStmt(stmt ast.Stmt, cursor *Cursor) {
w.CheckIf(s, cursor)
// for {} / for a; b; c {}
case *ast.ForStmt:
w.CheckFor(s, cursor)
// for _, _ = range a {}
case *ast.RangeStmt:
w.CheckRange(s, cursor)
// switch {} // switch a {}
case *ast.SwitchStmt:
w.CheckSwitch(s, cursor)
// switch a.(type) {}
case *ast.TypeSwitchStmt:
// return a
Expand All @@ -241,6 +265,7 @@ func (w *WSL) CheckStmt(stmt ast.Stmt, cursor *Cursor) {
// go func() {}
case *ast.GoStmt:
case *ast.ExprStmt:
case *ast.CaseClause:
case *ast.BlockStmt:
w.CheckBlock(s)
default:
Expand Down Expand Up @@ -370,13 +395,35 @@ func allIdents(node ast.Node) []*ast.Ident {
for _, lhs := range n.Lhs {
idents = append(idents, allIdents(lhs)...)
}

// TODO: This should be here right?
for _, rhs := range n.Rhs {
idents = append(idents, allIdents(rhs)...)
}
case *ast.IfStmt:
idents = append(idents, allIdents(n.Cond)...)
// TODO: idents = append(idents, allIdents(n.Else)...)
case *ast.BinaryExpr:
idents = append(idents, allIdents(n.X)...)
idents = append(idents, allIdents(n.Y)...)
case *ast.BasicLit:
case *ast.RangeStmt:
idents = append(idents, allIdents(n.X)...)
case *ast.ForStmt:
idents = append(idents, allIdents(n.Init)...)
idents = append(idents, allIdents(n.Cond)...)
idents = append(idents, allIdents(n.Post)...)
case *ast.SwitchStmt:
idents = append(idents, allIdents(n.Init)...)
idents = append(idents, allIdents(n.Tag)...)
case *ast.CallExpr:
for _, arg := range n.Args {
idents = append(idents, allIdents(arg)...)
}
case *ast.CompositeLit:
for _, elt := range n.Elts {
idents = append(idents, allIdents(elt)...)
}
case *ast.BasicLit, *ast.IncDecStmt:
default:
spew.Dump(node)
fmt.Printf("%T\n", node)
Expand Down

0 comments on commit 1471bab

Please sign in to comment.