Skip to content

Commit 67e00a9

Browse files
authored
Merge pull request #305 from wsnyder/bug282_hash
fix: disabling `#` reindentation inside quote (#282).
2 parents 60f84da + de50bfb commit 67e00a9

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/yamlfix/adapters.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,15 +579,17 @@ def _fix_comments(self, source_code: str) -> str:
579579

580580
for line in source_code.splitlines():
581581
# Comment at the start of the line
582-
if config.comments_require_starting_space and re.search(r"(^|\s)#\w", line):
583-
line = line.replace("#", "# ")
582+
if config.comments_require_starting_space:
583+
line = re.sub(r"^(|[^\"']*\s)#(\w)", r"\1# \2", line)
584584
# Comment in the middle of the line, but it's not part of a string
585585
if (
586586
config.comments_min_spaces_from_content > 1
587587
and " #" in line
588588
and line[-1] not in ["'", '"']
589589
):
590-
line = re.sub(r"(.+\S)(\s+?)#", rf"\1{comment_start}", line)
590+
line = re.sub(
591+
r"^([^\"']*[^\"' \t])(\s+?)#", rf"\1{comment_start}", line
592+
)
591593
fixed_source_lines.append(line)
592594

593595
return "\n".join(fixed_source_lines)

tests/unit/test_adapter_yaml.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,15 @@ def test_comment_spacing_config(self) -> None:
8080
---
8181
# comment
8282
project_name: yamlfix #comment
83+
"key #1": 'value #2'
8384
"""
8485
)
8586
fixed_source = dedent(
8687
"""\
8788
---
8889
# comment
8990
project_name: yamlfix # comment
91+
'key #1': 'value #2'
9092
"""
9193
)
9294
config = YamlfixConfig()

0 commit comments

Comments
 (0)