Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/oxc_formatter/src/utils/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ impl<'a> FormatConditionalLike<'a, '_> {
write!(f, [soft_line_break_or_space(), "?", space()])?;

let format_consequent = format_with(|f| {
let format_consequent_with_trailing_comments = format_once(|f| {
let format_consequent_with_trailing_comments = format_with(|f| {
let (start, end) = match self.conditional {
ConditionalLike::ConditionalExpression(conditional) => {
write!(f, FormatNodeWithoutTrailingComments(conditional.consequent()))?;
Expand Down Expand Up @@ -514,7 +514,7 @@ impl<'a> Format<'a> for FormatConditionalLike<'a, '_> {
let format_inner = format_with(|f| {
self.format_test(f, layout)?;

let format_tail_with_indent = format_once(|f| {
let format_tail_with_indent = format_with(|f| {
if is_jsx_chain
&& let ConditionalLike::ConditionalExpression(conditional) = self.conditional
{
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_formatter/src/utils/typecast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ pub fn format_type_cast_comment_node<'a, T>(

// https://github.com/prettier/prettier/blob/7584432401a47a26943dd7a9ca9a8e032ead7285/src/language-js/print/estree.js#L117-L120
if is_object_or_array_expression && !f.comments().has_comment_before(span.start) {
write!(f, group(&format_args!("(", &format_once(|f| node.fmt(f)), ")")))?;
write!(f, group(&format_args!("(", &format_with(|f| node.fmt(f)), ")")))?;
} else {
write!(
f,
group(&format_args!("(", soft_block_indent(&format_once(|f| node.fmt(f))), ")"))
group(&format_args!("(", soft_block_indent(&format_with(|f| node.fmt(f))), ")"))
)?;
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_formatter/src/write/array_element_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl<'a> Format<'a> for ArrayElementList<'a, '_> {
.with_group_id(self.group_id)
{
filler.entry(
&format_once(|f| {
&format_with(|f| {
if f.source_text().get_lines_before(element.span(), f.comments()) > 1 {
write!(f, empty_line())
} else if f
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_formatter/src/write/array_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<'a> Format<'a> for FormatArrayPattern<'a, '_> {
} else {
write!(
f,
group(&soft_block_indent(&format_once(|f| {
group(&soft_block_indent(&format_with(|f| {
let has_element = !self.elements.is_empty();
if has_element {
write_array_node(
Expand Down
13 changes: 6 additions & 7 deletions crates/oxc_formatter/src/write/arrow_function_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ impl<'a> Format<'a> for ArrowChain<'a, '_> {
let is_first = is_first_in_chain;

let formatted_signature = format_with(|f| {
let format_leading_comments = format_once(|f| {
let format_leading_comments = format_with(|f| {
if should_format_comments {
// A grouped layout implies that the arrow chain is trying to be rendered
// in a condensed, single-line format (at least the signatures, not
Expand Down Expand Up @@ -636,7 +636,7 @@ impl<'a> Format<'a> for ArrowChain<'a, '_> {

let group_id = f.group_id("arrow-chain");

let format_inner = format_once(|f| {
let format_inner = format_with(|f| {
if has_initial_indent {
write!(
f,
Expand Down Expand Up @@ -716,13 +716,13 @@ fn format_signature<'a, 'b>(
cache_mode: FunctionCacheMode,
) -> impl Format<'a> + 'b {
format_with(move |f| {
let content = format_once(|f| {
let content = format_with(|f| {
group(&format_args!(
maybe_space(!is_first_in_chain),
arrow.r#async().then_some("async "),
arrow.type_parameters(),
arrow.params(),
&format_once(|f| {
&format_with(|f| {
if let Some(return_type) = &arrow.return_type() {
group(&FormatNodeWithoutTrailingComments(return_type)).fmt(f)
} else {
Expand Down Expand Up @@ -760,8 +760,7 @@ fn format_signature<'a, 'b>(
// Print comments before the fat arrow (`=>`)
let comments_before_fat_arrow =
f.context().comments().comments_before_character(arrow.params.span().end, b'=');
let content =
format_once(|f| FormatTrailingComments::Comments(comments_before_fat_arrow).fmt(f));
let content = FormatTrailingComments::Comments(comments_before_fat_arrow);
write!(f, [FormatContentWithCacheMode::new(arrow.span, content, cache_mode)])
})
}
Expand All @@ -780,7 +779,7 @@ pub struct FormatMaybeCachedFunctionBody<'a, 'b> {

impl<'a> Format<'a> for FormatMaybeCachedFunctionBody<'a, '_> {
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
let content = format_once(|f| {
let content = format_with(|f| {
if self.expression
&& let AstNodes::ExpressionStatement(s) =
&self.body.statements().first().unwrap().as_ast_nodes()
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_formatter/src/write/binary_like_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl<'a> Format<'a> for BinaryLikeExpression<'a, '_> {
if is_inside_condition {
return write!(
f,
[&format_once(|f| {
[&format_with(|f| {
format_flattened_logical_expression(*self, is_inside_condition, f)
})]
);
Expand Down Expand Up @@ -231,7 +231,7 @@ impl<'a> Format<'a> for BinaryLikeExpression<'a, '_> {
if should_not_indent {
return write!(
f,
[group(&format_once(|f| {
[group(&format_with(|f| {
// is_inside_condition is always false here (we returned early if true)
format_flattened_logical_expression(*self, false, f)
}))]
Expand Down Expand Up @@ -267,7 +267,7 @@ impl<'a> Format<'a> for BinaryLikeExpression<'a, '_> {
f,
[group(&format_args!(
first,
indent(&format_once(|f| { f.join().entries(tail_parts.iter()).finish() }))
indent(&format_with(|f| { f.join().entries(tail_parts.iter()).finish() }))
))
.with_group_id(Some(group_id))]
)
Expand Down Expand Up @@ -395,7 +395,7 @@ impl<'a> Format<'a> for BinaryLeftOrRightSide<'a, '_> {
space(),
operator.as_str(),
soft_line_break_or_space(),
format_once(|f| {
format_with(|f| {
// If the left side of the right logical expression is still a logical expression with
// the same operator, we need to recursively format it inline.
// This way, we can ensure that all parts are in the same group.
Expand Down
14 changes: 7 additions & 7 deletions crates/oxc_formatter/src/write/call_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<'a> Format<'a> for AstNode<'a, ArenaVec<'a, Argument<'a>>> {
f,
[
l_paren_token,
soft_block_indent(&format_once(|f| {
soft_block_indent(&format_with(|f| {
f.join_with(soft_line_break_or_space())
.entries_with_trailing_separator(self.iter(), ",", trailing_operator)
.finish()
Expand Down Expand Up @@ -232,7 +232,7 @@ fn format_all_args_broken_out<'a, 'b>(
buffer,
[group(&format_args!(
"(",
soft_block_indent(&format_once(move |f| {
soft_block_indent(&format_with(move |f| {
for (index, argument) in node.iter().enumerate() {
if index > 0 {
match f.source_text().get_lines_before(argument.span(), f.comments()) {
Expand Down Expand Up @@ -670,7 +670,7 @@ fn write_grouped_arguments<'a>(
// which would lead to a wrong line count.
let lines_before = f.source_text().get_lines_before(argument.span(), f.comments());

let interned = f.intern(&format_once(|f| {
let interned = f.intern(&format_with(|f| {
format_argument.fmt(f)?;
write!(f, (last_index != index).then_some(","))
}));
Expand Down Expand Up @@ -717,7 +717,7 @@ fn write_grouped_arguments<'a>(
match group_layout {
GroupedCallArgumentLayout::GroupedFirstArgument => {
let argument = node.first().unwrap();
let interned = f.intern(&format_once(|f| {
let interned = f.intern(&format_with(|f| {
FormatGroupedFirstArgument { argument }.fmt(f)?;
write!(f, (last_index != 0).then_some(","))
}));
Expand Down Expand Up @@ -765,15 +765,15 @@ fn write_grouped_arguments<'a>(
buffer,
[
"(",
format_once(|f| {
format_with(|f| {
let mut joiner = f.join_with(soft_line_break_or_space());

for (i, (element, _)) in grouped.iter().enumerate() {
if (group_layout.is_grouped_first() && i == 0)
|| (group_layout.is_grouped_last() && i == last_index)
{
joiner.entry(
&group(&format_once(|f| {
&group(&format_with(|f| {
if let Some(arg_element) = element.clone()? {
f.write_element(arg_element)
} else {
Expand All @@ -783,7 +783,7 @@ fn write_grouped_arguments<'a>(
.should_expand(true),
);
} else {
joiner.entry(&format_once(|f| {
joiner.entry(&format_with(|f| {
if let Some(arg_element) = element.clone()? {
f.write_element(arg_element)
} else {
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_formatter/src/write/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ impl<'a> Format<'a> for FormatClass<'a, '_> {
});

let format_extends =
format_once(|f| write!(f, [space(), "extends", space(), &format_super]));
format_with(|f| write!(f, [space(), "extends", space(), &format_super]));

if group_mode {
write!(f, [soft_line_break_or_space(), group(&format_extends)])?;
Expand All @@ -430,7 +430,7 @@ impl<'a> Format<'a> for FormatClass<'a, '_> {
]
)?;
} else {
let format_inner = format_once(|f| {
let format_inner = format_with(|f| {
write!(
f,
[
Expand Down Expand Up @@ -602,7 +602,7 @@ pub fn format_grouped_parameters_with_return_type_for_method<'a>(
) -> FormatResult<()> {
write!(f, type_parameters)?;

group(&format_once(|f| {
group(&format_with(|f| {
let format_parameters = params.memoized();
let format_return_type = return_type.map(FormatNodeWithoutTrailingComments).memoized();

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_formatter/src/write/decorators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<'a> Format<'a> for AstNode<'a, Vec<'a, Decorator<'a>>> {
return write!(
f,
[group(&format_args!(
format_once(|f| {
format_with(|f| {
f.join_nodes_with_soft_line().entries(self.iter()).finish()
}),
soft_line_break_or_space()
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_formatter/src/write/export_declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn format_export_keyword_with_class_decorators<'a>(
) -> FormatResult<()> {
// `@decorator export class Cls {}`
// ^ print leading comments here
let format_leading_comments = format_once(|f| {
let format_leading_comments = format_with(|f| {
let comments = f.context().comments().comments_before(span.start);
FormatLeadingComments::Comments(comments).fmt(f)
});
Expand Down Expand Up @@ -181,7 +181,7 @@ impl<'a> Format<'a> for AstNode<'a, Vec<'a, ExportSpecifier<'a>>> {
FormatSeparatedIter::new(self.iter(), ",")
.with_trailing_separator(trailing_separator)
.map(|specifier| {
format_once(move |f| {
format_with(move |f| {
// Should add empty line before the specifier if there are comments before it.
let specifier_span = specifier.span();
if f.context().comments().has_comment_before(specifier_span.start)
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_formatter/src/write/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<'a> Deref for FormatFunction<'a, '_> {

impl<'a> Format<'a> for FormatFunction<'a, '_> {
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
let head = format_once(|f| {
let head = format_with(|f| {
write!(
f,
[
Expand All @@ -65,7 +65,7 @@ impl<'a> Format<'a> for FormatFunction<'a, '_> {
)
.memoized();

let format_parameters = format_once(|f: &mut Formatter<'_, 'a>| {
let format_parameters = format_with(|f: &mut Formatter<'_, 'a>| {
if self.options.call_argument_layout.is_some() {
let mut buffer = RemoveSoftLinesBuffer::new(f);

Expand All @@ -87,7 +87,7 @@ impl<'a> Format<'a> for FormatFunction<'a, '_> {
let format_return_type = self
.return_type()
.map(|return_type| {
let content = format_once(move |f| {
let content = format_with(move |f| {
let needs_space =
f.context().comments().has_comment_before(return_type.span.start);
write!(f, [maybe_space(needs_space), return_type])
Expand All @@ -98,7 +98,7 @@ impl<'a> Format<'a> for FormatFunction<'a, '_> {

write!(
f,
[group(&format_once(|f| {
[group(&format_with(|f| {
let params = &self.params;
// Inspect early, in case the `return_type` is formatted before `parameters`
// in `should_group_function_parameters`.
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_formatter/src/write/function_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSConstructorType<'a>> {
r#abstract.then_some("abstract "),
"new",
space(),
&format_once(|f| {
&format_with(|f| {
format_grouped_parameters_with_return_type(
self.type_parameters(),
None,
Expand Down Expand Up @@ -64,7 +64,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSCallSignatureDeclaration<'a>> {

impl<'a> FormatWrite<'a> for AstNode<'a, TSMethodSignature<'a>> {
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
let format_inner = format_once(|f| {
let format_inner = format_with(|f| {
match self.kind() {
TSMethodSignatureKind::Method => {}
TSMethodSignatureKind::Get => {
Expand Down Expand Up @@ -120,7 +120,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSConstructSignatureDeclaration<'a>> {
group(&format_args!(
"new",
space(),
&format_once(|f| {
&format_with(|f| {
format_grouped_parameters_with_return_type(
self.type_parameters(),
None,
Expand All @@ -144,7 +144,7 @@ pub fn format_grouped_parameters_with_return_type<'a>(
is_function_or_constructor_type: bool,
f: &mut Formatter<'_, 'a>,
) -> FormatResult<()> {
group(&format_once(|f| {
group(&format_with(|f| {
let format_type_parameters = type_parameters.memoized();
let format_parameters = params.memoized();
let format_return_type = return_type.map(FormatNodeWithoutTrailingComments).memoized();
Expand Down
10 changes: 5 additions & 5 deletions crates/oxc_formatter/src/write/import_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn format_import_and_export_source_with_clause<'a>(

impl<'a> FormatWrite<'a> for AstNode<'a, ImportDeclaration<'a>> {
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
let decl = &format_once(|f| {
let decl = &format_with(|f| {
write!(f, ["import", space(), self.import_kind])?;

if let Some(specifiers) = self.specifiers() {
Expand Down Expand Up @@ -113,7 +113,7 @@ impl<'a> Format<'a> for AstNode<'a, Vec<'a, ImportDeclarationSpecifier<'a>>> {
let iter = FormatSeparatedIter::new(specifiers_iter, ",")
.with_trailing_separator(trailing_separator)
.map(|specifier| {
format_once(move |f| {
format_with(move |f| {
// Should add empty line before the specifier if there are comments before it.
let specifier_span = specifier.span();
if f.context()
Expand Down Expand Up @@ -196,7 +196,7 @@ impl<'a> Format<'a> for AstNode<'a, Vec<'a, ImportAttribute<'a>>> {
return write!(f, "{}");
}

let format_inner = format_once(|f| {
let format_inner = format_with(|f| {
let should_insert_space_around_brackets = f.options().bracket_spacing.value();

write!(f, "{")?;
Expand All @@ -208,7 +208,7 @@ impl<'a> Format<'a> for AstNode<'a, Vec<'a, ImportAttribute<'a>>> {
write!(
f,
[soft_block_indent_with_maybe_space(
&format_once(|f| {
&format_with(|f| {
let trailing_separator =
FormatTrailingCommas::ES5.trailing_separator(f.options());

Expand All @@ -226,7 +226,7 @@ impl<'a> Format<'a> for AstNode<'a, Vec<'a, ImportAttribute<'a>>> {
} else {
write!(
f,
[format_once(|f| {
[format_with(|f| {
let maybe_space = maybe_space(f.options().bracket_spacing.value());
write!(f, [maybe_space])?;

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_formatter/src/write/intersection_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{

impl<'a> FormatWrite<'a> for AstNode<'a, TSIntersectionType<'a>> {
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
let content = format_once(|f| format_intersection_types(self.types(), f));
let content = format_with(|f| format_intersection_types(self.types(), f));
write!(f, [group(&content)])
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_formatter/src/write/jsx/child_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ impl FormatJsxChildList {
child_breaks = line_mode.is_some_and(LineMode::is_hard);

let child_should_be_suppressed = is_next_child_suppressed;
let format_child = format_once(|f| {
let format_child = format_with(|f| {
if child_should_be_suppressed {
FormatSuppressedNode(non_text.span()).fmt(f)
} else {
Expand Down Expand Up @@ -616,7 +616,7 @@ pub struct FormatMultilineChildren<'a> {

impl<'a> Format<'a> for FormatMultilineChildren<'a> {
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
let format_inner = format_once(|f| {
let format_inner = format_with(|f| {
if let Some(elements) =
f.intern_vec(self.elements.borrow_mut().take_in(f.context().allocator()))
{
Expand Down
Loading
Loading