Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
fix(rome_js_formatter): Inline object like pattern for formal paramet…
Browse files Browse the repository at this point in the history
…er (#3802)

Co-authored-by: Micha Reiser <[email protected]>
  • Loading branch information
denbezrukov and MichaReiser committed Nov 21, 2022
1 parent 3bcf54c commit cb33205
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 12 deletions.
27 changes: 18 additions & 9 deletions crates/rome_js_formatter/src/js/bindings/formal_parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,14 +18,23 @@ impl FormatNodeRule<JsFormalParameter> for FormatJsFormalParameter {
initializer,
} = node.as_fields();

write![
f,
[
binding.format(),
question_mark_token.format(),
type_annotation.format(),
FormatInitializerClause::new(initializer.as_ref())
let content = format_with(|f| {
write![
f,
[
binding.format(),
question_mark_token.format(),
type_annotation.format()
]
]
]
});

if let JsAnyBindingPattern::JsObjectBindingPattern(_) = node.binding()? {
write![f, [group(&content)]]?;
} else {
write![f, [content]]?;
}

write![f, [FormatInitializerClause::new(initializer.as_ref())]]
}
}
14 changes: 11 additions & 3 deletions crates/rome_js_formatter/src/utils/object_pattern_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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
)
);

Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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 {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
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 {}
```


0 comments on commit cb33205

Please sign in to comment.