mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 20:42:10 +00:00

Fix an instability where await was followed by a breaking fluent style expression: ```python test_data = await ( Stream.from_async(async_data) .flat_map_async() .map() .filter_async(is_valid_data) .to_list() ) ``` Note that this technically a minor style change (see ecosystem check)
48 lines
1.3 KiB
Rust
48 lines
1.3 KiB
Rust
use ruff_formatter::write;
|
|
use ruff_python_ast::AnyNodeRef;
|
|
use ruff_python_ast::ExprAwait;
|
|
|
|
use crate::expression::maybe_parenthesize_expression;
|
|
use crate::expression::parentheses::{
|
|
is_expression_parenthesized, NeedsParentheses, OptionalParentheses, Parenthesize,
|
|
};
|
|
use crate::prelude::*;
|
|
|
|
#[derive(Default)]
|
|
pub struct FormatExprAwait;
|
|
|
|
impl FormatNodeRule<ExprAwait> for FormatExprAwait {
|
|
fn fmt_fields(&self, item: &ExprAwait, f: &mut PyFormatter) -> FormatResult<()> {
|
|
let ExprAwait { range: _, value } = item;
|
|
|
|
write!(
|
|
f,
|
|
[
|
|
token("await"),
|
|
space(),
|
|
maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks)
|
|
]
|
|
)
|
|
}
|
|
}
|
|
|
|
impl NeedsParentheses for ExprAwait {
|
|
fn needs_parentheses(
|
|
&self,
|
|
parent: AnyNodeRef,
|
|
context: &PyFormatContext,
|
|
) -> OptionalParentheses {
|
|
if parent.is_expr_await() {
|
|
OptionalParentheses::Always
|
|
} else if is_expression_parenthesized(
|
|
self.value.as_ref().into(),
|
|
context.comments().ranges(),
|
|
context.source(),
|
|
) {
|
|
// Prefer splitting the value if it is parenthesized.
|
|
OptionalParentheses::Never
|
|
} else {
|
|
self.value.needs_parentheses(self.into(), context)
|
|
}
|
|
}
|
|
}
|