ruff/crates/ruff_python_formatter/tests/snapshots
konsti 730e6b2b4c
Refactor StmtIf: Formatter and Linter (#5459)
## Summary

Previously, `StmtIf` was defined recursively as
```rust
pub struct StmtIf {
    pub range: TextRange,
    pub test: Box<Expr>,
    pub body: Vec<Stmt>,
    pub orelse: Vec<Stmt>,
}
```
Every `elif` was represented as an `orelse` with a single `StmtIf`. This
means that this representation couldn't differentiate between
```python
if cond1:
    x = 1
else:
    if cond2:
        x = 2
```
and 
```python
if cond1:
    x = 1
elif cond2:
    x = 2
```
It also makes many checks harder than they need to be because we have to
recurse just to iterate over an entire if-elif-else and because we're
lacking nodes and ranges on the `elif` and `else` branches.

We change the representation to a flat

```rust
pub struct StmtIf {
    pub range: TextRange,
    pub test: Box<Expr>,
    pub body: Vec<Stmt>,
    pub elif_else_clauses: Vec<ElifElseClause>,
}

pub struct ElifElseClause {
    pub range: TextRange,
    pub test: Option<Expr>,
    pub body: Vec<Stmt>,
}
```
where `test: Some(_)` represents an `elif` and `test: None` an else.

This representation is different tradeoff, e.g. we need to allocate the
`Vec<ElifElseClause>`, the `elif`s are now different than the `if`s
(which matters in rules where want to check both `if`s and `elif`s) and
the type system doesn't guarantee that the `test: None` else is actually
last. We're also now a bit more inconsistent since all other `else`,
those from `for`, `while` and `try`, still don't have nodes. With the
new representation some things became easier, e.g. finding the `elif`
token (we can use the start of the `ElifElseClause`) and formatting
comments for if-elif-else (no more dangling comments splitting, we only
have to insert the dangling comment after the colon manually and set
`leading_alternate_branch_comments`, everything else is taken of by
having nodes for each branch and the usual placement.rs fixups).

## Merge Plan

This PR requires coordination between the parser repo and the main ruff
repo. I've split the ruff part, into two stacked PRs which have to be
merged together (only the second one fixes all tests), the first for the
formatter to be reviewed by @michareiser and the second for the linter
to be reviewed by @charliermarsh.

* MH: Review and merge
https://github.com/astral-sh/RustPython-Parser/pull/20
* MH: Review and merge or move later in stack
https://github.com/astral-sh/RustPython-Parser/pull/21
* MH: Review and approve
https://github.com/astral-sh/RustPython-Parser/pull/22
* MH: Review and approve formatter PR
https://github.com/astral-sh/ruff/pull/5459
* CM: Review and approve linter PR
https://github.com/astral-sh/ruff/pull/5460
* Merge linter PR in formatter PR, fix ecosystem checks (ecosystem
checks can't run on the formatter PR and won't run on the linter PR, so
we need to merge them first)
 * Merge https://github.com/astral-sh/RustPython-Parser/pull/22
 * Create tag in the parser, update linter+formatter PR
 * Merge linter+formatter PR https://github.com/astral-sh/ruff/pull/5459

---------

Co-authored-by: Micha Reiser <micha@reiser.io>
2023-07-18 13:40:15 +02:00
..
black_compatibility@conditional_expression.py.snap Format ExprIfExp (ternary operator) (#5597) 2023-07-07 19:11:52 +00:00
black_compatibility@miscellaneous__blackd_diff.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@miscellaneous__debug_visitor.py.snap Formatter: Better f-string dummy (#5730) 2023-07-13 09:27:25 +00:00
black_compatibility@miscellaneous__decorators.py.snap Format ExpressionStarred nodes (#5654) 2023-07-11 06:08:08 +00:00
black_compatibility@miscellaneous__docstring_no_string_normalization.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@miscellaneous__docstring_preview_no_string_normalization.py.snap Formatter: Better f-string dummy (#5730) 2023-07-13 09:27:25 +00:00
black_compatibility@miscellaneous__force_pyi.py.snap Format target: annotation = value? expressions (#5661) 2023-07-11 16:40:28 +02:00
black_compatibility@miscellaneous__long_strings_flag_disabled.py.snap Format assert statement (#5168) 2023-07-14 09:01:33 +02:00
black_compatibility@miscellaneous__power_op_newline.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@miscellaneous__string_quotes.py.snap Formatter: Better f-string dummy (#5730) 2023-07-13 09:27:25 +00:00
black_compatibility@py_36__numeric_literals.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@py_36__numeric_literals_skip_underscores.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@py_37__python37.py.snap Improve comprehension line break beheavior 2023-07-11 16:51:24 +02:00
black_compatibility@py_38__pep_570.py.snap Change lambda dummy to NOT_YET_IMPLEMENTED_lambda (#5687) 2023-07-11 13:16:18 +00:00
black_compatibility@py_38__pep_572.py.snap Handle right parens in join comma builder (#5711) 2023-07-12 18:21:28 +02:00
black_compatibility@py_38__python38.py.snap Format target: annotation = value? expressions (#5661) 2023-07-11 16:40:28 +02:00
black_compatibility@py_39__pep_572_py39.py.snap Format SetComp (#5774) 2023-07-15 15:50:47 +01:00
black_compatibility@py_39__python39.py.snap Format named expressions (walrus operator) (#5642) 2023-07-10 12:32:15 +00:00
black_compatibility@py_310__pattern_matching_complex.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@py_310__pattern_matching_extras.py.snap Format target: annotation = value? expressions (#5661) 2023-07-11 16:40:28 +02:00
black_compatibility@py_310__pattern_matching_generic.py.snap Formatter: Better f-string dummy (#5730) 2023-07-13 09:27:25 +00:00
black_compatibility@py_310__pattern_matching_simple.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@py_310__pattern_matching_style.py.snap Don't add a magic trailing comma for a single entry (#5463) 2023-07-03 21:48:44 +02:00
black_compatibility@py_310__pep_572_py310.py.snap Format named expressions (walrus operator) (#5642) 2023-07-10 12:32:15 +00:00
black_compatibility@py_310__remove_newline_after_match.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@simple_cases__attribute_access_on_number_literals.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@simple_cases__bracketmatch.py.snap Change lambda dummy to NOT_YET_IMPLEMENTED_lambda (#5687) 2023-07-11 13:16:18 +00:00
black_compatibility@simple_cases__class_methods_new_line.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@simple_cases__comment_after_escaped_newline.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@simple_cases__comments2.py.snap Improve comprehension line break beheavior 2023-07-11 16:51:24 +02:00
black_compatibility@simple_cases__comments4.py.snap Pass parent to NeedsParentheses (#5708) 2023-07-13 08:57:29 +02:00
black_compatibility@simple_cases__comments6.py.snap Format ExpressionStarred nodes (#5654) 2023-07-11 06:08:08 +00:00
black_compatibility@simple_cases__comments9.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@simple_cases__comments_non_breaking_space.py.snap Format import statements (#5493) 2023-07-04 07:07:20 +00:00
black_compatibility@simple_cases__composition.py.snap Format assert statement (#5168) 2023-07-14 09:01:33 +02:00
black_compatibility@simple_cases__composition_no_trailing_comma.py.snap Format assert statement (#5168) 2023-07-14 09:01:33 +02:00
black_compatibility@simple_cases__docstring.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@simple_cases__docstring_preview.py.snap Formatter: Better f-string dummy (#5730) 2023-07-13 09:27:25 +00:00
black_compatibility@simple_cases__empty_lines.py.snap Format assert statement (#5168) 2023-07-14 09:01:33 +02:00
black_compatibility@simple_cases__expression.py.snap Format DictComp expression (#5771) 2023-07-15 17:35:23 +01:00
black_compatibility@simple_cases__fmtonoff.py.snap Format assert statement (#5168) 2023-07-14 09:01:33 +02:00
black_compatibility@simple_cases__fmtonoff2.py.snap Format assert statement (#5168) 2023-07-14 09:01:33 +02:00
black_compatibility@simple_cases__fmtonoff3.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@simple_cases__fmtonoff4.py.snap Don't add a magic trailing comma for a single entry (#5463) 2023-07-03 21:48:44 +02:00
black_compatibility@simple_cases__fmtonoff5.py.snap Pass parent to NeedsParentheses (#5708) 2023-07-13 08:57:29 +02:00
black_compatibility@simple_cases__fmtskip.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@simple_cases__fmtskip2.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@simple_cases__fmtskip3.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@simple_cases__fmtskip5.py.snap Improve comprehension line break beheavior 2023-07-11 16:51:24 +02:00
black_compatibility@simple_cases__fmtskip7.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@simple_cases__fmtskip8.py.snap Format AsyncFor (#5808) 2023-07-17 10:38:59 +02:00
black_compatibility@simple_cases__fstring.py.snap Formatter: Better f-string dummy (#5730) 2023-07-13 09:27:25 +00:00
black_compatibility@simple_cases__function.py.snap Format assert statement (#5168) 2023-07-14 09:01:33 +02:00
black_compatibility@simple_cases__function2.py.snap Format import statements (#5493) 2023-07-04 07:07:20 +00:00
black_compatibility@simple_cases__import_spacing.py.snap Format import statements (#5493) 2023-07-04 07:07:20 +00:00
black_compatibility@simple_cases__power_op_spacing.py.snap Format DictComp expression (#5771) 2023-07-15 17:35:23 +01:00
black_compatibility@simple_cases__remove_await_parens.py.snap Parenthesize with statements (#5758) 2023-07-15 16:03:09 +01:00
black_compatibility@simple_cases__remove_except_parens.py.snap Format raise statement (#5595) 2023-07-10 21:23:49 +02:00
black_compatibility@simple_cases__remove_for_brackets.py.snap Change lambda dummy to NOT_YET_IMPLEMENTED_lambda (#5687) 2023-07-11 13:16:18 +00:00
black_compatibility@simple_cases__remove_newline_after_code_block_open.py.snap Formatter: Better f-string dummy (#5730) 2023-07-13 09:27:25 +00:00
black_compatibility@simple_cases__remove_parens.py.snap Format DictComp expression (#5771) 2023-07-15 17:35:23 +01:00
black_compatibility@simple_cases__return_annotation_brackets.py.snap Format target: annotation = value? expressions (#5661) 2023-07-11 16:40:28 +02:00
black_compatibility@simple_cases__slices.py.snap Change lambda dummy to NOT_YET_IMPLEMENTED_lambda (#5687) 2023-07-11 13:16:18 +00:00
black_compatibility@simple_cases__string_prefixes.py.snap Formatter: Better f-string dummy (#5730) 2023-07-13 09:27:25 +00:00
black_compatibility@simple_cases__torture.py.snap Format assert statement (#5168) 2023-07-14 09:01:33 +02:00
black_compatibility@simple_cases__trailing_comma_optional_parens3.py.snap Prefer expanding parenthesized expressions before operands 2023-07-11 14:07:39 +02:00
black_compatibility@simple_cases__trailing_commas_in_leading_parts.py.snap Format assert statement (#5168) 2023-07-14 09:01:33 +02:00
black_compatibility@simple_cases__tupleassign.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@simple_cases__whitespace.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
format@carriage_return__string.py.snap Normalize '\r' in string literals to '\n' 2023-06-30 10:13:23 +02:00
format@expression__annotated_assign.py.snap Format target: annotation = value? expressions (#5661) 2023-07-11 16:40:28 +02:00
format@expression__attribute.py.snap Format import statements (#5493) 2023-07-04 07:07:20 +00:00
format@expression__binary.py.snap Format SetComp (#5774) 2023-07-15 15:50:47 +01:00
format@expression__boolean_operation.py.snap Format call expressions (without call chaining) (#5341) 2023-06-27 09:29:40 +00:00
format@expression__call.py.snap Improve comprehension line break beheavior 2023-07-11 16:51:24 +02:00
format@expression__compare.py.snap Cover Black's is_aritmetic_like formatting (#5738) 2023-07-14 17:54:58 +02:00
format@expression__dict.py.snap Fix typos found by codespell (#5607) 2023-07-08 12:33:18 +02:00
format@expression__dict_comp.py.snap Format DictComp expression (#5771) 2023-07-15 17:35:23 +01:00
format@expression__if.py.snap Fix find_only_token_in_range with expression parentheses (#5645) 2023-07-10 15:55:19 +02:00
format@expression__list.py.snap Don't add a magic trailing comma for a single entry (#5463) 2023-07-03 21:48:44 +02:00
format@expression__list_comp.py.snap Improve comprehension line break beheavior 2023-07-11 16:51:24 +02:00
format@expression__named_expr.py.snap Format named expressions (walrus operator) (#5642) 2023-07-10 12:32:15 +00:00
format@expression__set_comp.py.snap Format SetComp (#5774) 2023-07-15 15:50:47 +01:00
format@expression__slice.py.snap Add Regression test for #5605, where formatting x[:,] failed. (#5759) 2023-07-14 11:55:05 +02:00
format@expression__starred.py.snap Format ExpressionStarred nodes (#5654) 2023-07-11 06:08:08 +00:00
format@expression__string.py.snap Cover Black's is_aritmetic_like formatting (#5738) 2023-07-14 17:54:58 +02:00
format@expression__tuple.py.snap Format raise statement (#5595) 2023-07-10 21:23:49 +02:00
format@expression__unary.py.snap Prefer expanding parenthesized expressions before operands 2023-07-11 14:07:39 +02:00
format@skip_magic_trailing_comma.py.snap Fix typos found by codespell (#5607) 2023-07-08 12:33:18 +02:00
format@statement__ann_assign.py.snap Fix StmtAnnAssign formatting by mirroring StmtAssign (#5732) 2023-07-13 10:51:25 +00:00
format@statement__assert.py.snap Format assert statement (#5168) 2023-07-14 09:01:33 +02:00
format@statement__assign.py.snap Properly group assignment targets (#5728) 2023-07-13 16:00:49 +02:00
format@statement__aug_assign.py.snap Fix StmtAnnAssign formatting by mirroring StmtAssign (#5732) 2023-07-13 10:51:25 +00:00
format@statement__break.py.snap Add tests for skip magic trailing comma 2023-06-26 14:15:55 +02:00
format@statement__class_definition.py.snap Fix invalid syntax for binary expression in unary op (#5370) 2023-06-29 08:09:26 +02:00
format@statement__delete.py.snap Format delete statement (#5169) 2023-07-11 08:36:26 +02:00
format@statement__for.py.snap Pass parent to NeedsParentheses (#5708) 2023-07-13 08:57:29 +02:00
format@statement__function.py.snap Refactor StmtIf: Formatter and Linter (#5459) 2023-07-18 13:40:15 +02:00
format@statement__if.py.snap Refactor StmtIf: Formatter and Linter (#5459) 2023-07-18 13:40:15 +02:00
format@statement__import.py.snap Format import statements (#5493) 2023-07-04 07:07:20 +00:00
format@statement__import_from.py.snap Format import statements (#5493) 2023-07-04 07:07:20 +00:00
format@statement__raise.py.snap Pass parent to NeedsParentheses (#5708) 2023-07-13 08:57:29 +02:00
format@statement__try.py.snap Fix formatter StmtTry test (#5568) 2023-07-06 18:23:53 +00:00
format@statement__while.py.snap Pass parent to NeedsParentheses (#5708) 2023-07-13 08:57:29 +02:00
format@statement__with.py.snap Parenthesize with statements (#5758) 2023-07-15 16:03:09 +01:00
format@trivia.py.snap Add tests for skip magic trailing comma 2023-06-26 14:15:55 +02:00