Skip to content

Commit a4b70c3

Browse files
committed
refactor(formatter): adjust implementations of Function and ArrowFunctionExpression (#16035)
Pure refactor, I am going to remove `Format::fmt_with_options`
1 parent 14b0a6a commit a4b70c3

File tree

4 files changed

+97
-64
lines changed

4 files changed

+97
-64
lines changed

crates/oxc_formatter/src/write/arrow_function_expression.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,23 @@ use crate::{
1717
write::function::FormatContentWithCacheMode,
1818
};
1919

20-
use super::parameters::has_only_simple_parameters;
20+
use super::{FormatWrite, parameters::has_only_simple_parameters};
21+
22+
impl<'a> FormatWrite<'a, FormatJsArrowFunctionExpressionOptions>
23+
for AstNode<'a, ArrowFunctionExpression<'a>>
24+
{
25+
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
26+
FormatJsArrowFunctionExpression::new(self).fmt(f)
27+
}
28+
29+
fn write_with_options(
30+
&self,
31+
options: FormatJsArrowFunctionExpressionOptions,
32+
f: &mut Formatter<'_, 'a>,
33+
) -> FormatResult<()> {
34+
FormatJsArrowFunctionExpression::new_with_options(self, options).fmt(f)
35+
}
36+
}
2137

