Skip to content

Commit

Permalink
Fix double desugaring in array comps
Browse files Browse the repository at this point in the history
Accidentally the array we were iterating over was desugared
two times, which resulted in crash when an object was there.
  • Loading branch information
sbarzowski committed Jun 23, 2019
1 parent 3ad064e commit 7081ee0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
20 changes: 8 additions & 12 deletions desugarer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions testdata/object_literal_in_array_comp.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
42
]
1 change: 1 addition & 0 deletions testdata/object_literal_in_array_comp.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[42 for i in [{}]]
3 changes: 3 additions & 0 deletions testdata/object_literal_in_object_comp.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"a": 42
}
1 change: 1 addition & 0 deletions testdata/object_literal_in_object_comp.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ ["a"]: 42 for i in [{}]}

0 comments on commit 7081ee0

Please sign in to comment.