Skip to content

Commit d3c38be

Browse files
committed
simplify the tracking of parent funcs for clothing returns
We can take advantage of the cursor's pre and post steps to add and remove items from the stack. We can also track func types directly to further simplify the logic. Finally, the parentLoop seemed unnecessary; a return statement is always directly inside the function that it relates to.
1 parent 0e5ef64 commit d3c38be

File tree

1 file changed

+9
-32
lines changed

1 file changed

+9
-32
lines changed

format/format.go

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ func File(fset *token.FileSet, file *ast.File, opts Options) {
114114
switch node := c.Node().(type) {
115115
case *ast.FuncDecl:
116116
topFuncType = node.Type
117+
f.parentFuncTypes = append(f.parentFuncTypes, node.Type)
118+
case *ast.FuncLit:
119+
f.parentFuncTypes = append(f.parentFuncTypes, node.Type)
117120
case *ast.FieldList:
118121
ft, _ := c.Parent().(*ast.FuncType)
119122
if ft == nil || ft != topFuncType {
@@ -149,6 +152,8 @@ func File(fset *token.FileSet, file *ast.File, opts Options) {
149152

150153
// Reset minSplitFactor and blockLevel.
151154
switch node := c.Node().(type) {
155+
case *ast.FuncDecl, *ast.FuncLit:
156+
f.parentFuncTypes = f.parentFuncTypes[:len(f.parentFuncTypes)-1]
152157
case *ast.FuncType:
153158
if node == topFuncType {
154159
f.minSplitFactor = 0.4
@@ -186,10 +191,9 @@ type fumpter struct {
186191

187192
minSplitFactor float64
188193

189-
// parentFuncs is a stack of parent function declarations or
190-
// literals, used to determine return type information when clothing
191-
// naked returns.
192-
parentFuncs []ast.Node
194+
// parentFuncTypes is a stack of parent function types,
195+
// used to determine return type information when clothing naked returns.
196+
parentFuncTypes []*ast.FuncType
193197
}
194198

195199
func (f *fumpter) commentsBetween(p1, p2 token.Pos) []*ast.CommentGroup {
@@ -324,16 +328,6 @@ var rxCommentDirective = regexp.MustCompile(`^([a-z-]+:[a-z]+|line\b|export\b|ex
324328
func (f *fumpter) applyPre(c *astutil.Cursor) {
325329
f.splitLongLine(c)
326330

327-
if c.Node() != nil && len(f.parentFuncs) > 0 {
328-
// "pop" the last parent if it's no longer valid.
329-
for i := len(f.parentFuncs) - 1; i >= 0; i-- {
330-
if f.parentFuncs[i].End() < c.Node().Pos() {
331-
f.parentFuncs = f.parentFuncs[:i]
332-
break
333-
}
334-
}
335-
}
336-
337331
switch node := c.Node().(type) {
338332
case *ast.File:
339333
// Join contiguous lone var/const/import lines.
@@ -701,30 +695,13 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {
701695
// Only remove lines between the assignment token and the first right-hand side expression
702696
f.removeLines(f.Line(node.TokPos), f.Line(node.Rhs[0].Pos()))
703697

704-
case *ast.FuncDecl, *ast.FuncLit:
705-
// Track the current function declaration or literal, to access
706-
// return type information for clothing of naked returns.
707-
f.parentFuncs = append(f.parentFuncs, node)
708698
// Clothe naked returns
709699
case *ast.ReturnStmt:
710700
if node.Results != nil {
711701
break
712702
}
713703

714-
// We have either a naked return, or a function with no return values
715-
var results *ast.FieldList
716-
// Find the nearest ancestor that is either a func declaration or func literal
717-
parentLoop:
718-
for i := len(f.parentFuncs) - 1; i >= 0; i-- {
719-
switch p := f.parentFuncs[i].(type) {
720-
case *ast.FuncDecl:
721-
results = p.Type.Results
722-
break parentLoop
723-
case *ast.FuncLit:
724-
results = p.Type.Results
725-
break parentLoop
726-
}
727-
}
704+
results := f.parentFuncTypes[len(f.parentFuncTypes)-1].Results
728705
if results.NumFields() == 0 {
729706
break
730707
}

0 commit comments

Comments
 (0)