mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 12:29:28 +00:00
Range formatting: Fix invalid syntax after parenthesizing expression (#9751)
This commit is contained in:
parent
50bfbcf568
commit
4f7fb566f0
24 changed files with 351 additions and 212 deletions
|
@ -20,7 +20,7 @@ impl FormatNodeRule<ExprNumberLiteral> for FormatExprNumberLiteral {
|
|||
|
||||
match normalized {
|
||||
Cow::Borrowed(_) => source_text_slice(range).fmt(f),
|
||||
Cow::Owned(normalized) => text(&normalized, Some(range.start())).fmt(f),
|
||||
Cow::Owned(normalized) => text(&normalized).fmt(f),
|
||||
}
|
||||
}
|
||||
Number::Float(_) => {
|
||||
|
@ -30,7 +30,7 @@ impl FormatNodeRule<ExprNumberLiteral> for FormatExprNumberLiteral {
|
|||
|
||||
match normalized {
|
||||
Cow::Borrowed(_) => source_text_slice(range).fmt(f),
|
||||
Cow::Owned(normalized) => text(&normalized, Some(range.start())).fmt(f),
|
||||
Cow::Owned(normalized) => text(&normalized).fmt(f),
|
||||
}
|
||||
}
|
||||
Number::Complex { .. } => {
|
||||
|
@ -43,7 +43,7 @@ impl FormatNodeRule<ExprNumberLiteral> for FormatExprNumberLiteral {
|
|||
source_text_slice(range.sub_end(TextSize::from(1))).fmt(f)?;
|
||||
}
|
||||
Cow::Owned(normalized) => {
|
||||
text(&normalized, Some(range.start())).fmt(f)?;
|
||||
text(&normalized).fmt(f)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -304,30 +304,25 @@ fn format_with_parentheses_comments(
|
|||
// Custom FormatNodeRule::fmt variant that only formats the inner comments
|
||||
let format_node_rule_fmt = format_with(|f| {
|
||||
// No need to handle suppression comments, those are statement only
|
||||
leading_comments(leading_inner).fmt(f)?;
|
||||
|
||||
let is_source_map_enabled = f.options().source_map_generation().is_enabled();
|
||||
|
||||
if is_source_map_enabled {
|
||||
source_position(expression.start()).fmt(f)?;
|
||||
}
|
||||
|
||||
fmt_fields.fmt(f)?;
|
||||
|
||||
if is_source_map_enabled {
|
||||
source_position(expression.end()).fmt(f)?;
|
||||
}
|
||||
|
||||
trailing_comments(trailing_inner).fmt(f)
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
leading_comments(leading_inner),
|
||||
fmt_fields,
|
||||
trailing_comments(trailing_inner)
|
||||
]
|
||||
)
|
||||
});
|
||||
|
||||
// The actual parenthesized formatting
|
||||
parenthesized("(", &format_node_rule_fmt, ")")
|
||||
.with_dangling_comments(parentheses_comment)
|
||||
.fmt(f)?;
|
||||
trailing_comments(trailing_outer).fmt(f)?;
|
||||
|
||||
Ok(())
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
parenthesized("(", &format_node_rule_fmt, ")")
|
||||
.with_dangling_comments(parentheses_comment),
|
||||
trailing_comments(trailing_outer)
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
/// Wraps an expression in an optional parentheses except if its [`NeedsParentheses::needs_parentheses`] implementation
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue