diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/if.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/if.py index b3fc460e442f06..6aa991bb48087d 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/if.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/if.py @@ -108,3 +108,54 @@ def something(): if flat else ValuesListIterable) ) + + +# Expanding leading and trailing comments. +before = ( + # comment + 0 + if self.thing is None + else before - after +) + +before = ( + 0 + if self.thing is None + else before - after + # comment +) + +before = ( # comment + 0 + if self.thing is None + else before - after +) + +before = ( + # comment + (0 + if self.thing is None + else before - after + )) + +before = [ + # comment + 0 + if self.thing is None + else before - after +] + +before = ( + # comment + 0 + if self.thing is None + else before - after +) + +before = [ + # comment + 0 + if self.thing is None + else before - after, + 2 +] diff --git a/crates/ruff_python_formatter/src/expression/expr_if_exp.rs b/crates/ruff_python_formatter/src/expression/expr_if_exp.rs index 1b22fb568c1993..b7a38f0a40d95b 100644 --- a/crates/ruff_python_formatter/src/expression/expr_if_exp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_if_exp.rs @@ -53,9 +53,22 @@ impl FormatNodeRule for FormatExprIfExp { let comments = f.context().comments().clone(); let inner = format_with(|f: &mut PyFormatter| { - // We place `if test` and `else orelse` on a single line, so the `test` and `orelse` leading - // comments go on the line before the `if` or `else` instead of directly ahead `test` or - // `orelse` + // If the expression has any leading or trailing comments, always expand it, as in: + // ``` + // ( + // # comment + // 0 + // if self.thing is None + // else before - after + // ) + // ``` + if comments.has_leading(item) || comments.has_trailing(item) { + expand_parent().fmt(f)?; + } + + // We place `if test` and `else orelse` on a single line, so the `test` and `orelse` + // leading comments go on the line before the `if` or `else` instead of directly ahead + // `test` or `orelse`. write!( f, [ diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__if.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__if.py.snap index 7aa5a080c82982..7d7f8f56b85ae4 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@expression__if.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__if.py.snap @@ -114,6 +114,57 @@ def something(): if flat else ValuesListIterable) ) + + +# Expanding leading and trailing comments. +before = ( + # comment + 0 + if self.thing is None + else before - after +) + +before = ( + 0 + if self.thing is None + else before - after + # comment +) + +before = ( # comment + 0 + if self.thing is None + else before - after +) + +before = ( + # comment + (0 + if self.thing is None + else before - after + )) + +before = [ + # comment + 0 + if self.thing is None + else before - after +] + +before = ( + # comment + 0 + if self.thing is None + else before - after +) + +before = [ + # comment + 0 + if self.thing is None + else before - after, + 2 +] ``` ## Output @@ -240,6 +291,57 @@ def something(): else ValuesListIterable ) ) + + +# Expanding leading and trailing comments. +before = ( + # comment + 0 + if self.thing is None + else before - after +) + +before = ( + 0 + if self.thing is None + else before - after + # comment +) + +before = ( # comment + 0 + if self.thing is None + else before - after +) + +before = ( + # comment + 0 + if self.thing is None + else before - after +) + +before = [ + # comment + 0 + if self.thing is None + else before - after +] + +before = ( + # comment + 0 + if self.thing is None + else before - after +) + +before = [ + # comment + 0 + if self.thing is None + else before - after, + 2, +] ```