fix(rome_js_formatter): Inline object like pattern for formal parameter
MODIFY
crates/rome_js_formatter/src/js/bindings/formal_parameter.rs
crates/rome_js_formatter/src/js/bindings/formal_parameter.rs
@@ -3,8 +3,8 @@ use rome_formatter::write;
use crate::utils::FormatInitializerClause;
-use rome_js_syntax::JsFormalParameter;
use rome_js_syntax::JsFormalParameterFields;
+use rome_js_syntax::{JsAnyBindingPattern, JsFormalParameter};
#[derive(Debug, Clone, Default)]
pub struct FormatJsFormalParameter;
@@ -18,14 +18,23 @@ impl FormatNodeRule<JsFormalParameter> for FormatJsFormalParameter {
initializer,
} = node.as_fields();
+ let content = format_with(|f| {
write![
f,
[
binding.format(),
question_mark_token.format(),
- type_annotation.format(),
- FormatInitializerClause::new(initializer.as_ref())
+ type_annotation.format()
]
]
+ });
+
+ if let JsAnyBindingPattern::JsObjectBindingPattern(_) = node.binding()? {
+ write![f, [group(&content)]]?;
+ } else {
+ write![f, [content]]?;
+ }
+
+ write![f, [FormatInitializerClause::new(initializer.as_ref())]]
}
}

MODIFY
crates/rome_js_formatter/src/utils/object_pattern_like.rs
crates/rome_js_formatter/src/utils/object_pattern_like.rs
@@ -48,6 +48,16 @@ impl JsObjectPatternLike {
}
}
+ fn is_inline(&self, comments: &JsComments) -> FormatResult<bool> {
+ let parent_kind = self.syntax().parent().kind();
+
+ Ok(
+ (matches!(parent_kind, Some(JsSyntaxKind::JS_FORMAL_PARAMETER))
+ || self.is_hug_parameter(comments))
+ && !self.l_curly_token()?.leading_trivia().has_skipped(),
+ )
+ }
+
fn should_break_properties(&self) -> bool {
let parent_kind = self.syntax().parent().kind();
@@ -60,7 +70,6 @@ impl JsObjectPatternLike {
| JsSyntaxKind::JS_OBJECT_ASSIGNMENT_PATTERN_PROPERTY
| JsSyntaxKind::JS_CATCH_DECLARATION
| JsSyntaxKind::JS_OBJECT_BINDING_PATTERN_PROPERTY
- | JsSyntaxKind::JS_FORMAL_PARAMETER
)
);
@@ -132,8 +141,7 @@ impl JsObjectPatternLike {
return Ok(ObjectPatternLayout::Empty);
}
- if self.is_hug_parameter(comments) && !self.l_curly_token()?.leading_trivia().has_skipped()
- {
+ if self.is_inline(comments)? {
return Ok(ObjectPatternLayout::Inline);
}

ADD
crates/rome_js_formatter/tests/specs/ts/function/function_parameters.ts
+export function formatNumber1(
+ value: string,
+ {
+ a,
+ b,
+ c,
+ formatNumber,
+ ...props
+ }: Omit<NumberFormatterProps, 'value' | 'defaultFractionDigits'> & {
+ useGrouping?: boolean;
+ }
+): string {}
+
+export function formatNumber2(
+ value: string,
+ { a }: Omit<NumberFormatterProps, 'value' | 'defaultFractionDigits'> & {
+ useGrouping?: boolean;
+ }
+): string {}

ADD
crates/rome_js_formatter/tests/specs/ts/function/function_parameters.ts.snap
+---
+source: crates/rome_js_formatter/tests/spec_test.rs
+expression: function_parameters.ts
+---
+
+# Input
+
+```js
+export function formatNumber1(
+ value: string,
+ {
+ a,
+ b,
+ c,
+ formatNumber,
+ ...props
+ }: Omit<NumberFormatterProps, 'value' | 'defaultFractionDigits'> & {
+ useGrouping?: boolean;
+ }
+): string {}
+
+export function formatNumber2(
+ value: string,
+ { a }: Omit<NumberFormatterProps, 'value' | 'defaultFractionDigits'> & {
+ useGrouping?: boolean;
+ }
+): string {}
+
+```
+
+
+=============================
+
+# Outputs
+
+## Output 1
+
+-----
+Indent style: Tab
+Line width: 80
+Quote style: Double Quotes
+Quote properties: As needed
+Trailing comma: All
+Semicolons: Always
+-----
+
+```js
+export function formatNumber1(
+ value: string,
+ {
+ a,
+ b,
+ c,
+ formatNumber,
+ ...props
+ }: Omit<NumberFormatterProps, "value" | "defaultFractionDigits"> & {
+ useGrouping?: boolean;
+ },
+): string {}
+
+export function formatNumber2(
+ value: string,
+ {
+ a,
+ }: Omit<NumberFormatterProps, "value" | "defaultFractionDigits"> & {
+ useGrouping?: boolean;
+ },
+): string {}
+```
+
+

RENAME
crates/rome_js_formatter/tests/specs/ts/function/function_trailing_comma.ts
crates/rome_js_formatter/tests/specs/ts/function/trailing_comma/function_trailing_comma.ts
RENAME
crates/rome_js_formatter/tests/specs/ts/function/function_trailing_comma.ts.snap
crates/rome_js_formatter/tests/specs/ts/function/trailing_comma/function_trailing_comma.ts.snap
RENAME
crates/rome_js_formatter/tests/specs/ts/function/options.json
crates/rome_js_formatter/tests/specs/ts/function/trailing_comma/options.json
fix(rome_js_formatter): Inline object like pattern for formal parameter (#3802)

Co-authored-by: Micha Reiser <micha@reiser.io>