cue/parser: fix comment attachement issue
Change-Id: I93fe8f4f5ce825c3b1558eeba5d264f0bce264ff
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/3820
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cue/parser/parser.go b/cue/parser/parser.go
index bc8ecce..7d2ef07 100644
--- a/cue/parser/parser.go
+++ b/cue/parser/parser.go
@@ -748,7 +748,9 @@
return decl
}
+ sc := p.openComments()
expr := p.parseStruct()
+ sc.closeExpr(p, expr)
if p.atComma("struct literal", token.RBRACE) { // TODO: may be EOF
p.next()
diff --git a/cue/parser/parser_test.go b/cue/parser/parser_test.go
index b1cbc42..6e50229 100644
--- a/cue/parser/parser_test.go
+++ b/cue/parser/parser_test.go
@@ -384,6 +384,16 @@
`,
`<[l5/* c */ // d] a: <[1/* a */] 1> <[1/* b */] @a()> @b()>`,
}, {
+ "comprehension comments",
+ `
+ if X {
+ // Comment 1
+ Field: 2
+ // Comment 2
+ }
+ `,
+ `if X <[d2// Comment 2] {<[d0// Comment 1] Field: 2>}>`,
+ }, {
"emit comments",
`// a comment at the beginning of the file