Skip to content

Commit

Permalink
fix(rome_js_formatter): Parity with prettier for many function argume…
Browse files Browse the repository at this point in the history
…nts rome#4229
  • Loading branch information
denbezrukov committed Mar 4, 2023
1 parent 40f9c7d commit 5d20492
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 84 deletions.
17 changes: 13 additions & 4 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,9 @@ use rome_formatter::write;

use crate::utils::FormatInitializerClause;

use crate::js::bindings::parameters::{should_hug_function_parameters, FormatAnyJsParameters};
use rome_js_syntax::JsFormalParameter;
use rome_js_syntax::JsFormalParameterFields;
use rome_js_syntax::{AnyJsBindingPattern, JsFormalParameter};

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatJsFormalParameter;
Expand All @@ -29,10 +30,18 @@ impl FormatNodeRule<JsFormalParameter> for FormatJsFormalParameter {
]
});

if let AnyJsBindingPattern::JsObjectBindingPattern(_) = node.binding()? {
write![f, [group(&content)]]?;
} else {
let is_hug_parameter = node
.syntax()
.grand_parent()
.and_then(FormatAnyJsParameters::cast)
.map_or(false, |parameters| {
should_hug_function_parameters(&parameters, f.comments()).unwrap_or(false)
});

if is_hug_parameter {
write![f, [content]]?;
} else {
write![f, [group(&content)]]?;
}

write![f, [FormatInitializerClause::new(initializer.as_ref())]]
Expand Down
23 changes: 15 additions & 8 deletions crates/rome_js_formatter/src/ts/expressions/type_arguments.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::utils::should_hug_type;
use crate::{prelude::*, utils::is_object_like_type};
use rome_formatter::write;
use rome_formatter::FormatError::SyntaxError;
use rome_formatter::{format_args, write};
use rome_js_syntax::{
AnyJsExpression, AnyTsType, JsSyntaxKind, JsVariableDeclarator, TsTypeArguments,
TsTypeArgumentsFields,
Expand Down Expand Up @@ -75,17 +75,24 @@ impl FormatNodeRule<TsTypeArguments> for FormatTsTypeArguments {
let should_inline = !is_arrow_function_variables
&& (ts_type_argument_list.len() == 0 || first_argument_can_be_hugged_or_is_null_type);

write!(f, [l_angle_token.format(),])?;

if should_inline {
write!(f, [ts_type_argument_list.format()])?;
write!(
f,
[
l_angle_token.format(),
ts_type_argument_list.format(),
r_angle_token.format()
]
)
} else {
write!(
f,
[group(&soft_block_indent(&ts_type_argument_list.format()))]
)?;
[group(&format_args![
l_angle_token.format(),
soft_block_indent(&ts_type_argument_list.format()),
r_angle_token.format()
])]
)
}

write!(f, [r_angle_token.format()])
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,17 @@ export function formatNumber2(
useGrouping?: boolean;
}
): string {}


export const findByDatefindByDatefindByDatefindByDate =
(_, { date }, { req } ) => findByDatefindByDatefindByDatefindByDate;

export const queryAuditLog = async ({
startDate,
endDate,
jobId,
src,
type,
}: Filter): Promise<DBAuditLog[]> => {

};
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
---
source: crates/rome_formatter_test/src/snapshot_builder.rs
info: ts/function/parameters/function_parameters.ts
---

# Input

```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 {}


export const findByDatefindByDatefindByDatefindByDate =
(_, { date }, { req } ) => findByDatefindByDatefindByDatefindByDate;

export const queryAuditLog = async ({
startDate,
endDate,
jobId,
src,
type,
}: Filter): Promise<DBAuditLog[]> => {

};

```


=============================

# Outputs

## Output 1

-----
Indent style: Tab
Line width: 80
Quote style: Double Quotes
Quote properties: As needed
Trailing comma: All
Semicolons: Always
-----

```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 {}

export const findByDatefindByDatefindByDatefindByDate = (
_,
{ date },
{ req },
) => findByDatefindByDatefindByDatefindByDate;

export const queryAuditLog = async ({
startDate,
endDate,
jobId,
src,
type,
}: Filter): Promise<DBAuditLog[]> => {};
```

## Output 2

-----
Indent style: Tab
Line width: 100
Quote style: Double Quotes
Quote properties: As needed
Trailing comma: All
Semicolons: Always
-----

```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 {}

export const findByDatefindByDatefindByDatefindByDate = (_, { date }, { req }) =>
findByDatefindByDatefindByDatefindByDate;

export const queryAuditLog = async ({
startDate,
endDate,
jobId,
src,
type,
}: Filter): Promise<DBAuditLog[]> => {};
```

## Output 3

-----
Indent style: Tab
Line width: 120
Quote style: Double Quotes
Quote properties: As needed
Trailing comma: All
Semicolons: Always
-----

```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 {}

export const findByDatefindByDatefindByDatefindByDate = (_, { date }, { req }) =>
findByDatefindByDatefindByDatefindByDate;

export const queryAuditLog = async ({ startDate, endDate, jobId, src, type }: Filter): Promise<DBAuditLog[]> => {};
```


Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"cases": [
{
"line_width": 100
},
{
"line_width": 120
}
]
}

0 comments on commit 5d20492

Please sign in to comment.