Skip to content

Commit 5a8de28

Browse files
olehermanseclaude
andcommitted
cfengine format: Fixed issue with comments and end-of-promise semicolons
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com> Signed-off-by: Ole Herman Schumacher Elgesem <ole@northern.tech>
1 parent 2e9b939 commit 5a8de28

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

src/cfengine_cli/format.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,10 @@ def _comment_indent(node: Node, indent: int) -> int:
822822
# content at the end of the previous sibling subtree, so that comments
823823
# appended after a class-guarded block visually belong to that block.
824824
if nearest is None:
825+
# A comment between a promiser and its terminating ';' is a promise
826+
# continuation — indent it past the promiser.
827+
if node.parent and node.parent.type in {"promise", "half_promise"}:
828+
return indent + 2
825829
prev = _skip_comments(node.prev_named_sibling, "prev")
826830
if prev and prev.type in INDENTED_TYPES:
827831
return _trailing_comment_indent(prev, indent)
@@ -905,6 +909,10 @@ def _autoformat(
905909
if node.type in {",", ";"}:
906910
if previous and previous.type == "macro":
907911
fmt.print(node, indent + 2)
912+
elif previous and previous.type == "comment":
913+
# A '#' comment runs to end of line, so a following ';'/',' can't
914+
# share that line — put it on its own continuation line.
915+
fmt.print(node, indent + 2)
908916
else:
909917
fmt.print_same_line(node)
910918
elif node.type == "comment":

tests/format/004_comments.expected.cf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,16 @@ bundle agent stigs
151151
vars:
152152
"foo" string => "bar";
153153
}
154+
155+
bundle agent example_comment_before_semicolon
156+
{
157+
files:
158+
"affected object"
159+
# ...details....
160+
;
161+
162+
processes:
163+
"affected object"
164+
# ...details....
165+
;
166+
}

tests/format/004_comments.input.cf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,14 @@ bundle agent stigs
143143
vars:
144144
"foo" string => "bar";
145145
}
146+
bundle agent example_comment_before_semicolon
147+
{
148+
files:
149+
"affected object"
150+
# ...details....
151+
;
152+
processes:
153+
"affected object"
154+
# ...details....
155+
;
156+
}

0 commit comments

Comments
 (0)