diff --git a/desugarer.go b/desugarer.go index 0fe4351..148b4a2 100644 --- a/desugarer.go +++ b/desugarer.go @@ -228,6 +228,11 @@ func desugarArrayComp(comp *ast.ArrayComp, objLevel int) (ast.Node, error) { func desugarObjectComp(comp *ast.ObjectComp, objLevel int) (ast.Node, error) { + if objLevel == 0 { + dollar := ast.Identifier("$") + comp.Fields = append(comp.Fields, ast.ObjectFieldLocalNoMethod(&dollar, &ast.Self{})) + } + // TODO(sbarzowski) find a consistent convention to prevent desugaring the same thing twice // here we deeply desugar fields and it will happen again err := desugarFields(*comp.Loc(), &comp.Fields, objLevel+1) diff --git a/testdata/object_comp_dollar.golden b/testdata/object_comp_dollar.golden new file mode 100644 index 0000000..a488a32 --- /dev/null +++ b/testdata/object_comp_dollar.golden @@ -0,0 +1,5 @@ +{ + "a": 42, + "b": 43, + "c": 43 +} diff --git a/testdata/object_comp_dollar.jsonnet b/testdata/object_comp_dollar.jsonnet new file mode 100644 index 0000000..44c8fe6 --- /dev/null +++ b/testdata/object_comp_dollar.jsonnet @@ -0,0 +1 @@ +{ [x]: if x == "a" then 42 else $.a + 1 for x in ["a", "b", "c"] } diff --git a/testdata/object_comp_dollar2.golden b/testdata/object_comp_dollar2.golden new file mode 100644 index 0000000..8762a12 --- /dev/null +++ b/testdata/object_comp_dollar2.golden @@ -0,0 +1,14 @@ +{ + "a": { + "a": 1, + "b": 42 + }, + "b": { + "a": 1, + "b": 43 + }, + "c": { + "a": 1, + "b": 43 + } +} diff --git a/testdata/object_comp_dollar2.jsonnet b/testdata/object_comp_dollar2.jsonnet new file mode 100644 index 0000000..361b8ed --- /dev/null +++ b/testdata/object_comp_dollar2.jsonnet @@ -0,0 +1 @@ +{ [x]: { a: 1, b: if x == "a" then 42 else $.a.b + 1 } for x in ["a", "b", "c"] } diff --git a/testdata/object_comp_dollar3.golden b/testdata/object_comp_dollar3.golden new file mode 100644 index 0000000..ec532ea --- /dev/null +++ b/testdata/object_comp_dollar3.golden @@ -0,0 +1,7 @@ +{ + "obj": { + "a": 1, + "b": 2, + "c": 2 + } +} diff --git a/testdata/object_comp_dollar3.jsonnet b/testdata/object_comp_dollar3.jsonnet new file mode 100644 index 0000000..e0a35ca --- /dev/null +++ b/testdata/object_comp_dollar3.jsonnet @@ -0,0 +1,3 @@ +local obj = { [x]: if x == "a" then 42 else $.a + 1 for x in ["a", "b", "c"] }; +{ obj: obj + {a: 1} } +