cfengine format: Refactored code, added type hints and docstrings, expanded tests, and fixed small issues#55
Conversation
74de2df to
b829661
Compare
04f4df3 to
4f76e8b
Compare
|
Thanks for submitting a pull request! Maybe @craigcomstock can review this? |
634ef92 to
b8a080d
Compare
18d010a to
be21cdc
Compare
d5e3685 to
be21cdc
Compare
src/cfengine_cli/format.py
Outdated
|
|
||
|
|
||
| def _has_trailing_comma(middle): | ||
| """Check if list middle nodes end with a trailing comma.""" |
There was a problem hiding this comment.
middle? I know this is just a function parameter name but seems like it should be more like just list_of_nodes or something like that.
There was a problem hiding this comment.
With this tree-sitter code you often have to consider what the list of nodes looks like - it has commas and curly braces as separate elements.
The reason for the name is that you have a list of nodes like;
nodes = ["{", "a", ",", "b", "}"]So nodes[1:-1] are referred to as middle (what's left after removing the ends on each side).
There was a problem hiding this comment.
This is the kind of things that I would like to see in the doc string. I.e., the parameter middle contains tokens between the two curly braces of a list.
| return True | ||
|
|
||
| # Multi-line with split stakeholder | ||
| if _has_stakeholder(children) and _stakeholder_needs_splitting( |
There was a problem hiding this comment.
Shouldn't the list of stakeholders be reformatted for trailing comma even if the stakeholder has no comments which is the condition included invisibly by _stakeholder_needs_splitting() as I read it.
My example I was trying was
bundle agent main
{
reports: "hi" -> { "yoda", "boba", };
}
And was surprised to not see some of this code exercised.
There was a problem hiding this comment.
This is a bug I can fix. Adding / removing commas is a bit more "tricky" than the other reformatting we do, since commas are nodes in the syntax tree, and since comment nodes can occur pretty much everywhere.
| """Format the middle elements (between { and }) of a stakeholder list.""" | ||
| if not any(n.type == "comment" for n in middle): | ||
| if _has_trailing_comma(middle): | ||
| return split_generic_list(middle, indent, line_length) |
There was a problem hiding this comment.
I don't see these functions properly doing what they say: always add trailing comma in multi-line lists:
191 # Always add a trailing comma on multi-line lists
192 if elements and not elements[-1].endswith(","):
193 elements[-1] = elements[-1] + ","
An example:
bundle agent main
{
reports:
"hi" -> {
"yoda", # this is yoda of course
"boba" # and yet another star wars character
};
}
Is formatted as:
bundle agent main
{
reports:
"hi" -> {
"yoda",
# this is yoda of course
"boba"
# and yet another star wars character
};
}
And it would seem according to the comment in the code I mention above that "boba" should be followed by a comma.
| return False | ||
| prefix = promiser_line(children) | ||
| if not prefix: | ||
| line = _promiser_line_with_stakeholder(children) |
There was a problem hiding this comment.
Do we have code coverage maybe? I am not sure when this case would be reached. Seems like most cases would pop out above at line 361:
if _has_stakeholder(children) and _stakeholder_needs_splitting(
There was a problem hiding this comment.
I can add comments to make it as clear as possible.
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Ole Herman Schumacher Elgesem <ole.elgesem@northern.tech>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Ole Herman Schumacher Elgesem <ole.elgesem@northern.tech>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Ole Herman Schumacher Elgesem <ole.elgesem@northern.tech>
Other PRs were merged to master, and so there are test failures / linting errors I had to fix now. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Ole Herman Schumacher Elgesem <ole.elgesem@northern.tech>
JSON code was returning 1 (error) for case where file was reformatted. Also ended up refactoring a bit to make this as clear as possible. Signed-off-by: Ole Herman Schumacher Elgesem <ole@northern.tech>
…ilining Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Ole Herman Schumacher Elgesem <ole@northern.tech>
Signed-off-by: Ole Herman Schumacher Elgesem <ole@northern.tech>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Ole Herman Schumacher Elgesem <ole@northern.tech>
be21cdc to
88ff48d
Compare
Signed-off-by: Ole Herman Schumacher Elgesem <ole@northern.tech>
Signed-off-by: Ole Herman Schumacher Elgesem <ole@northern.tech>
| if root.type == node_type: | ||
| return root | ||
| for child in root.children: | ||
| found = _find_opt(child, node_type) | ||
| if found: | ||
| return found |
There was a problem hiding this comment.
Is it not sufficient to just call the _find_opt() and check the returned value?
| raise ValueError(f"No node of type {node_type!r} found") | ||
|
|
||
|
|
||
| def _find_opt(root: Node, node_type: str): |
There was a problem hiding this comment.
Maybe there is a better name that can be used here? E.g., _find_or_none
| def test_maybe_split_rval_too_long(): | ||
| root = _parse('bundle agent x { vars: "v" slist => { "a", "b" }; }') | ||
| list_node = _find(root, "list") | ||
| result = maybe_split_rval(list_node, 6, 999, 80) |
There was a problem hiding this comment.
It would be nice if you'd suffix the argument name, so that it is clear what these numbers are.
|
I would just merge and summit follow-up PRs if you want to change anything more now. |
No description provided.