Skip to content

Commit 4b98d98

Browse files
committed
fix(formatter): don't print CallExpression as MemberChain style when its only has one argument and it is a TemplateLiteral on its own line
1 parent 5333332 commit 4b98d98

File tree

3 files changed

+29
-36
lines changed

3 files changed

+29
-36
lines changed

crates/oxc_formatter/src/write/arrow_function_expression.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -170,21 +170,9 @@ impl<'a> Format<'a> for FormatJsArrowFunctionExpression<'a, '_> {
170170
true
171171
}
172172
Expression::JSXElement(_) | Expression::JSXFragment(_) => true,
173-
Expression::TemplateLiteral(template) => {
174-
is_multiline_template_starting_on_same_line(
175-
template.span.start,
176-
template,
177-
f.source_text(),
178-
)
173+
_ => {
174+
is_multiline_template_starting_on_same_line(expression, f.source_text())
179175
}
180-
Expression::TaggedTemplateExpression(template) => {
181-
is_multiline_template_starting_on_same_line(
182-
template.span.start,
183-
&template.quasi,
184-
f.source_text(),
185-
)
186-
}
187-
_ => false,
188176
}
189177
});
190178

@@ -392,10 +380,15 @@ impl<'a, 'b> ArrowFunctionLayout<'a, 'b> {
392380
///
393381
/// Returns `false` because the template isn't on the same line as the '+' token.
394382
pub fn is_multiline_template_starting_on_same_line(
395-
start: u32,
396-
template: &TemplateLiteral,
383+
expression: &Expression,
397384
source_text: SourceText,
398385
) -> bool {
386+
let (start, template) = match expression {
387+
Expression::TemplateLiteral(template) => (template.span.start, template.as_ref()),
388+
Expression::TaggedTemplateExpression(tagged) => (tagged.span.start, &tagged.quasi),
389+
_ => return false,
390+
};
391+
399392
template.quasis.iter().any(|quasi| source_text.contains_newline(quasi.span))
400393
&& !source_text.has_newline_before(start)
401394
}

crates/oxc_formatter/src/write/call_arguments.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -959,19 +959,11 @@ fn is_multiline_template_only_args(arguments: &[Argument], source_text: SourceTe
959959
return false;
960960
}
961961

962-
match arguments.first().unwrap() {
963-
Argument::TemplateLiteral(template) => {
964-
is_multiline_template_starting_on_same_line(template.span.start, template, source_text)
965-
}
966-
Argument::TaggedTemplateExpression(template) => {
967-
is_multiline_template_starting_on_same_line(
968-
template.span.start,
969-
&template.quasi,
970-
source_text,
971-
)
972-
}
973-
_ => false,
974-
}
962+
arguments
963+
.first()
964+
.unwrap()
965+
.as_expression()
966+
.is_some_and(|expr| is_multiline_template_starting_on_same_line(expr, source_text))
975967
}
976968

977969
/// This function is used to check if the code is a hook-like code:

crates/oxc_formatter/src/write/mod.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ use crate::{
7878

7979
use self::{
8080
array_expression::FormatArrayExpression,
81+
arrow_function_expression::is_multiline_template_starting_on_same_line,
8182
class::format_grouped_parameters_with_return_type,
8283
function::should_group_function_parameters,
8384
object_like::ObjectLike,
@@ -215,13 +216,20 @@ impl<'a> FormatWrite<'a> for AstNode<'a, CallExpression<'a>> {
215216
let arguments = self.arguments();
216217
let optional = self.optional();
217218

218-
if callee.as_member_expression().is_some_and(|e| {
219-
matches!(
220-
e,
221-
MemberExpression::StaticMemberExpression(_)
222-
| MemberExpression::ComputedMemberExpression(_)
223-
)
224-
}) && !callee.needs_parentheses(f)
219+
let is_template_literal_single_arg = arguments.len() == 1
220+
&& arguments.first().unwrap().as_expression().is_some_and(|expr| {
221+
is_multiline_template_starting_on_same_line(expr, f.source_text())
222+
});
223+
224+
if !is_template_literal_single_arg
225+
&& callee.as_member_expression().is_some_and(|e| {
226+
matches!(
227+
e,
228+
MemberExpression::StaticMemberExpression(_)
229+
| MemberExpression::ComputedMemberExpression(_)
230+
)
231+
})
232+
&& !callee.needs_parentheses(f)
225233
{
226234
MemberChain::from_call_expression(self, f).fmt(f)
227235
} else {

0 commit comments

Comments
 (0)