From 52c062640adf9e07dcd2037369d84c2167f92f63 Mon Sep 17 00:00:00 2001 From: Superchupu <53496941+SuperchupuDev@users.noreply.github.com> Date: Tue, 18 Jul 2023 10:16:27 +0000 Subject: [PATCH 1/2] fix(rome_js_formatter): improve handling of arrow parentheses --- crates/rome_cli/tests/commands/format.rs | 14 +++++++++++ .../expressions/arrow_function_expression.rs | 7 +++++- crates/rome_js_formatter/tests/quick_test.rs | 25 ++++++++++++------- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/crates/rome_cli/tests/commands/format.rs b/crates/rome_cli/tests/commands/format.rs index d09b870edb6..a216d86f834 100644 --- a/crates/rome_cli/tests/commands/format.rs +++ b/crates/rome_cli/tests/commands/format.rs @@ -55,6 +55,13 @@ const APPLY_TRAILING_COMMA_AFTER: &str = r#"const a = [ const APPLY_ARROW_PARENTHESES_BEFORE: &str = r#" action => {} (action) => {} +(action?) => {} +(action: h) => {} +(action): h => {} +( + action + // hhhhhhhh +) => {} ({ action }) => {} ([ action ]) => {} (...action) => {} @@ -63,6 +70,13 @@ action => {} const APPLY_ARROW_PARENTHESES_AFTER: &str = r#"action => {}; action => {}; +(action?) => {}; +(action: h) => {}; +(action): h => {}; +( + action + // hhhhhhhh +) => {} ({ action }) => {}; ([action]) => {}; (...action) => {}; diff --git a/crates/rome_js_formatter/src/js/expressions/arrow_function_expression.rs b/crates/rome_js_formatter/src/js/expressions/arrow_function_expression.rs index d291fc95ee7..e0b9ddf5833 100644 --- a/crates/rome_js_formatter/src/js/expressions/arrow_function_expression.rs +++ b/crates/rome_js_formatter/src/js/expressions/arrow_function_expression.rs @@ -344,7 +344,12 @@ pub fn can_avoid_parentheses(arrow: &JsArrowFunctionExpression, f: &mut JsFormat .as_js_parameters() .and_then(|p| p.items().first()?.ok()) .and_then(|p| JsFormalParameter::cast(p.syntax().clone())) - .is_some_and(|p| p.initializer().is_some()) + .is_some_and(|p| { + f.context().comments().has_comments(p.syntax()) + || p.initializer().is_some() + || p.question_mark_token().is_some() + || p.type_annotation().is_some() + }) }) } diff --git a/crates/rome_js_formatter/tests/quick_test.rs b/crates/rome_js_formatter/tests/quick_test.rs index 0faf23952e9..bde26a22f2d 100644 --- a/crates/rome_js_formatter/tests/quick_test.rs +++ b/crates/rome_js_formatter/tests/quick_test.rs @@ -13,8 +13,11 @@ mod language { // use this test check if your snippet prints as you wish, without using a snapshot fn quick_test() { let src = r#" - action => {} - (action) => {} + (action: h) => {} + (action?) => {} + (action + // yes + ) => {} ({ action }) => {} ([ action ]) => {} (...action) => {} @@ -28,7 +31,7 @@ fn quick_test() { ); dbg!(tree.syntax()); let options = JsFormatOptions::new(syntax) - .with_semicolons(Semicolons::AsNeeded) + .with_semicolons(Semicolons::Always) .with_quote_style(QuoteStyle::Double) .with_jsx_quote_style(QuoteStyle::Single) .with_arrow_parentheses(ArrowParentheses::AsNeeded); @@ -46,12 +49,16 @@ fn quick_test() { // I don't know why semicolons are added there, but it's not related to my code changes so ¯\_(ツ)_/¯ assert_eq!( result.as_code(), - r#";action => {} -;action => {} -;({ action }) => {} -;([action]) => {} -;(...action) => {} -;(action = 1) => {} + r#"(action: h) => {}; +(action?) => {}; +( + action, + // yes +) => {}; +({ action }) => {}; +([action]) => {}; +(...action) => {}; +(action = 1) => {}; "# ); } From cd530e212a9ded75c01616831f050aa1d519da27 Mon Sep 17 00:00:00 2001 From: Superchupu <53496941+SuperchupuDev@users.noreply.github.com> Date: Tue, 18 Jul 2023 10:37:54 +0000 Subject: [PATCH 2/2] chore: move the new tests to their own file --- crates/rome_cli/tests/commands/format.rs | 14 --- .../tests/specs/ts/arrow/arrow_parentheses.ts | 13 +++ .../specs/ts/arrow/arrow_parentheses.ts.snap | 88 +++++++++++++++++++ .../tests/specs/ts/arrow/options.json | 7 ++ 4 files changed, 108 insertions(+), 14 deletions(-) create mode 100644 crates/rome_js_formatter/tests/specs/ts/arrow/arrow_parentheses.ts create mode 100644 crates/rome_js_formatter/tests/specs/ts/arrow/arrow_parentheses.ts.snap create mode 100644 crates/rome_js_formatter/tests/specs/ts/arrow/options.json diff --git a/crates/rome_cli/tests/commands/format.rs b/crates/rome_cli/tests/commands/format.rs index a216d86f834..d09b870edb6 100644 --- a/crates/rome_cli/tests/commands/format.rs +++ b/crates/rome_cli/tests/commands/format.rs @@ -55,13 +55,6 @@ const APPLY_TRAILING_COMMA_AFTER: &str = r#"const a = [ const APPLY_ARROW_PARENTHESES_BEFORE: &str = r#" action => {} (action) => {} -(action?) => {} -(action: h) => {} -(action): h => {} -( - action - // hhhhhhhh -) => {} ({ action }) => {} ([ action ]) => {} (...action) => {} @@ -70,13 +63,6 @@ action => {} const APPLY_ARROW_PARENTHESES_AFTER: &str = r#"action => {}; action => {}; -(action?) => {}; -(action: h) => {}; -(action): h => {}; -( - action - // hhhhhhhh -) => {} ({ action }) => {}; ([action]) => {}; (...action) => {}; diff --git a/crates/rome_js_formatter/tests/specs/ts/arrow/arrow_parentheses.ts b/crates/rome_js_formatter/tests/specs/ts/arrow/arrow_parentheses.ts new file mode 100644 index 00000000000..47dd080ee33 --- /dev/null +++ b/crates/rome_js_formatter/tests/specs/ts/arrow/arrow_parentheses.ts @@ -0,0 +1,13 @@ +action => {} +(action) => {} +(action?) => {} +(action: string) => {} +(action): void => {} +( + action + // hhhhhhhh +) => {} +({ action }) => {} +([ action ]) => {} +(...action) => {} +(action = 1) => {} diff --git a/crates/rome_js_formatter/tests/specs/ts/arrow/arrow_parentheses.ts.snap b/crates/rome_js_formatter/tests/specs/ts/arrow/arrow_parentheses.ts.snap new file mode 100644 index 00000000000..2ca93d44a6a --- /dev/null +++ b/crates/rome_js_formatter/tests/specs/ts/arrow/arrow_parentheses.ts.snap @@ -0,0 +1,88 @@ +--- +source: crates/rome_formatter_test/src/snapshot_builder.rs +info: ts/arrow/arrow_parentheses.ts +--- + +# Input + +```ts +action => {} +(action) => {} +(action?) => {} +(action: string) => {} +(action): void => {} +( + action + // hhhhhhhh +) => {} +({ action }) => {} +([ action ]) => {} +(...action) => {} +(action = 1) => {} + +``` + + +============================= + +# Outputs + +## Output 1 + +----- +Indent style: Tab +Line width: 80 +Quote style: Double Quotes +JSX quote style: Double Quotes +Quote properties: As needed +Trailing comma: All +Semicolons: Always +Arrow parentheses: Always +----- + +```ts +(action) => {}; +(action) => {}; +(action?) => {}; +(action: string) => {}; +(action): void => {}; +( + action, + // hhhhhhhh +) => {}; +({ action }) => {}; +([action]) => {}; +(...action) => {}; +(action = 1) => {}; +``` + +## Output 2 + +----- +Indent style: Tab +Line width: 80 +Quote style: Double Quotes +JSX quote style: Double Quotes +Quote properties: As needed +Trailing comma: All +Semicolons: Always +Arrow parentheses: As needed +----- + +```ts +action => {}; +action => {}; +(action?) => {}; +(action: string) => {}; +(action): void => {}; +( + action, + // hhhhhhhh +) => {}; +({ action }) => {}; +([action]) => {}; +(...action) => {}; +(action = 1) => {}; +``` + + diff --git a/crates/rome_js_formatter/tests/specs/ts/arrow/options.json b/crates/rome_js_formatter/tests/specs/ts/arrow/options.json new file mode 100644 index 00000000000..7ef4276dd98 --- /dev/null +++ b/crates/rome_js_formatter/tests/specs/ts/arrow/options.json @@ -0,0 +1,7 @@ +{ + "cases": [ + { + "arrow_parentheses": "AsNeeded" + } + ] +}