Skip to content

Commit

Permalink
fix: respect comments indentation in lists.
Browse files Browse the repository at this point in the history
Closes #55
  • Loading branch information
lyz-code committed Feb 5, 2021
1 parent 4173c15 commit cb11882
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/yamlfix/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ def _fix_top_level_lists(source_code: str) -> str:
# Remove the indentation from the line
fixed_source_lines.append(re.sub(rf"^{indent}(.*)", r"\1", line))
elif is_top_level_list:
fixed_source_lines.append(re.sub(rf"^{indent}(.*)", r"\1", line))
# ruyaml doesn't change the indentation of comments
if re.match(r"\s*#.*", line):
fixed_source_lines.append(line)
else:
fixed_source_lines.append(re.sub(rf"^{indent}(.*)", r"\1", line))
else:
return source_code

Expand Down
14 changes: 14 additions & 0 deletions tests/unit/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ def test_fix_code_respects_parent_lists_with_comments() -> None:
assert result == source


def test_fix_code_preserves_indented_comments() -> None:
"""Don't remove indentation from comments in the code."""
source = dedent(
"""\
---
- program:
# Keep comments!"""
)

result = fix_code(source)

assert result == source


def test_fix_code_removes_extra_apostrophes() -> None:
"""Remove not needed apostrophes."""
source = dedent(
Expand Down

0 comments on commit cb11882

Please sign in to comment.