this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cue/format: fix comma placement for ellipsis with inline comments

When formatting list elements like `...string // comment`, the comma
was placed after the comment instead of before it. The fix collects
trailing comments from the type and defers them until after the comma.

Fixes #4238

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I2b40bab3f5c0c6bed3bd2e010f6a2c2e72c955d8
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1229757
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Matthew Sackman <matthew@cue.works>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

+15 -9
+13 -7
cue/format/node.go
··· 231 231 for _, x := range list { 232 232 f.before(x) 233 233 234 - // This is a hack to ensure that comments are printed correctly in lists. 235 - // A comment must be printed after each element in a list, but we can't 236 - // print a comma at the end of a comment because it will be considered 237 - // part of the comment and ignored. 238 - // To fix this we collect all comments that appear after the element, 239 - // and only handle them after it's formatted. 234 + // Collect comments that appear after the element's start position. 235 + // These need to be printed after the comma, not before it. 240 236 var commentsAfter []*ast.CommentGroup 241 237 splitComments := x.Pos().IsValid() 242 238 if splitComments { ··· 257 253 f.expr(n.Value) 258 254 259 255 case *ast.Ellipsis: 260 - f.ellipsis(n) 256 + // For ellipsis, also collect trailing comments from the type 257 + // since they're attached to the nested node, not the ellipsis itself. 258 + f.print(n.Ellipsis, token.ELLIPSIS) 259 + if n.Type != nil && !isTop(n.Type) { 260 + for _, cg := range ast.Comments(n.Type) { 261 + if n.Type.Pos().Compare(cg.Pos()) < 0 { 262 + commentsAfter = append(commentsAfter, cg) 263 + } 264 + } 265 + f.exprRaw(n.Type, token.LowestPrec, 1) 266 + } 261 267 262 268 case *ast.Alias: 263 269 f.expr(n.Ident)
+2 -2
cue/format/testdata/comments.txtar
··· 343 343 344 344 // issue #4238 345 345 [string]: [ 346 - ...string // inline comment without a comma, 346 + ...string, // inline comment without a comma 347 347 ] 348 348 [string]: [ 349 - ...string // inline comment with a comma, 349 + ...string, // inline comment with a comma 350 350 ] 351 351 -- comment_alone.input -- 352 352 // Just one comment on its own.