Skip to content

Commit

Permalink
Bug with map returned from lamba single arg, missing outer braces tha…
Browse files Browse the repository at this point in the history
…t are needed (#177)

* Bug with map returned from lamba single arg, missing outer braces that are needed

* fix: need extra braces when the 1 statement is a map/braces

* better to test the test... () are special in regular expressions that testscript uses, need escaping
  • Loading branch information
ldemailly authored Aug 23, 2024
1 parent 50cd5ac commit bba995c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 4 additions & 0 deletions main_test.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ stdout '^{"63":63,"abc":42,"x":{"\[3\]":122,"true":false}}$'
grol -quiet -c 'println(json_go({"abc":42, 63:63, "x": {[3]:122, true:false} }, " "))'
cmp stdout json_output

# returning a map in lambda shouldn't lose needed extra {} despite being solo argument
grol -quiet -c '()=>{{"a":1,"b":2}}'
stdout '^\(\)=>{{"a":1,"b":2}}$'

-- json_output --
{
"63": 63,
Expand Down
6 changes: 4 additions & 2 deletions object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"grol.io/grol/ast"
"grol.io/grol/token"
)

type Type uint8
Expand Down Expand Up @@ -535,11 +536,12 @@ func (f *Function) lambdaPrint(ps *ast.PrintState, out *strings.Builder) string
} else {
out.WriteString("=>")
}
if len(f.Body.Statements) != 1 {
needBraces := len(f.Body.Statements) != 1 || f.Body.Statements[0].Value().Type() == token.LBRACE
if needBraces {
out.WriteString("{")
}
f.Body.PrettyPrint(ps)
if len(f.Body.Statements) != 1 {
if needBraces {
out.WriteString("}")
}
return out.String()
Expand Down

0 comments on commit bba995c

Please sign in to comment.