mirror of https://github.com/astral-sh/ruff
avoid breaking when the first parameter has leading comments
This commit is contained in:
parent
90f43bde84
commit
b0a82983af
|
|
@ -241,6 +241,32 @@ impl FormatNodeRule<Parameters> for FormatParameters {
|
|||
let num_parameters = item.len();
|
||||
|
||||
if self.parentheses == ParametersParentheses::Never {
|
||||
// In a lambda, format any leading comments on the first parameter outside of the
|
||||
// parameters group so that the parameters don't break. For example, format this input:
|
||||
//
|
||||
// ```py
|
||||
// (
|
||||
// lambda
|
||||
// * # this becomes a leading comment on *x
|
||||
// x, **y:
|
||||
// x
|
||||
// )
|
||||
// ```
|
||||
//
|
||||
// as:
|
||||
//
|
||||
// ```py
|
||||
// (
|
||||
// lambda
|
||||
// # this becomes a leading comment on *x
|
||||
// *x, **y: x
|
||||
// )
|
||||
// ```
|
||||
if let Some(first) = item.iter().next()
|
||||
&& comments.has_leading(first.as_parameter())
|
||||
{
|
||||
leading_node_comments(first.as_parameter()).fmt(f)?;
|
||||
}
|
||||
write!(f, [group(&format_inner), dangling_comments(dangling)])
|
||||
} else if num_parameters == 0 {
|
||||
let mut f = WithNodeLevel::new(NodeLevel::ParenthesizedExpression, f);
|
||||
|
|
|
|||
|
|
@ -539,15 +539,13 @@ def a():
|
|||
(
|
||||
lambda
|
||||
# comment
|
||||
*x,
|
||||
**y: x
|
||||
*x, **y: x
|
||||
)
|
||||
|
||||
(
|
||||
lambda
|
||||
# comment 2
|
||||
*x,
|
||||
**y: x
|
||||
*x, **y: x
|
||||
)
|
||||
|
||||
(
|
||||
|
|
|
|||
Loading…
Reference in New Issue