fix(rome_js_formatter): Fix arrow binding parameter in test call
MODIFY
crates/rome_js_formatter/src/js/bindings/parameters.rs
crates/rome_js_formatter/src/js/bindings/parameters.rs
@@ -5,10 +5,10 @@ use crate::js::lists::parameter_list::{
AnyParameter, FormatJsAnyParameterList, JsAnyParameterList,
};
-use crate::utils::test_call::is_test_call_expression;
+use crate::utils::test_call::is_test_call_argument;
use rome_js_syntax::{
- JsAnyConstructorParameter, JsAnyFormalParameter, JsCallExpression, JsConstructorParameters,
- JsParameters, JsSyntaxKind, JsSyntaxToken, TsType,
+ JsAnyConstructorParameter, JsAnyFormalParameter, JsConstructorParameters, JsParameters,
+ JsSyntaxToken, TsType,
};
use rome_rowan::{declare_node_union, SyntaxResult};
@@ -129,29 +129,10 @@ impl FormatJsAnyParameters {
/// Returns `true` for function parameters if the function is an argument of a [test `CallExpression`](is_test_call_expression).
fn is_in_test_call(&self) -> SyntaxResult<bool> {
let result = match self {
- FormatJsAnyParameters::JsParameters(parameters) => {
- match parameters.syntax().grand_parent() {
- Some(function_parent) => match function_parent.kind() {
- JsSyntaxKind::JS_CALL_ARGUMENT_LIST => {
- let arguments = function_parent.parent();
- let call_expression = arguments.and_then(|args| args.parent());
-
- match call_expression {
- Some(call_expression)
- if JsCallExpression::can_cast(call_expression.kind()) =>
- {
- is_test_call_expression(&JsCallExpression::unwrap_cast(
- call_expression,
- ))?
- }
- _ => false,
- }
- }
- _ => false,
- },
+ FormatJsAnyParameters::JsParameters(parameters) => match parameters.syntax().parent() {
+ Some(function) => is_test_call_argument(&function)?,
None => false,
- }
- }
+ },
FormatJsAnyParameters::JsConstructorParameters(_) => false,
};

MODIFY
crates/rome_js_formatter/src/js/expressions/arrow_function_expression.rs
crates/rome_js_formatter/src/js/expressions/arrow_function_expression.rs
@@ -10,6 +10,7 @@ use crate::parentheses::{
update_or_lower_expression_needs_parentheses, NeedsParentheses,
};
use crate::utils::function_body::{FormatMaybeCachedFunctionBody, FunctionBodyCacheMode};
+use crate::utils::test_call::is_test_call_argument;
use crate::utils::{
resolve_left_most_expression, AssignmentLikeLayout, JsAnyBinaryLikeLeftExpression,
};
@@ -222,17 +223,25 @@ fn format_signature(
write!(f, [arrow.type_parameters().format()])?;
match arrow.parameters()? {
- JsAnyArrowFunctionParameters::JsAnyBinding(binding) => write!(
+ JsAnyArrowFunctionParameters::JsAnyBinding(binding) => {
+ let should_hug = is_test_call_argument(arrow.syntax())?;
+
+ write!(f, [text("(")])?;
+
+ if should_hug {
+ write!(f, [binding.format()])?;
+ } else {
+ write!(
f,
- [
- text("("),
- &soft_block_indent(&format_args![
+ [&soft_block_indent(&format_args![
binding.format(),
if_group_breaks(&text(","))
- ]),
- text(")")
- ]
- )?,
+ ])]
+ )?
+ }
+
+ write!(f, [text(")")])?;
+ }
JsAnyArrowFunctionParameters::JsParameters(params) => {
write!(f, [params.format()])?;
}

MODIFY
crates/rome_js_formatter/src/utils/test_call.rs
crates/rome_js_formatter/src/utils/test_call.rs
@@ -2,9 +2,21 @@ use crate::prelude::*;
use rome_js_syntax::{
JsAnyArrowFunctionParameters, JsAnyCallArgument, JsAnyExpression, JsAnyFunctionBody,
JsAnyLiteralExpression, JsAnyName, JsCallArgumentList, JsCallArguments, JsCallExpression,
+ JsSyntaxNode,
};
use rome_rowan::{SyntaxResult, SyntaxTokenText};
+/// Returns `Ok(true)` if `maybe_argument` is an argument of a [test call expression](is_test_call_expression).
+pub(crate) fn is_test_call_argument(maybe_argument: &JsSyntaxNode) -> SyntaxResult<bool> {
+ let call_expression = maybe_argument
+ .parent()
+ .and_then(JsCallArgumentList::cast)
+ .and_then(|args| args.syntax().grand_parent())
+ .and_then(JsCallExpression::cast);
+
+ call_expression.map_or(Ok(false), |call| is_test_call_expression(&call))
+}
+
/// This is a specialised function that checks if the current [call expression]
/// resembles a call expression usually used by a testing frameworks.
///

ADD
crates/rome_js_formatter/tests/specs/js/module/arrow/arrow_test_callback.js
+it('should have the default duration when using the onClose arguments', done => {
+ expect(true);
+ done();
+});

ADD
crates/rome_js_formatter/tests/specs/js/module/arrow/arrow_test_callback.js.snap
+---
+source: crates/rome_js_formatter/tests/spec_test.rs
+expression: arrow_test_callback.js
+---
+# Input
+it('should have the default duration when using the onClose arguments', done => {
+ expect(true);
+ done();
+});
+
+=============================
+# Outputs
+## Output 1
+-----
+Indent style: Tab
+Line width: 80
+Quote style: Double Quotes
+Quote properties: As needed
+-----
+it("should have the default duration when using the onClose arguments", (done) => {
+ expect(true);
+ done();
+});
+
+
+## Lines exceeding width of 80 characters
+
+ 1: it("should have the default duration when using the onClose arguments", (done) => {
+

fix(rome_js_formatter): Fix arrow binding parameter in test call (#3399)