Skip to content

Commit 88ff48d

Browse files
olehermanseclaude
andcommitted
Fixed trailing comma and closing curly brace issues
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Ole Herman Schumacher Elgesem <ole@northern.tech>
1 parent 63348d2 commit 88ff48d

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

src/cfengine_cli/format.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,16 @@ def stringify_single_line_nodes(nodes: list[Node]) -> str:
133133
"""Join tree-sitter nodes into a single-line string with CFEngine spacing.
134134
135135
Inserts spaces after ",", around "=>", and inside "{ }".
136+
Strips trailing commas immediately preceding ")" or "}".
136137
"""
137138
result = ""
138139
previous = None
139-
for node in nodes:
140+
for i, node in enumerate(nodes):
141+
# Strip trailing comma before closing bracket/paren
142+
if node.type == ",":
143+
next_node = nodes[i + 1] if i + 1 < len(nodes) else None
144+
if next_node is not None and next_node.type in (")", "}"):
145+
continue
140146
string = stringify_single_line_node(node)
141147
if previous and previous.type == ",":
142148
result += " "
@@ -188,9 +194,14 @@ def split_generic_list(middle: list[Node], indent: int, line_length: int) -> lis
188194
lines = split_generic_value(element, indent, line_length)
189195
elements.append(" " * indent + lines[0])
190196
elements.extend(lines[1:])
191-
# Always add a trailing comma on multi-line lists
192-
if elements and not elements[-1].endswith(","):
193-
elements[-1] = elements[-1] + ","
197+
# Always add a trailing comma on multi-line lists, on the last
198+
# non-comment element (so it doesn't end up after a trailing comment).
199+
for i in range(len(elements) - 1, -1, -1):
200+
if elements[i].lstrip().startswith("#"):
201+
continue
202+
if not elements[i].endswith(","):
203+
elements[i] = elements[i] + ","
204+
break
194205
return elements
195206

196207

@@ -371,6 +382,13 @@ def _format_stakeholder_elements(
371382
lines = split_generic_value(node, indent, line_length)
372383
elements.append(" " * indent + lines[0])
373384
elements.extend(lines[1:])
385+
# Always add a trailing comma to the last non-comment element
386+
for i in range(len(elements) - 1, -1, -1):
387+
if elements[i].lstrip().startswith("#"):
388+
continue
389+
if not elements[i].endswith(","):
390+
elements[i] = elements[i] + ","
391+
break
374392
return elements
375393

376394

@@ -450,8 +468,7 @@ def _format_promise(
450468
elements = _format_stakeholder_elements(middle, element_indent, line_length)
451469
fmt.print_lines(elements, indent=0)
452470

453-
has_comments = _stakeholder_has_comments(children)
454-
close_indent = indent + 2 if (attrs or has_comments) else indent
471+
close_indent = indent + 2
455472
if attrs:
456473
fmt.print("}", close_indent)
457474
_format_remaining_children(children, fmt, indent, line_length, macro_indent)

0 commit comments

Comments
 (0)