diff --git a/desugarer.go b/desugarer.go index f602353a..f7952b6e 100644 --- a/desugarer.go +++ b/desugarer.go @@ -232,7 +232,13 @@ func wrapInArray(inside ast.Node) ast.Node { return &ast.Array{Elements: ast.Nodes{inside}} } + + func desugarArrayComp(comp *ast.ArrayComp, objLevel int) (ast.Node, error) { + err := desugar(&comp.Body, objLevel) + if err != nil { + return nil, err + } return desugarForSpec(wrapInArray(comp.Body), &comp.Spec, objLevel) } @@ -246,12 +252,7 @@ func desugarObjectComp(comp *ast.ObjectComp, objLevel int) (ast.Node, error) { panic("Too many fields in object comprehension, it should have been caught during parsing") } - arrComp := ast.ArrayComp{ - Body: obj, - Spec: comp.Spec, - } - - desugaredArrayComp, err := desugarArrayComp(&arrComp, objLevel) + desugaredArrayComp, err := desugarForSpec(wrapInArray(obj), &comp.Spec, objLevel) if err != nil { return nil, err } @@ -362,12 +363,7 @@ func desugar(astPtr *ast.Node, objLevel int) (err error) { } case *ast.ArrayComp: - comp, err := desugarArrayComp(node, objLevel) - if err != nil { - return err - } - *astPtr = comp - err = desugar(astPtr, objLevel) + *astPtr, err = desugarArrayComp(node, objLevel) if err != nil { return err } diff --git a/testdata/object_literal_in_array_comp.golden b/testdata/object_literal_in_array_comp.golden new file mode 100644 index 00000000..16430e86 --- /dev/null +++ b/testdata/object_literal_in_array_comp.golden @@ -0,0 +1,3 @@ +[ + 42 +] diff --git a/testdata/object_literal_in_array_comp.jsonnet b/testdata/object_literal_in_array_comp.jsonnet new file mode 100644 index 00000000..b6ffdaf8 --- /dev/null +++ b/testdata/object_literal_in_array_comp.jsonnet @@ -0,0 +1 @@ +[42 for i in [{}]] \ No newline at end of file diff --git a/testdata/object_literal_in_object_comp.golden b/testdata/object_literal_in_object_comp.golden new file mode 100644 index 00000000..3a259355 --- /dev/null +++ b/testdata/object_literal_in_object_comp.golden @@ -0,0 +1,3 @@ +{ + "a": 42 +} diff --git a/testdata/object_literal_in_object_comp.jsonnet b/testdata/object_literal_in_object_comp.jsonnet new file mode 100644 index 00000000..349996ec --- /dev/null +++ b/testdata/object_literal_in_object_comp.jsonnet @@ -0,0 +1 @@ +{ ["a"]: 42 for i in [{}]} \ No newline at end of file