Range formatting: Fix invalid syntax after parenthesizing expression (#9751)

This commit is contained in:
Micha Reiser 2024-02-02 17:56:25 +01:00 committed by GitHub
parent 50bfbcf568
commit 4f7fb566f0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 351 additions and 212 deletions

View file

@ -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)?;
}
}

View file

@ -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