2238
#[derive(Clone, Copy)]
2339
pub struct FormatJsArrowFunctionExpression<'a, 'b> {
@@ -28,7 +44,7 @@ pub struct FormatJsArrowFunctionExpression<'a, 'b> {
2844
#[derive(Default, Clone, Copy)]
2945
pub struct FormatJsArrowFunctionExpressionOptions {
3046
pub assignment_layout: Option<AssignmentLikeLayout>,
31-
pub call_arg_layout: Option<GroupedCallArgumentLayout>,
47+
pub call_argument_layout: Option<GroupedCallArgumentLayout>,
3248
// Determine whether the signature and body should be cached.
3349
pub cache_mode: FunctionCacheMode,
3450
}
@@ -73,10 +89,9 @@ impl<'a, 'b> FormatJsArrowFunctionExpression<'a, 'b> {
7389
) -> Self {
7490
Self { arrow, options }
7591
}
76-
}
7792

78-
impl<'a> Format<'a> for FormatJsArrowFunctionExpression<'a, '_> {
79-
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
93+
#[inline]
94+
pub fn format(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
8095
let layout = ArrowFunctionLayout::for_arrow(self.arrow, self.options);
8196

8297
match layout {
@@ -92,7 +107,7 @@ impl<'a> Format<'a> for FormatJsArrowFunctionExpression<'a, '_> {
92107
[
93108
format_signature(
94109
arrow,
95-
self.options.call_arg_layout.is_some(),
110+
self.options.call_argument_layout.is_some(),
96111
true,
97112
self.options.cache_mode
98113
),
@@ -176,7 +191,7 @@ impl<'a> Format<'a> for FormatJsArrowFunctionExpression<'a, '_> {
176191
let should_add_parens = arrow.expression && should_add_parens(body);
177192

178193
let is_last_call_arg = matches!(
179-
self.options.call_arg_layout,
194+
self.options.call_argument_layout,
180195
Some(GroupedCallArgumentLayout::GroupedLastArgument)
181196
);
182197

@@ -245,6 +260,12 @@ impl<'a> Format<'a> for FormatJsArrowFunctionExpression<'a, '_> {
245260
}
246261
}
247262

263+
impl<'a> Format<'a> for FormatJsArrowFunctionExpression<'a, '_> {
264+
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
265+
self.format(f)
266+
}
267+
}
268+
248269
enum ArrowFunctionLayout<'a, 'b> {
249270
/// Arrow function with a non-arrow function body
250271
Single(&'b AstNode<'a, ArrowFunctionExpression<'a>>),
@@ -286,7 +307,7 @@ impl<'a, 'b> ArrowFunctionLayout<'a, 'b> {
286307
&& let AstNodes::ArrowFunctionExpression(next) =
287308
&expr_stmt.expression().as_ast_nodes()
288309
&& matches!(
289-
options.call_arg_layout,
310+
options.call_argument_layout,
290311
None | Some(GroupedCallArgumentLayout::GroupedLastArgument)
291312
)
292313
{
@@ -416,7 +437,7 @@ impl<'a> Format<'a> for ArrowChain<'a, '_> {
416437
let ArrowChain { tail, expand_signatures, .. } = self;
417438

418439
let tail_body = tail.body();
419-
let is_grouped_call_arg_layout = self.options.call_arg_layout.is_some();
440+
let is_grouped_call_arg_layout = self.options.call_argument_layout.is_some();
420441

421442
// If this chain is the callee in a parent call expression, then we
422443
// want it to break onto a new line to clearly show that the arrow

crates/oxc_formatter/src/write/call_arguments.rs

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ use crate::{
2626
};
2727

2828
use super::{
29+
FormatJsArrowFunctionExpression,
2930
array_element_list::can_concisely_print_array_list,
3031
arrow_function_expression::{
3132
FormatJsArrowFunctionExpressionOptions, FunctionCacheMode, GroupedCallArgumentLayout,
3233
},
34+
function::FormatFunction,
3335
parameters::has_only_simple_parameters,
3436
};
3537

@@ -641,23 +643,25 @@ fn write_grouped_arguments<'a>(
641643
|| function_has_only_simple_parameters(&function.params)) =>
642644
{
643645
has_cached = true;
644-
return function.fmt_with_options(
646+
return FormatFunction::new_with_options(
647+
function,
645648
FormatFunctionOptions {
646649
cache_mode: FunctionCacheMode::Cache,
647-
..Default::default()
650+
..FormatFunctionOptions::default()
648651
},
649-
f,
650-
);
652+
)
653+
.fmt(f);
651654
}
652655
AstNodes::ArrowFunctionExpression(arrow) => {
653656
has_cached = true;
654-
return arrow.fmt_with_options(
657+
return FormatJsArrowFunctionExpression::new_with_options(
658+
arrow,
655659
FormatJsArrowFunctionExpressionOptions {
656660
cache_mode: FunctionCacheMode::Cache,
657661
..FormatJsArrowFunctionExpressionOptions::default()
658662
},
659-
f,
660-
);
663+
)
664+
.fmt(f);
661665
}
662666
_ => {}
663667
}
@@ -866,14 +870,17 @@ impl<'a> Format<'a> for FormatGroupedFirstArgument<'a, '_> {
866870
match self.argument.as_ast_nodes() {
867871
// Call the arrow function formatting but explicitly passes the call argument layout down
868872
// so that the arrow function formatting removes any soft line breaks between parameters and the return type.
869-
AstNodes::ArrowFunctionExpression(arrow) => arrow.fmt_with_options(
870-
FormatJsArrowFunctionExpressionOptions {
871-
cache_mode: FunctionCacheMode::Cache,
872-
call_arg_layout: Some(GroupedCallArgumentLayout::GroupedFirstArgument),
873-
..FormatJsArrowFunctionExpressionOptions::default()
874-
},
875-
f,
876-
),
873+
AstNodes::ArrowFunctionExpression(arrow) => {
874+
FormatJsArrowFunctionExpression::new_with_options(
875+
arrow,
876+
FormatJsArrowFunctionExpressionOptions {
877+
cache_mode: FunctionCacheMode::Cache,
878+
call_argument_layout: Some(GroupedCallArgumentLayout::GroupedFirstArgument),
879+
..FormatJsArrowFunctionExpressionOptions::default()
880+
},
881+
)
882+
.fmt(f)
883+
}
877884

878885
// For all other nodes, use the normal formatting (which already has been cached)
879886
_ => self.argument.fmt(f),
@@ -898,23 +905,26 @@ impl<'a> Format<'a> for FormatGroupedLastArgument<'a, '_> {
898905
AstNodes::Function(function)
899906
if !self.is_only || function_has_only_simple_parameters(&function.params) =>
900907
{
901-
function.fmt_with_options(
908+
FormatFunction::new_with_options(
909+
function,
902910
FormatFunctionOptions {
903911
cache_mode: FunctionCacheMode::Cache,
904912
call_argument_layout: Some(GroupedCallArgumentLayout::GroupedLastArgument),
905913
},
906-
f,
907914
)
915+
.fmt(f)
916+
}
917+
AstNodes::ArrowFunctionExpression(arrow) => {
918+
FormatJsArrowFunctionExpression::new_with_options(
919+
arrow,
920+
FormatJsArrowFunctionExpressionOptions {
921+
cache_mode: FunctionCacheMode::Cache,
922+
call_argument_layout: Some(GroupedCallArgumentLayout::GroupedLastArgument),
923+
..FormatJsArrowFunctionExpressionOptions::default()
924+
},
925+
)
926+
.fmt(f)
908927
}
909-
910-
AstNodes::ArrowFunctionExpression(arrow) => arrow.fmt_with_options(
911-
FormatJsArrowFunctionExpressionOptions {
912-
cache_mode: FunctionCacheMode::Cache,
913-
call_arg_layout: Some(GroupedCallArgumentLayout::GroupedLastArgument),
914-
..FormatJsArrowFunctionExpressionOptions::default()
915-
},
916-
f,
917-
),
918928
_ => self.argument.fmt(f),
919929
}
920930
}

crates/oxc_formatter/src/write/function.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ use crate::{
2020
},
2121
};
2222

23+
impl<'a> FormatWrite<'a, FormatFunctionOptions> for AstNode<'a, Function<'a>> {
24+
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
25+
FormatFunction::new(self).fmt(f)
26+
}
27+
28+
fn write_with_options(
29+
&self,
30+
options: FormatFunctionOptions,
31+
f: &mut Formatter<'_, 'a>,
32+
) -> FormatResult<()> {
33+
FormatFunction::new_with_options(self, options).fmt(f)
34+
}
35+
}
36+
2337
#[derive(Copy, Clone, Debug, Default)]
2438
pub struct FormatFunctionOptions {
2539
pub call_argument_layout: Option<GroupedCallArgumentLayout>,
@@ -40,8 +54,20 @@ impl<'a> Deref for FormatFunction<'a, '_> {
4054
}
4155
}
4256

43-
impl<'a> Format<'a> for FormatFunction<'a, '_> {
44-
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
57+
impl<'a, 'b> FormatFunction<'a, 'b> {
58+
pub fn new(function: &'b AstNode<'a, Function<'a>>) -> Self {
59+
Self { function, options: FormatFunctionOptions::default() }
60+
}
61+
62+
pub fn new_with_options(
63+
function: &'b AstNode<'a, Function<'a>>,
64+
options: FormatFunctionOptions,
65+
) -> Self {
66+
Self { function, options }
67+
}
68+
69+
#[inline]
70+
pub fn format(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
4571
let head = format_with(|f| {
4672
write!(
4773
f,
@@ -144,17 +170,9 @@ impl<'a> Format<'a> for FormatFunction<'a, '_> {
144170
}
145171
}
146172

147-
impl<'a> FormatWrite<'a, FormatFunctionOptions> for AstNode<'a, Function<'a>> {
148-
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
149-
FormatFunction { function: self, options: FormatFunctionOptions::default() }.fmt(f)
150-
}
151-
152-
fn write_with_options(
153-
&self,
154-
options: FormatFunctionOptions,
155-
f: &mut Formatter<'_, 'a>,
156-
) -> FormatResult<()> {
157-
FormatFunction { function: self, options }.fmt(f)
173+
impl<'a> Format<'a> for FormatFunction<'a, '_> {
174+
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
175+
self.format(f)
158176
}
159177
}
160178

crates/oxc_formatter/src/write/mod.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -988,22 +988,6 @@ impl<'a> FormatWrite<'a> for AstNode<'a, BindingRestElement<'a>> {
988988
}
989989
}
990990

991-
impl<'a> FormatWrite<'a, FormatJsArrowFunctionExpressionOptions>
992-
for AstNode<'a, ArrowFunctionExpression<'a>>
993-
{
994-
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
995-
FormatJsArrowFunctionExpression::new(self).fmt(f)
996-
}
997-
998-
fn write_with_options(
999-
&self,
1000-
options: FormatJsArrowFunctionExpressionOptions,
1001-
f: &mut Formatter<'_, 'a>,
1002-
) -> FormatResult<()> {
1003-
FormatJsArrowFunctionExpression::new_with_options(self, options).fmt(f)
1004-
}
1005-
}
1006-
1007991
impl<'a> FormatWrite<'a> for AstNode<'a, YieldExpression<'a>> {
1008992
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
1009993
write!(f, ["yield", self.delegate().then_some("*")])?;

0 commit comments

Comments
 (0)