Skip to content

Commit 133de4a

Browse files
authored
Merge pull request #69 from olehermanse/trailing
cfengine format: Removed trailing commas where they're not supported
2 parents 6793970 + beef613 commit 133de4a

3 files changed

Lines changed: 60 additions & 14 deletions

File tree

CLAUDE.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# CFEngine CLI information for LLMs
2+
3+
## Fix the implementation
4+
5+
In general, when the prompter asks you to fix the implementation, this means that they have adjusted the tests already and they want you to fix the implementation.
6+
Typically you should not touch the tests in this case, unless there is something obviously wrong in them, like a typo.
7+
The first step to identify what is necessary should be to run the tests and see which ones are failing.
8+
9+
## Running tests
10+
11+
In general, the main command to run for testing is:
12+
13+
```bash
14+
make check
15+
```
16+
17+
This will run all the test suites.
18+
19+
## Running python tools
20+
21+
This project uses `uv`.
22+
That means that you should not run `python`, `python3`, `pip`, `pip3` directly.
23+
Instead, run the appropriate uv command to ensure we're using the right python and the right dependencies.
24+
25+
## Pointers for the source code
26+
27+
When fixing issues, these are usually the files to look at:
28+
29+
- The implementation of `cfengine format` is in `src/cfengine_cli/format.py`.
30+
- The implementation of `cfengine lint` is in `src/cfengine_cli/lint.py`.
31+
32+
## Test suites
33+
34+
As mentioned above, the `make check` command runs all the tests.
35+
We have different suites:
36+
37+
- Unit tests in `tests/unit` test individual python functions.
38+
- Formatting tests in `tests/format` test the formatter (`cfengine format`).
39+
- Linting tests in `tests/lint` test the linter.
40+
- Shell tests in `tests/shell` tests various subcommands and the tool as a whole in an end-to-end fashion.

src/cfengine_cli/format.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ def split_generic_value(node: Node, indent: int, line_length: int) -> list[str]:
182182
return [stringify_single_line_node(node)]
183183

184184

185-
def split_generic_list(middle: list[Node], indent: int, line_length: int) -> list[str]:
185+
def split_generic_list(
186+
middle: list[Node], indent: int, line_length: int, trailing_comma: bool = True
187+
) -> list[str]:
186188
"""Split list elements into one-per-line strings, each pre-indented."""
187189
elements: list[str] = []
188190
for element in middle:
@@ -196,25 +198,27 @@ def split_generic_list(middle: list[Node], indent: int, line_length: int) -> lis
196198
lines = split_generic_value(element, indent, line_length)
197199
elements.append(" " * indent + lines[0])
198200
elements.extend(lines[1:])
199-
# Always add a trailing comma on multi-line lists, on the last
201+
# Ensure trailing comma state matches the desired setting, on the last
200202
# non-comment element (so it doesn't end up after a trailing comment).
201203
for i in range(len(elements) - 1, -1, -1):
202204
if elements[i].lstrip().startswith("#"):
203205
continue
204-
if not elements[i].endswith(","):
206+
if trailing_comma and not elements[i].endswith(","):
205207
elements[i] = elements[i] + ","
208+
elif not trailing_comma and elements[i].endswith(","):
209+
elements[i] = elements[i][:-1]
206210
break
207211
return elements
208212

209213

210214
def maybe_split_generic_list(
211-
nodes: list[Node], indent: int, line_length: int
215+
nodes: list[Node], indent: int, line_length: int, trailing_comma: bool = True
212216
) -> list[str]:
213217
"""Try a single-line rendering; fall back to split_generic_list if too long."""
214218
string = " " * indent + stringify_single_line_nodes(nodes)
215219
if len(string) < line_length:
216220
return [string]
217-
return split_generic_list(nodes, indent, line_length)
221+
return split_generic_list(nodes, indent, line_length, trailing_comma)
218222

219223

220224
def split_rval_list(node: Node, indent: int, line_length: int) -> list[str]:
@@ -236,7 +240,9 @@ def split_rval_call(node: Node, indent: int, line_length: int) -> list[str]:
236240
first = text(node.children[0]) + "("
237241
last = " " * indent + text(node.children[-1])
238242
middle = node.children[2:-1]
239-
elements = maybe_split_generic_list(middle, indent + 2, line_length)
243+
elements = maybe_split_generic_list(
244+
middle, indent + 2, line_length, trailing_comma=False
245+
)
240246
return [first, *elements, last]
241247

242248

tests/format/003_wrapping.expected.cf

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ bundle agent function_calls
7070
"seven",
7171
"eight",
7272
"nine",
73-
"ten",
73+
"ten"
7474
);
7575
}
7676

@@ -90,15 +90,15 @@ bundle agent nested_function_calls
9090
"variable_name"
9191
string => concat(
9292
"one",
93-
concat("two", "three", "four", "five", "six", "seven", "eight", ""),
93+
concat("two", "three", "four", "five", "six", "seven", "eight", "")
9494
);
9595

9696
"variable_name"
9797
string => concat(
9898
"one",
9999
concat(
100100
"two", "three", "four", "five", "six", "seven", "eight", "nine", ""
101-
),
101+
)
102102
);
103103

104104
"variable_name"
@@ -113,24 +113,24 @@ bundle agent nested_function_calls
113113
"seven",
114114
"eight",
115115
"nine",
116-
"ten",
117-
),
116+
"ten"
117+
)
118118
);
119119

120120
"variable_name"
121121
string => concat(
122122
"one",
123123
"two",
124124
"three",
125-
concat("two", "three", "four", "five", "six", "seven", "eight"),
125+
concat("two", "three", "four", "five", "six", "seven", "eight")
126126
);
127127

128128
"variable_name"
129129
string => concat(
130130
"one",
131131
"two",
132132
"three",
133-
concat("two", "three", "four", "five", "six", "seven", "eight"),
133+
concat("two", "three", "four", "five", "six", "seven", "eight")
134134
);
135135

136136
"variable_name"
@@ -139,7 +139,7 @@ bundle agent nested_function_calls
139139
"two",
140140
"three",
141141
# comment a
142-
concat("two", "three", "four", "five", "six", "seven", "eight"),
142+
concat("two", "three", "four", "five", "six", "seven", "eight")
143143
# comment b
144144
);
145145
}

0 commit comments

Comments
 (0)