Skip to content

Commit

Permalink
fix: ignore empty lines when searching for not-comment line
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry authored and t3rn0 committed Oct 25, 2023
1 parent 6169092 commit f9b0221
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ast_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ def _extend_interval(interval: _t.Tuple[int, int], code: str) -> _t.Tuple[int, i
# In each block there must be at least one, otherwise the code is not valid
def _get_first_line_not_comment(lines: _t.List[str]):
for line in lines:
if not line.strip():
continue
if not re.match(r"^ *#.*", line):
return line
return ""
Expand Down
17 changes: 17 additions & 0 deletions test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,23 @@ def test_comment_to_multiline_expr():
assert body_nodes[1].inline


def test_empty_line_not_affect_comment_placement():
"""Empty line doesn't mess with indendation intervals."""
source = dedent(
"""
# comment 1
if a: # comment 2
pass
"""
)
body = parse(source).body
assert len(body) == 2
assert isinstance(body[0], Comment)
if_body = body[1].body
assert isinstance(if_body[0], Comment)


@pytest.mark.xfail(reason="https://github.com/t3rn0/ast-comments/issues/13")
def test_comment_in_multilined_list():
"""Comment to element of the list stays inside the list."""
Expand Down
13 changes: 13 additions & 0 deletions test_unparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,19 @@ def test_comment_to_multiline_expr():
_test_unparse(source)


def test_empty_line_not_affect_comment_placement():
"""Empty line doesn't mess with indendation intervals."""
source = dedent(
"""
# comment 1
if a: # comment 2
pass
"""
)
_test_unparse(source)


@pytest.mark.xfail(reason="https://github.com/t3rn0/ast-comments/issues/13")
def test_comment_in_multilined_list():
source = dedent(
Expand Down

0 comments on commit f9b0221

Please sign in to comment.