mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-01 12:25:45 +00:00
Preserve newlines after nested compound statements (#7608)
## Summary
Given:
```python
if True:
if True:
pass
else:
pass
# a
# b
# c
else:
pass
```
We want to preserve the newline after the `# c` (before the `else`).
However, the `last_node` ends at the `pass`, and the comments are
trailing comments on the `pass`, not trailing comments on the
`last_node` (the `if`). As such, when counting the trailing newlines on
the outer `if`, we abort as soon as we see the comment (`# a`).
This PR changes the logic to skip _all_ comments (even those with
newlines between them). This is safe as we know that there are no
"leading" comments on the `else`, so there's no risk of skipping those
accidentally.
Closes https://github.com/astral-sh/ruff/issues/7602.
## Test Plan
No change in compatibility.
Before:
| project | similarity index | total files | changed files |
|--------------|------------------:|------------------:|------------------:|
| cpython | 0.76083 | 1789 | 1631 |
| django | 0.99983 | 2760 | 36 |
| transformers | 0.99963 | 2587 | 319 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99979 | 3496 | 22 |
| warehouse | 0.99967 | 648 | 15 |
| zulip | 0.99972 | 1437 | 21 |
After:
| project | similarity index | total files | changed files |
|--------------|------------------:|------------------:|------------------:|
| cpython | 0.76083 | 1789 | 1631 |
| django | 0.99983 | 2760 | 36 |
| transformers | 0.99963 | 2587 | 319 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99983 | 3496 | 18 |
| warehouse | 0.99967 | 648 | 15 |
| zulip | 0.99972 | 1437 | 21 |
This commit is contained in:
parent
8ce138760a
commit
17ceb5dcb3
8 changed files with 182 additions and 42 deletions
|
|
@ -213,6 +213,31 @@ if True:
|
|||
else:
|
||||
pass
|
||||
|
||||
if True:
|
||||
if True:
|
||||
pass
|
||||
else:
|
||||
pass
|
||||
# a
|
||||
|
||||
# b
|
||||
# c
|
||||
|
||||
else:
|
||||
pass
|
||||
|
||||
if True:
|
||||
if True:
|
||||
pass
|
||||
else:
|
||||
pass
|
||||
|
||||
# b
|
||||
# c
|
||||
|
||||
else:
|
||||
pass
|
||||
|
||||
|
||||
# Regression test for: https://github.com/astral-sh/ruff/issues/7602
|
||||
if True:
|
||||
|
|
@ -493,6 +518,31 @@ if True:
|
|||
else:
|
||||
pass
|
||||
|
||||
if True:
|
||||
if True:
|
||||
pass
|
||||
else:
|
||||
pass
|
||||
# a
|
||||
|
||||
# b
|
||||
# c
|
||||
|
||||
else:
|
||||
pass
|
||||
|
||||
if True:
|
||||
if True:
|
||||
pass
|
||||
else:
|
||||
pass
|
||||
|
||||
# b
|
||||
# c
|
||||
|
||||
else:
|
||||
pass
|
||||
|
||||
|
||||
# Regression test for: https://github.com/astral-sh/ruff/issues/7602
|
||||
if True:
|
||||
|
|
@ -503,7 +553,6 @@ if True:
|
|||
# a
|
||||
# b
|
||||
# c
|
||||
|
||||
else:
|
||||
pass
|
||||
|
||||
|
|
@ -515,6 +564,7 @@ if True:
|
|||
|
||||
# a
|
||||
# c
|
||||
|
||||
else:
|
||||
pass
|
||||
|
||||
|
|
@ -528,7 +578,6 @@ if True:
|
|||
# a
|
||||
# b
|
||||
# c
|
||||
|
||||
else:
|
||||
pass
|
||||
|
||||
|
|
|
|||
|
|
@ -71,6 +71,17 @@ import os
|
|||
|
||||
|
||||
logger = logging.getLogger("FastProject")
|
||||
|
||||
# Regression test for: https://github.com/astral-sh/ruff/issues/7604
|
||||
import os
|
||||
# comment
|
||||
|
||||
# comment
|
||||
|
||||
|
||||
# comment
|
||||
x = 1
|
||||
|
||||
```
|
||||
|
||||
## Output
|
||||
|
|
@ -149,6 +160,16 @@ import os
|
|||
|
||||
|
||||
logger = logging.getLogger("FastProject")
|
||||
|
||||
# Regression test for: https://github.com/astral-sh/ruff/issues/7604
|
||||
import os
|
||||
# comment
|
||||
|
||||
# comment
|
||||
|
||||
|
||||
# comment
|
||||
x = 1
|
||||
```
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue