Skip to content

Commit 0cd3ebc

Browse files
committed
format.py: Renamed functions / parameters / variables to improve readability
Signed-off-by: Ole Herman Schumacher Elgesem <ole.elgesem@northern.tech>
1 parent c2cfd2f commit 0cd3ebc

2 files changed

Lines changed: 44 additions & 44 deletions

File tree

src/cfengine_cli/format.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def update_previous(self, node):
4949
return tmp
5050

5151

52-
def stringify_children_from_strings(parts):
52+
def stringify_parameter_list(parts):
5353
"""Join pre-extracted string tokens into a formatted parameter list.
5454
5555
Used when formatting bundle/body headers. Comments are
@@ -79,47 +79,47 @@ def stringify_children_from_strings(parts):
7979
return result
8080

8181

82-
def stringify_children(children):
82+
def stringify_single_line_nodes(nodes):
8383
"""Join a list of tree-sitter nodes into a single-line string.
8484
8585
Operates on the direct child nodes of a CFEngine syntax construct
8686
(e.g. a list, call, or attribute). Each child is recursively
87-
flattened via stringify_single_line(). Spacing rules:
87+
flattened via stringify_single_line_node(). Spacing rules:
8888
- A space is inserted after each "," separator.
8989
- A space is inserted before and after "=>" (fat arrow).
9090
- No extra space otherwise (e.g. no space after "(" or before ")").
9191
92-
Used by stringify_single_line() to recursively flatten any node with
92+
Used by stringify_single_line_node() to recursively flatten any node with
9393
children, and by maybe_split_generic_list() to attempt a single-line
9494
rendering before falling back to multi-line splitting.
9595
"""
9696
result = ""
9797
previous = None
98-
for child in children:
99-
string = stringify_single_line(child)
98+
for node in nodes:
99+
string = stringify_single_line_node(node)
100100
if previous and previous.type == ",":
101101
result += " "
102-
if previous and child.type == "=>":
102+
if previous and node.type == "=>":
103103
result += " "
104104
if previous and previous.type == "=>":
105105
result += " "
106106
result += string
107-
previous = child
107+
previous = node
108108
return result
109109

110110

111-
def stringify_single_line(node):
111+
def stringify_single_line_node(node):
112112
if not node.children:
113113
return text(node)
114-
return stringify_children(node.children)
114+
return stringify_single_line_nodes(node.children)
115115

116116

117117
def split_generic_value(node, indent, line_length):
118118
if node.type == "call":
119119
return split_rval_call(node, indent, line_length)
120120
if node.type == "list":
121121
return split_rval_list(node, indent, line_length)
122-
return [stringify_single_line(node)]
122+
return [stringify_single_line_node(node)]
123123

124124

125125
def split_generic_list(middle, indent, line_length):
@@ -128,7 +128,7 @@ def split_generic_list(middle, indent, line_length):
128128
if elements and element.type == ",":
129129
elements[-1] = elements[-1] + ","
130130
continue
131-
line = " " * indent + stringify_single_line(element)
131+
line = " " * indent + stringify_single_line_node(element)
132132
if len(line) < line_length:
133133
elements.append(line)
134134
else:
@@ -139,7 +139,7 @@ def split_generic_list(middle, indent, line_length):
139139

140140

141141
def maybe_split_generic_list(nodes, indent, line_length):
142-
string = " " * indent + stringify_children(nodes)
142+
string = " " * indent + stringify_single_line_nodes(nodes)
143143
if len(string) < line_length:
144144
return [string]
145145
return split_generic_list(nodes, indent, line_length)
@@ -171,11 +171,11 @@ def split_rval(node, indent, line_length):
171171
return split_rval_list(node, indent, line_length)
172172
if node.type == "call":
173173
return split_rval_call(node, indent, line_length)
174-
return [stringify_single_line(node)]
174+
return [stringify_single_line_node(node)]
175175

176176

177177
def maybe_split_rval(node, indent, offset, line_length):
178-
line = stringify_single_line(node)
178+
line = stringify_single_line_node(node)
179179
if len(line) + offset < line_length:
180180
return [line]
181181
return split_rval(node, indent, line_length)
@@ -193,11 +193,11 @@ def attempt_split_attribute(node, indent, line_length):
193193
lines = maybe_split_rval(rval, indent, offset, line_length)
194194
lines[0] = prefix + lines[0]
195195
return lines
196-
return [" " * indent + stringify_single_line(node)]
196+
return [" " * indent + stringify_single_line_node(node)]
197197

198198

199199
def stringify(node, indent, line_length):
200-
single_line = " " * indent + stringify_single_line(node)
200+
single_line = " " * indent + stringify_single_line_node(node)
201201
# Reserve 1 char for trailing ; or , after attributes
202202
effective_length = line_length - 1 if node.type == "attribute" else line_length
203203
if len(single_line) < effective_length:
@@ -233,7 +233,7 @@ def autoformat(node, fmt, line_length, macro_indent, indent=0):
233233
else:
234234
parts.append(text(p))
235235
# Append directly to previous part (no space before parens)
236-
header_parts[-1] = header_parts[-1] + stringify_children_from_strings(
236+
header_parts[-1] = header_parts[-1] + stringify_parameter_list(
237237
parts
238238
)
239239
else:
@@ -273,7 +273,7 @@ def autoformat(node, fmt, line_length, macro_indent, indent=0):
273273
line = (
274274
text(promiser_node)
275275
+ " "
276-
+ stringify_single_line(attr_children[0])
276+
+ stringify_single_line_node(attr_children[0])
277277
+ ";"
278278
)
279279
if indent + len(line) <= line_length:

tests/unit/test_format.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
1-
from cfengine_cli.format import stringify_children_from_strings, stringify_children
1+
from cfengine_cli.format import stringify_parameter_list, stringify_single_line_nodes
22

33

44
class MockNode:
5-
"""Minimal stand-in for a tree-sitter Node used by stringify_children."""
5+
"""Minimal stand-in for a tree-sitter Node used by stringify_single_line_nodes."""
66

77
def __init__(self, node_type, node_text=None, children=None):
88
self.type = node_type
99
self.text = node_text.encode("utf-8") if node_text is not None else None
1010
self.children = children or []
1111

12+
1213
def _leaf(node_type, node_text=None):
1314
return MockNode(node_type, node_text or node_type)
1415

15-
def test_stringify_children_from_strings():
16-
assert stringify_children_from_strings([]) == ""
17-
assert stringify_children_from_strings(["foo"]) == "foo"
18-
assert stringify_children_from_strings(["(", "a", ")"]) == "(a)"
19-
assert stringify_children_from_strings(["(", "a", ",", "b", ")"]) == "(a, b)"
20-
assert stringify_children_from_strings(["(", "a", ",", ")"]) == "(a)"
21-
assert (
22-
stringify_children_from_strings(["(", "a", ",", "b", ",", ")"])
23-
== "(a, b)"
24-
)
25-
assert stringify_children_from_strings(["a", "b", "c"]) == "a b c"
26-
assert stringify_children_from_strings(["a", ",", "b"]) == "a, b"
27-
assert stringify_children_from_strings(["(", ")"]) == "()"
16+
17+
def test_stringify_parameter_list():
18+
assert stringify_parameter_list([]) == ""
19+
assert stringify_parameter_list(["foo"]) == "foo"
20+
assert stringify_parameter_list(["(", "a", ")"]) == "(a)"
21+
assert stringify_parameter_list(["(", "a", ",", "b", ")"]) == "(a, b)"
22+
assert stringify_parameter_list(["(", "a", ",", ")"]) == "(a)"
23+
assert stringify_parameter_list(["(", "a", ",", "b", ",", ")"]) == "(a, b)"
24+
assert stringify_parameter_list(["a", "b", "c"]) == "a b c"
25+
assert stringify_parameter_list(["a", ",", "b"]) == "a, b"
26+
assert stringify_parameter_list(["(", ")"]) == "()"
2827
parts = ["(", "x", ",", "y", ",", "z", ")"]
29-
assert stringify_children_from_strings(parts) == "(x, y, z)"
28+
assert stringify_parameter_list(parts) == "(x, y, z)"
29+
3030

31-
def test_stringify_children():
32-
assert stringify_children([]) == ""
33-
assert stringify_children([_leaf("identifier", "foo")]) == "foo"
31+
def test_stringify_single_line_nodes():
32+
assert stringify_single_line_nodes([]) == ""
33+
assert stringify_single_line_nodes([_leaf("identifier", "foo")]) == "foo"
3434

3535
nodes = [_leaf("string", '"a"'), _leaf(","), _leaf("string", '"b"')]
36-
assert stringify_children(nodes) == '"a", "b"'
36+
assert stringify_single_line_nodes(nodes) == '"a", "b"'
3737

3838
nodes = [_leaf("identifier", "lval"), _leaf("=>"), _leaf("string", '"rval"')]
39-
assert stringify_children(nodes) == 'lval => "rval"'
39+
assert stringify_single_line_nodes(nodes) == 'lval => "rval"'
4040

4141
nodes = [_leaf("("), _leaf("identifier", "x"), _leaf(")")]
42-
assert stringify_children(nodes) == "(x)"
42+
assert stringify_single_line_nodes(nodes) == "(x)"
4343

4444
nodes = [
4545
_leaf("{"),
@@ -48,14 +48,14 @@ def test_stringify_children():
4848
_leaf("string", '"b"'),
4949
_leaf("}"),
5050
]
51-
assert stringify_children(nodes) == '{"a", "b"}'
51+
assert stringify_single_line_nodes(nodes) == '{"a", "b"}'
5252
nodes = [
5353
_leaf("identifier", "package_name"),
5454
_leaf("=>"),
5555
_leaf("string", '"nginx"'),
5656
]
5757

58-
assert stringify_children(nodes) == 'package_name => "nginx"'
58+
assert stringify_single_line_nodes(nodes) == 'package_name => "nginx"'
5959
inner = MockNode(
6060
"call",
6161
children=[
@@ -67,4 +67,4 @@ def test_stringify_children():
6767
)
6868

6969
nodes = [_leaf("identifier", "x"), _leaf("=>"), inner]
70-
assert stringify_children(nodes) == 'x => func("arg")'
70+
assert stringify_single_line_nodes(nodes) == 'x => func("arg")'

0 commit comments

Comments
 (0)