Skip to content

Commit 83a46e7

Browse files
committed
Add setting to force a newline after multi-line function signatures
1 parent 77f8a14 commit 83a46e7

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

main.go

+26-19
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const (
2424

2525
// Settings contains settings for edge-cases
2626
type Settings struct {
27-
MultiIf bool
27+
MultiIf bool
28+
MultiFunc bool
2829
}
2930

3031
// Run runs this linter on the provided code
@@ -47,42 +48,40 @@ func Run(file *ast.File, fset *token.FileSet, settings Settings) []Message {
4748
}
4849

4950
type visitor struct {
50-
comments []*ast.CommentGroup
51-
fset *token.FileSet
52-
messages []Message
53-
multiIf map[*ast.BlockStmt]bool
54-
settings Settings
51+
comments []*ast.CommentGroup
52+
fset *token.FileSet
53+
messages []Message
54+
wantNewline map[*ast.BlockStmt]bool
55+
settings Settings
5556
}
5657

5758
func (v *visitor) Visit(node ast.Node) ast.Visitor {
5859
if node == nil {
5960
return v
6061
}
6162

62-
if v.settings.MultiIf {
63-
if stmt, ok := node.(*ast.IfStmt); ok {
64-
start, end := posLine(v.fset, stmt.Cond.Pos()), posLine(v.fset, stmt.Cond.End())
63+
if stmt, ok := node.(*ast.IfStmt); ok && v.settings.MultiIf {
64+
checkMultiLine(v, stmt.Body, stmt.Cond)
65+
}
6566

66-
if end > start { // Check only multi line conditions
67-
v.multiIf[stmt.Body] = true
68-
}
69-
}
67+
if stmt, ok := node.(*ast.FuncDecl); ok && v.settings.MultiFunc {
68+
checkMultiLine(v, stmt.Body, stmt.Type)
7069
}
7170

7271
if stmt, ok := node.(*ast.BlockStmt); ok {
73-
multiIf := v.multiIf[stmt]
72+
wantNewline := v.wantNewline[stmt]
7473

7574
comments := v.comments
76-
if multiIf {
77-
comments = nil
75+
if wantNewline {
76+
comments = nil // Comments also count as a newline if we want a newline
7877
}
7978
first, last := firstAndLast(comments, v.fset, stmt.Pos(), stmt.End(), stmt.List)
8079

8180
startMsg := checkStart(v.fset, stmt.Lbrace, first)
8281

83-
if multiIf && startMsg == nil {
84-
v.messages = append(v.messages, Message{v.fset.Position(stmt.Pos()), MessageTypeAddAfter, `multi-line if should be followed by a newline`})
85-
} else if !multiIf && startMsg != nil {
82+
if wantNewline && startMsg == nil {
83+
v.messages = append(v.messages, Message{v.fset.Position(stmt.Pos()), MessageTypeAddAfter, `multi-line statement should be followed by a newline`})
84+
} else if !wantNewline && startMsg != nil {
8685
v.messages = append(v.messages, *startMsg)
8786
}
8887

@@ -94,6 +93,14 @@ func (v *visitor) Visit(node ast.Node) ast.Visitor {
9493
return v
9594
}
9695

96+
func checkMultiLine(v *visitor, body *ast.BlockStmt, stmtStart ast.Node) {
97+
start, end := posLine(v.fset, stmtStart.Pos()), posLine(v.fset, stmtStart.End())
98+
99+
if end > start { // Check only multi line conditions
100+
v.wantNewline[body] = true
101+
}
102+
}
103+
97104
func posLine(fset *token.FileSet, pos token.Pos) int {
98105
return fset.Position(pos).Line
99106
}

0 commit comments

Comments
 (0)