Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ repos:
exclude: >
(?x)^(
examples/playbooks/templates/.*|
examples/playbooks/transform-yaml-comments.yml|
examples/yamllint/.*|
examples/other/some.j2.yaml|
examples/playbooks/collections/.*|
Expand Down
8 changes: 8 additions & 0 deletions examples/playbooks/transform-yaml-comments.transformed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# comment without space
- name: Fixture
hosts: localhost
tasks:
- name: Task
ansible.builtin.debug:
msg: hello # inline without space
8 changes: 8 additions & 0 deletions examples/playbooks/transform-yaml-comments.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
#comment without space
- name: Fixture
hosts: localhost
tasks:
- name: Task
ansible.builtin.debug:
msg: hello #inline without space
5 changes: 2 additions & 3 deletions src/ansiblelint/rules/yaml_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ def transform(
:param lintable: Lintable instance
:param data: data to transform
"""
# This method does nothing because the YAML reformatting is implemented
# in data dumper. Still presence of this method helps us with
# documentation generation.
if match.tag == "yaml[comments]":
match.fixed = True


# testing code to be loaded only with pytest or when executed the rule file
Expand Down
5 changes: 5 additions & 0 deletions src/ansiblelint/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,11 @@ def write_comment(
else:
# single blank lines in post comments
value = self._re_repeat_blank_lines.sub("\n\n", value)

# make sure that comments have a space after #
if value.startswith("#") and not value.startswith("# ") and len(value) > 1:
value = "# " + value[1:]

comment.value = value

# make sure that the eol comment only has one space before it.
Expand Down
7 changes: 7 additions & 0 deletions test/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ def fixture_runner_result(
True,
id="name-capitalize",
),
pytest.param(
"examples/playbooks/transform-yaml-comments.yml",
2,
True,
True,
id="yaml-comments",
),
),
)
@mock.patch.dict(os.environ, {"ANSIBLE_LINT_WRITE_TMP": "1"}, clear=True)
Expand Down
Loading