diff --git a/crates/oxc_formatter/src/utils/assignment_like.rs b/crates/oxc_formatter/src/utils/assignment_like.rs index 1f4016bd27a8f..40cfdc95122a2 100644 --- a/crates/oxc_formatter/src/utils/assignment_like.rs +++ b/crates/oxc_formatter/src/utils/assignment_like.rs @@ -918,7 +918,9 @@ fn is_poorly_breakable_member_or_call_chain<'a>( let is_breakable_call = match args.len() { 0 => false, 1 => match args.iter().next() { - Some(first_argument) => !is_short_argument(first_argument, threshold, f), + Some(first_argument) => first_argument + .as_expression() + .is_none_or(|e| !is_short_argument(e, threshold, f)), None => false, }, _ => true, @@ -945,15 +947,14 @@ fn is_poorly_breakable_member_or_call_chain<'a>( /// We need it to decide if `JsCallExpression` with the argument is breakable or not /// If the argument is short the function call isn't breakable /// [Prettier applies]: -fn is_short_argument(argument: &Argument, threshold: u16, f: &Formatter) -> bool { - match argument { - Argument::Identifier(identifier) => identifier.name.len() <= threshold as usize, - Argument::UnaryExpression(unary_expression) => { - unary_expression.operator.is_arithmetic() - && matches!(unary_expression.argument, Expression::NumericLiteral(_)) +fn is_short_argument(expression: &Expression, threshold: u16, f: &Formatter) -> bool { + match expression { + Expression::Identifier(identifier) => identifier.name.len() <= threshold as usize, + Expression::UnaryExpression(unary_expression) => { + is_short_argument(&unary_expression.argument, threshold, f) } - Argument::RegExpLiteral(regex) => regex.regex.pattern.text.len() <= threshold as usize, - Argument::StringLiteral(literal) => { + Expression::RegExpLiteral(regex) => regex.regex.pattern.text.len() <= threshold as usize, + Expression::StringLiteral(literal) => { let formatter = FormatLiteralStringToken::new( f.source_text().text_for(literal.as_ref()), literal.span, @@ -964,7 +965,7 @@ fn is_short_argument(argument: &Argument, threshold: u16, f: &Formatter) -> bool formatter.clean_text(f.context().source_type(), f.options()).width() <= threshold as usize } - Argument::TemplateLiteral(literal) => { + Expression::TemplateLiteral(literal) => { let elements = &literal.expressions; // Besides checking length exceed we also need to check that the template doesn't have any expressions. @@ -975,11 +976,11 @@ fn is_short_argument(argument: &Argument, threshold: u16, f: &Formatter) -> bool raw.len() <= threshold as usize && !raw.contains('\n') } } - Argument::ThisExpression(_) - | Argument::NullLiteral(_) - | Argument::BigIntLiteral(_) - | Argument::BooleanLiteral(_) - | Argument::NumericLiteral(_) => true, + Expression::ThisExpression(_) + | Expression::NullLiteral(_) + | Expression::BigIntLiteral(_) + | Expression::BooleanLiteral(_) + | Expression::NumericLiteral(_) => true, _ => false, } } diff --git a/crates/oxc_formatter/tests/fixtures/ts/assignments/short-argument.ts b/crates/oxc_formatter/tests/fixtures/ts/assignments/short-argument.ts new file mode 100644 index 0000000000000..fe7819666ea7f --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/assignments/short-argument.ts @@ -0,0 +1,2 @@ +const requestTrie = + TernarySearchTree.forPaths(!isLinux); diff --git a/crates/oxc_formatter/tests/fixtures/ts/assignments/short-argument.ts.snap b/crates/oxc_formatter/tests/fixtures/ts/assignments/short-argument.ts.snap new file mode 100644 index 0000000000000..12865cfd95812 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/assignments/short-argument.ts.snap @@ -0,0 +1,12 @@ +--- +source: crates/oxc_formatter/tests/fixtures/mod.rs +--- +==================== Input ==================== +const requestTrie = + TernarySearchTree.forPaths(!isLinux); + +==================== Output ==================== +const requestTrie = + TernarySearchTree.forPaths(!isLinux); + +===================== End =====================