Skip to content

Commit

Permalink
Add support for else-if/else chains
Browse files Browse the repository at this point in the history
  • Loading branch information
bombsimon committed May 28, 2023
1 parent c13b294 commit eb2c2ce
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
20 changes: 20 additions & 0 deletions testdata/src/default_config/else_if.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
func fn() {
if true { // want "block should not start with a whitespace"

fmt.Println("a")
}

if true {
fmt.Println("a")
} else if false { // want "block should not start with a whitespace"

fmt.Println("b")
} else { // want "block should not start with a whitespace"

fmt.Println("c")
fmt.Println("c")
fmt.Println("c")
return // want "return statements should not be cuddled if block has more than two lines"

} // want "block should not end with a whitespace"
}
16 changes: 16 additions & 0 deletions testdata/src/default_config/else_if.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
func fn() {
if true { // want "block should not start with a whitespace"
fmt.Println("a")
}

if true {
fmt.Println("a")
} else if false { // want "block should not start with a whitespace"
fmt.Println("b")
} else { // want "block should not start with a whitespace"
fmt.Println("c")
fmt.Println("c")
fmt.Println("c")
return // want "return statements should not be cuddled if block has more than two lines"
} // want "block should not end with a whitespace"
}
13 changes: 12 additions & 1 deletion wsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,14 @@ func (p *processor) firstBodyStatement(i int, allStmt []ast.Stmt) ast.Node {
}
}

p.parseBlockBody(nil, statementBodyContent)
// If statement bodies will be parsed already when finding block bodies.
// The reason is because if/else-if/else chains is nested in the AST
// where the else bit is a part of the if statement. Since if statements
// is the only statement that can be chained like this we exclude it
// from parsing it again here.
if _, ok := stmt.(*ast.IfStmt); !ok {
p.parseBlockBody(nil, statementBodyContent)
}
case []ast.Stmt:
// The Body field for an *ast.CaseClause or *ast.CommClause is of type
// []ast.Stmt. We must check leading and trailing whitespaces and then
Expand Down Expand Up @@ -946,6 +953,8 @@ func (p *processor) findBlockStmt(node ast.Node) []*ast.BlockStmt {
var blocks []*ast.BlockStmt

switch t := node.(type) {
case *ast.BlockStmt:
return []*ast.BlockStmt{t}
case *ast.AssignStmt:
for _, x := range t.Rhs {
blocks = append(blocks, p.findBlockStmt(x)...)
Expand All @@ -968,6 +977,8 @@ func (p *processor) findBlockStmt(node ast.Node) []*ast.BlockStmt {
blocks = append(blocks, p.findBlockStmt(t.Call)...)
case *ast.GoStmt:
blocks = append(blocks, p.findBlockStmt(t.Call)...)
case *ast.IfStmt:
blocks = append([]*ast.BlockStmt{t.Body}, p.findBlockStmt(t.Else)...)
}

return blocks
Expand Down

0 comments on commit eb2c2ce

Please sign in to comment.