From 760d1ddbbd7d5f366fae072239cb07a3ff209d15 Mon Sep 17 00:00:00 2001 From: mappu Date: Wed, 8 Apr 2020 19:58:35 +1200 Subject: [PATCH] node: elide parentheses in some cases --- node.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/node.go b/node.go index 32581a5..743a85d 100644 --- a/node.go +++ b/node.go @@ -598,7 +598,7 @@ func (this *conversionState) convertNoFreeFloating(n_ node.Node) (string, error) return "", parseErr{n, err} } - args = append(args, exprGo) + args = append(args, removeParens(exprGo)) } this.importPackages["fmt"] = struct{}{} @@ -1182,6 +1182,16 @@ func constructorName(className string) string { return `New` + className } +// removeParens removes surrounding parentheses from an expression. +// This is only safe in cases where there is ambiguously a single rvalue wanted, +// e.g. between ( and , in a function call argument +func removeParens(expr string) string { + for len(expr) > 2 && expr[0] == '(' && expr[len(expr)-1] == ')' { + expr = expr[1 : len(expr)-1] + } + return expr +} + // resolveName turns a `*name.Name` node into a Go string. func (this *conversionState) resolveName(n node.Node) (string, error) { // TODO support namespace lookups @@ -1346,7 +1356,7 @@ func (this *conversionState) convertFuncCallArgsCommon(args *node.ArgumentList) rvalue = "..." + rvalue } - callParams = append(callParams, rvalue) + callParams = append(callParams, removeParens(rvalue)) } return "(" + strings.Join(callParams, `, `) + ")", nil // expr only, no semicolon/newline