ruff/crates
Charlie Marsh 17ceb5dcb3
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 |
2023-09-25 14:21:44 +00:00
..
flake8_to_ruff Bump version to v0.0.291 (#7606) 2023-09-22 13:25:37 -04:00
ruff_benchmark Introduce LinterSettings 2023-09-20 17:02:34 +02:00
ruff_cache Introduce FormatterSettings (#7545) 2023-09-21 08:01:24 +02:00
ruff_cli Bump version to v0.0.291 (#7606) 2023-09-22 13:25:37 -04:00
ruff_dev Support option group documentation (#7593) 2023-09-22 16:31:52 +00:00
ruff_diagnostics Create ruff_notebook crate (#7039) 2023-09-01 13:56:44 +00:00
ruff_formatter Add most formatter options to ruff.toml / pyproject.toml (#7566) 2023-09-22 15:47:57 +00:00
ruff_index Add unreachable code rule (#5384) 2023-07-04 14:27:23 +00:00
ruff_linter Bump semver from 1.0.18 to 1.0.19 (#7641) 2023-09-25 09:09:17 +00:00
ruff_macros Support option group documentation (#7593) 2023-09-22 16:31:52 +00:00
ruff_notebook chore: Upgrade pyproject-toml crate (#7335) 2023-09-13 17:55:03 +02:00
ruff_python_ast Remove Int wrapper type from parser (#7577) 2023-09-21 17:01:44 +00:00
ruff_python_codegen Remove Int wrapper type from parser (#7577) 2023-09-21 17:01:44 +00:00
ruff_python_formatter Preserve newlines after nested compound statements (#7608) 2023-09-25 14:21:44 +00:00
ruff_python_index Remove triple-quoted string ranges computation (#7476) 2023-09-18 20:57:49 +05:30
ruff_python_literal Avoid parsing other parts of a format specification if replacements are present (#6858) 2023-08-25 17:42:57 +00:00
ruff_python_parser Emit LexError for dedent to incorrect level (#7638) 2023-09-25 11:45:44 +01:00
ruff_python_resolver Replace .map_or(false, $closure) with .is_some_and(closure) (#6244) 2023-08-01 19:29:42 +02:00
ruff_python_semantic Detect asyncio.get_running_loop calls in RUF006 (#7562) 2023-09-21 04:37:38 +00:00
ruff_python_stdlib Use u8 to represent version segments (#7578) 2023-09-21 14:24:51 -04:00
ruff_python_trivia Preserve newlines after nested compound statements (#7608) 2023-09-25 14:21:44 +00:00
ruff_shrinking Bump shlex from 1.1.0 to 1.2.0 (#7381) 2023-09-14 09:40:05 -05:00
ruff_source_file Skip BOM when inserting start-of-file imports (#7622) 2023-09-23 19:36:50 +00:00
ruff_text_size Unify line size settings between ruff and the formatter (#6873) 2023-08-28 06:44:56 +00:00
ruff_wasm Add most formatter options to ruff.toml / pyproject.toml (#7566) 2023-09-22 15:47:57 +00:00
ruff_workspace Fix line ending doc typo (#7611) 2023-09-22 20:16:41 +00:00