Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for else-if/else chains #132

Merged
merged 1 commit into from
May 28, 2023
Merged
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
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