Skip to content
Merged
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
2 changes: 0 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ jobs:
run: make lint
- name: Test type checkers
run: make mypy
- name: Test security
run: make security
- name: Test with pytest
run: make test
- name: Upload Coverage
Expand Down
8 changes: 5 additions & 3 deletions src/yamlfix/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,15 +579,17 @@ def _fix_comments(self, source_code: str) -> str:

for line in source_code.splitlines():
# Comment at the start of the line
if config.comments_require_starting_space and re.search(r"(^|\s)#\w", line):
line = line.replace("#", "# ")
if config.comments_require_starting_space:
line = re.sub(r"^(|[^\"']*\s)#(\w)", r"\1# \2", line)
# Comment in the middle of the line, but it's not part of a string
if (
config.comments_min_spaces_from_content > 1
and " #" in line
and line[-1] not in ["'", '"']
):
line = re.sub(r"(.+\S)(\s+?)#", rf"\1{comment_start}", line)
line = re.sub(
r"^([^\"']*[^\"' \t])(\s+?)#", rf"\1{comment_start}", line
)
fixed_source_lines.append(line)

return "\n".join(fixed_source_lines)
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/test_adapter_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@ def test_comment_spacing_config(self) -> None:
---
# comment
project_name: yamlfix #comment
"key #1": 'value #2'
"""
)
fixed_source = dedent(
"""\
---
# comment
project_name: yamlfix # comment
'key #1': 'value #2'
"""
)
config = YamlfixConfig()
Expand Down