From 990916ae7f0d3925418e78c23f497b5d94698409 Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Wed, 22 Oct 2025 07:34:10 +0000 Subject: [PATCH] fix(formatter): correct handling of leading own line before arrow function body (#14877) close: https://github.com/oxc-project/oxc/issues/14871 --- .../src/write/arrow_function_expression.rs | 12 ++++------ .../tests/fixtures/js/comments/arrow.js | 7 ++++++ .../tests/fixtures/js/comments/arrow.js.snap | 22 +++++++++++++++++++ 3 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 crates/oxc_formatter/tests/fixtures/js/comments/arrow.js create mode 100644 crates/oxc_formatter/tests/fixtures/js/comments/arrow.js.snap diff --git a/crates/oxc_formatter/src/write/arrow_function_expression.rs b/crates/oxc_formatter/src/write/arrow_function_expression.rs index e76a840e88666..7bf5dc8339b3e 100644 --- a/crates/oxc_formatter/src/write/arrow_function_expression.rs +++ b/crates/oxc_formatter/src/write/arrow_function_expression.rs @@ -159,22 +159,18 @@ impl<'a> Format<'a> for FormatJsArrowFunctionExpression<'a, '_> { }; } - #[expect(clippy::match_same_arms)] - let body_has_soft_line_break = arrow_expression.is_none_or(|expression| { - match expression { + let body_has_soft_line_break = + arrow_expression.is_none_or(|expression| match expression { Expression::ArrowFunctionExpression(_) | Expression::ArrayExpression(_) | Expression::ObjectExpression(_) => { - // TODO: It seems no difference whether check there is a leading comment or not. - // !f.comments().has_leading_own_line_comment(body.span().start) - true + !f.comments().has_leading_own_line_comment(body.span().start) } Expression::JSXElement(_) | Expression::JSXFragment(_) => true, _ => { is_multiline_template_starting_on_same_line(expression, f.source_text()) } - } - }); + }); let body_is_condition_type = matches!(arrow_expression, Some(Expression::ConditionalExpression(_))); diff --git a/crates/oxc_formatter/tests/fixtures/js/comments/arrow.js b/crates/oxc_formatter/tests/fixtures/js/comments/arrow.js new file mode 100644 index 0000000000000..9bc75b8521601 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/comments/arrow.js @@ -0,0 +1,7 @@ +() => + // comment + []; + +() => + // comment + ({}) diff --git a/crates/oxc_formatter/tests/fixtures/js/comments/arrow.js.snap b/crates/oxc_formatter/tests/fixtures/js/comments/arrow.js.snap new file mode 100644 index 0000000000000..d3afaa4201b23 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/comments/arrow.js.snap @@ -0,0 +1,22 @@ +--- +source: crates/oxc_formatter/tests/fixtures/mod.rs +--- +==================== Input ==================== +() => + // comment + []; + +() => + // comment + ({}) + +==================== Output ==================== +() => + // comment + []; + +() => + // comment + ({}); + +===================== End =====================