Fix dropping comments after handling unexpectedCommentError (#8553)

Signed-off-by: Sebastian Spaink <sebastianspaink@gmail.com>
This commit is contained in:
Sebastian Spaink
2026-04-23 04:47:05 -05:00
committed by GitHub
parent 5e2142efc0
commit f60893275c
3 changed files with 48 additions and 2 deletions
+2 -2
View File
@@ -953,7 +953,7 @@ func (w *writer) writeExpr(expr *ast.Expr, comments []*ast.Comment) ([]*ast.Comm
// Print on same row if already there, otherwise increase indent a print remaining
if withs[0].Location.Row == lastRow {
if comments, err = w.writeWith(withs[0], comments, false); err != nil {
return nil, err
return comments, err
}
lastRow, withs = withs[0].Location.Row, withs[1:]
}
@@ -1150,7 +1150,7 @@ func (w *writer) writeWith(with *ast.With, comments []*ast.Comment, indented boo
w.write(" as ")
comments, err = w.writeTerm(with.Value, comments)
if err != nil {
return nil, err
return comments, err
}
return comments, nil
}
@@ -0,0 +1,23 @@
package p
rule_that_formats_ok if {
r := other with input.x as {
"key": {"value˚": {{
"another": [
[["b", "c"], "3:1:3:8"], # this
[["b"], "4:1:4:8"], # is
[["c"], "5:1:5:8"], # fine
],
}}},
}
r == {{}}
}
# this is a comment
# there are many like this
# but this one is mine
this_comment_above_gets_nuked_and_that_is_not_good if {
r := set()
r == set()
}
@@ -0,0 +1,23 @@
package p
rule_that_formats_ok if {
r := other with input.x as {
"key": {"value˚": {{
"another": [
[["b", "c"], "3:1:3:8"], # this
[["b"], "4:1:4:8"], # is
[["c"], "5:1:5:8"], # fine
],
}}},
}
r == {{}}
}
# this is a comment
# there are many like this
# but this one is mine
this_comment_above_gets_nuked_and_that_is_not_good if {
r := set()
r == set()
}