Skip to content

Commit fe611e7

Browse files
committed
cfengine format: Added empty lines before comments in bodies
Signed-off-by: Ole Herman Schumacher Elgesem <ole@northern.tech>
1 parent 19fe733 commit fe611e7

3 files changed

Lines changed: 19 additions & 8 deletions

File tree

src/cfengine_cli/format.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -777,14 +777,17 @@ def _needs_blank_line_before(child: Node, indent: int, line_length: int) -> bool
777777
return True
778778
if parent and parent.type in {"bundle_section", "class_guarded_promises"}:
779779
return prev.type in {"promise", "half_promise"} | CLASS_GUARD_TYPES
780-
if parent and parent.type in {"body_block_body", "promise_block_body"}:
781-
next_sib = _skip_comments(child.next_named_sibling, "next")
782-
if next_sib is None:
783-
return False
784-
# Leading comment for a class-guarded section preceded by
785-
# content above it.
786-
if next_sib.type in CLASS_GUARD_TYPES:
787-
return prev.type in CLASS_GUARD_TYPES | {"attribute"}
780+
if parent and parent.type in {
781+
"body_block_body",
782+
"promise_block_body",
783+
"class_guarded_body_attributes",
784+
}:
785+
# A comment is separated by a blank line from a preceding
786+
# attribute or a preceding class-guarded block, so it reads as
787+
# a leading comment for what follows (or a trailing comment for
788+
# the block). This mirrors how a comment after a promise is
789+
# separated in bundles.
790+
return prev.type == "attribute" or prev.type in CLASS_GUARD_TYPES
788791
return False
789792

790793
return False

tests/format/004_comments.expected.cf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,22 @@ body package_method apt
8383
package_list_command => "/usr/bin/dpkg -l";
8484
package_list_name_regex => "ii\s+([^\s]+).*";
8585
package_list_version_regex => "ii\s+[^\s]+\s+([^\s]+).*";
86+
8687
# package_list_arch_regex => "none";
8788
package_installed_regex => ".*";
89+
8890
# all reported are installed:
8991
# package_name_convention => "$(name)_$(version)_$(arch)";
9092
package_name_convention => "$(name)";
93+
9194
# Use these only if not using a separate version/arch string:
9295
# package_version_regex => "";
9396
# package_name_regex => "";
9497
# package_arch_regex => "";
9598
package_add_command => "/usr/bin/apt-get --yes install";
9699
package_delete_command => "/usr/bin/apt-get --yes remove";
97100
package_update_command => "/usr/bin/apt-get --yes dist-upgrade";
101+
98102
# package_verify_command => "/bin/rpm -V";
99103
}
100104

@@ -107,13 +111,15 @@ body package_method ca
107111
body package_method ac
108112
{
109113
package_changes => "bulk";
114+
110115
# bar
111116
}
112117

113118
body package_method cac
114119
{
115120
# foo
116121
package_changes => "bulk";
122+
117123
# bar
118124
}
119125

@@ -129,6 +135,7 @@ body package_method cgcac
129135
linux::
130136
# bar
131137
package_changes => "bulk";
138+
132139
# baz
133140
}
134141

tests/format/011_promises.expected.cf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ body common control
120120
any::
121121
ignore_missing_bundles => "$(def.control_common_ignore_missing_bundles)";
122122
ignore_missing_inputs => "$(def.control_common_ignore_missing_inputs)";
123+
123124
# The number of minutes after which last-seen entries are purged from cf_lastseen.lmdb
124125
lastseenexpireafter => "$(def.control_common_lastseenexpireafter)";
125126

0 commit comments

Comments
 (0)