mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:09:22 +00:00

## Summary If a lambda doesn't contain any parameters, or any parameter _tokens_ (like `*`), we can use `None` for the parameters. This feels like a better representation to me, since, e.g., what should the `TextRange` be for a non-existent set of parameters? It also allows us to remove several sites where we check if the `Parameters` is empty by seeing if it contains any arguments, so semantically, we're already trying to detect and model around this elsewhere. Changing this also fixes a number of issues with dangling comments in parameter-less lambdas, since those comments are now automatically marked as dangling on the lambda. (As-is, we were also doing something not-great whereby the lambda was responsible for formatting dangling comments on the parameters, which has been removed.) Closes https://github.com/astral-sh/ruff/issues/6646. Closes https://github.com/astral-sh/ruff/issues/6647. ## Test Plan `cargo test`
70 lines
1.9 KiB
Rust
70 lines
1.9 KiB
Rust
use ruff_formatter::prelude::{space, text};
|
|
use ruff_formatter::{write, Buffer, FormatResult};
|
|
use ruff_python_ast::node::AnyNodeRef;
|
|
use ruff_python_ast::ExprLambda;
|
|
|
|
use crate::comments::{dangling_comments, SourceComment};
|
|
use crate::context::PyFormatContext;
|
|
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
|
use crate::other::parameters::ParametersParentheses;
|
|
use crate::AsFormat;
|
|
use crate::{FormatNodeRule, PyFormatter};
|
|
|
|
#[derive(Default)]
|
|
pub struct FormatExprLambda;
|
|
|
|
impl FormatNodeRule<ExprLambda> for FormatExprLambda {
|
|
fn fmt_fields(&self, item: &ExprLambda, f: &mut PyFormatter) -> FormatResult<()> {
|
|
let ExprLambda {
|
|
range: _,
|
|
parameters,
|
|
body,
|
|
} = item;
|
|
|
|
let comments = f.context().comments().clone();
|
|
let dangling = comments.dangling(item);
|
|
|
|
write!(f, [text("lambda")])?;
|
|
|
|
if let Some(parameters) = parameters {
|
|
write!(
|
|
f,
|
|
[
|
|
space(),
|
|
parameters
|
|
.format()
|
|
.with_options(ParametersParentheses::Never),
|
|
]
|
|
)?;
|
|
}
|
|
|
|
write!(f, [text(":")])?;
|
|
|
|
if dangling.is_empty() {
|
|
write!(f, [space()])?;
|
|
} else {
|
|
write!(f, [dangling_comments(dangling)])?;
|
|
}
|
|
|
|
write!(f, [body.format()])
|
|
}
|
|
|
|
fn fmt_dangling_comments(
|
|
&self,
|
|
_dangling_comments: &[SourceComment],
|
|
_f: &mut PyFormatter,
|
|
) -> FormatResult<()> {
|
|
// Override. Dangling comments are handled in `fmt_fields`.
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl NeedsParentheses for ExprLambda {
|
|
fn needs_parentheses(
|
|
&self,
|
|
_parent: AnyNodeRef,
|
|
_context: &PyFormatContext,
|
|
) -> OptionalParentheses {
|
|
OptionalParentheses::Multiline
|
|
}
|
|
}
|