Skip to content

Commit ddcb2f6

Browse files
committed
refactored translateFunction
1 parent 2c75ca9 commit ddcb2f6

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

compiler/expressions.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,17 @@ func (c *funcContext) translateExpr(expr ast.Expr) *expression {
179179
}
180180

181181
case *ast.FuncLit:
182-
params, body := translateFunction(e.Type, e.Body.List, c, exprType.(*types.Signature), c.p.FuncLitInfos[e], "")
182+
_, fun := translateFunction(e.Type, e.Body.List, c, exprType.(*types.Signature), c.p.FuncLitInfos[e], "")
183183
if len(c.p.escapingVars) != 0 {
184184
names := make([]string, 0, len(c.p.escapingVars))
185185
for obj := range c.p.escapingVars {
186186
names = append(names, c.p.objectVars[obj])
187187
}
188188
sort.Strings(names)
189189
list := strings.Join(names, ", ")
190-
return c.formatExpr("(function(%s) { return function(%s) {\n%s%s}; })(%s)", list, strings.Join(params, ", "), string(body), strings.Repeat("\t", c.p.indentation), list)
190+
return c.formatExpr("(function(%s) { return %s; })(%s)", list, fun, list)
191191
}
192-
return c.formatExpr("(function(%s) {\n%s%s})", strings.Join(params, ", "), string(body), strings.Repeat("\t", c.p.indentation))
192+
return c.formatExpr("(%s)", fun)
193193

194194
case *ast.UnaryExpr:
195195
t := c.p.Types[e.X].Type

compiler/package.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,9 @@ func (c *funcContext) translateToplevelFunction(fun *ast.FuncDecl, info *analysi
552552
},
553553
}, stmts...)
554554
}
555-
params, body := translateFunction(fun.Type, stmts, c, sig, info, fun.Name.Name)
555+
params, fun := translateFunction(fun.Type, stmts, c, sig, info, fun.Name.Name)
556556
joinedParams = strings.Join(params, ", ")
557-
return []byte(fmt.Sprintf("\t%s = function(%s) {\n%s\t};\n", lhs, joinedParams, string(body)))
557+
return []byte(fmt.Sprintf("\t%s = %s;\n", lhs, fun))
558558
}
559559

560560
if fun.Recv == nil {
@@ -603,7 +603,7 @@ func (c *funcContext) translateToplevelFunction(fun *ast.FuncDecl, info *analysi
603603
return code.Bytes()
604604
}
605605

606-
func translateFunction(typ *ast.FuncType, stmts []ast.Stmt, outerContext *funcContext, sig *types.Signature, info *analysis.FuncInfo, name string) ([]string, []byte) {
606+
func translateFunction(typ *ast.FuncType, stmts []ast.Stmt, outerContext *funcContext, sig *types.Signature, info *analysis.FuncInfo, name string) ([]string, string) {
607607
c := &funcContext{
608608
FuncInfo: info,
609609
p: outerContext.p,
@@ -638,7 +638,7 @@ func translateFunction(typ *ast.FuncType, stmts []ast.Stmt, outerContext *funcCo
638638
c.localVars = append(c.localVars, "$this = this")
639639
}
640640

641-
body := c.CatchOutput(1, func() {
641+
body := string(c.CatchOutput(1, func() {
642642
if c.sig != nil && c.sig.Results().Len() != 0 && c.sig.Results().At(0).Name() != "" {
643643
c.resultNames = make([]ast.Expr, c.sig.Results().Len())
644644
for i := 0; i < c.sig.Results().Len(); i++ {
@@ -720,12 +720,12 @@ func translateFunction(typ *ast.FuncType, stmts []ast.Stmt, outerContext *funcCo
720720
if suffix != "" {
721721
c.Printf("/* */%s", suffix)
722722
}
723-
})
723+
}))
724724

725725
if len(c.localVars) != 0 {
726726
sort.Strings(c.localVars)
727-
body = append([]byte(fmt.Sprintf("%svar %s;\n", strings.Repeat("\t", c.p.indentation+1), strings.Join(c.localVars, ", "))), body...)
727+
body = fmt.Sprintf("%svar %s;\n", strings.Repeat("\t", c.p.indentation+1), strings.Join(c.localVars, ", ")) + body
728728
}
729729

730-
return params, body
730+
return params, fmt.Sprintf("function(%s) {\n%s%s}", strings.Join(params, ", "), body, strings.Repeat("\t", c.p.indentation))
731731
}

0 commit comments

Comments
 (0)