diff --git a/crates/rome_js_formatter/src/js/expressions/unary_expression.rs b/crates/rome_js_formatter/src/js/expressions/unary_expression.rs index eae44ac6aa1..8ebc5ebe821 100644 --- a/crates/rome_js_formatter/src/js/expressions/unary_expression.rs +++ b/crates/rome_js_formatter/src/js/expressions/unary_expression.rs @@ -1,5 +1,5 @@ use crate::prelude::*; -use rome_formatter::{format_args, write, CstFormatContext}; +use rome_formatter::{format_args, write}; use crate::parentheses::{unary_like_expression_needs_parentheses, NeedsParentheses}; @@ -33,7 +33,9 @@ impl FormatNodeRule for FormatJsUnaryExpression { write!(f, [space()])?; } - if f.context().comments().has_comments(argument.syntax()) { + if f.comments().has_comments(argument.syntax()) + && !f.comments().is_suppressed(argument.syntax()) + { write!( f, [group(&format_args![ diff --git a/crates/rome_js_formatter/src/js/statements/return_statement.rs b/crates/rome_js_formatter/src/js/statements/return_statement.rs index 33b9b171361..cf348411eaa 100644 --- a/crates/rome_js_formatter/src/js/statements/return_statement.rs +++ b/crates/rome_js_formatter/src/js/statements/return_statement.rs @@ -110,12 +110,14 @@ pub(super) struct FormatReturnOrThrowArgument<'a>(&'a JsAnyExpression); impl Format for FormatReturnOrThrowArgument<'_> { fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let argument = self.0; + let is_suppressed = f.comments().is_suppressed(argument.syntax()); if has_argument_leading_comments(argument, f.context().comments()) && !matches!(argument, JsAnyExpression::JsxTagExpression(_)) + && !is_suppressed { write!(f, [text("("), &block_indent(&argument.format()), text(")")]) - } else if is_binary_or_sequence_argument(argument) { + } else if is_binary_or_sequence_argument(argument) && !is_suppressed { write!( f, [group(&format_args![ diff --git a/crates/rome_js_formatter/src/utils/conditional.rs b/crates/rome_js_formatter/src/utils/conditional.rs index 64e57a76cc3..b058de71259 100644 --- a/crates/rome_js_formatter/src/utils/conditional.rs +++ b/crates/rome_js_formatter/src/utils/conditional.rs @@ -778,7 +778,7 @@ fn format_jsx_chain_alternate(alternate: &JsAnyExpression) -> FormatJsxChainExpr } /// Wraps all expressions in parentheses if they break EXCEPT -/// * Nested conditionals in the alterante +/// * Nested conditionals in the alternate /// * `null` /// * `undefined` struct FormatJsxChainExpression<'a> { diff --git a/crates/rome_js_formatter/tests/specs/js/module/expression/unary_expression_verbatim_argument.js b/crates/rome_js_formatter/tests/specs/js/module/expression/unary_expression_verbatim_argument.js new file mode 100644 index 00000000000..0172fb4c6b7 --- /dev/null +++ b/crates/rome_js_formatter/tests/specs/js/module/expression/unary_expression_verbatim_argument.js @@ -0,0 +1,8 @@ +// https://github.com/rome/tools/issues/3735 + +!( + // rome-ignore format: Work around https://github.com/rome/tools/issues/3734 + a && b +); + + diff --git a/crates/rome_js_formatter/tests/specs/js/module/expression/unary_expression_verbatim_argument.js.snap b/crates/rome_js_formatter/tests/specs/js/module/expression/unary_expression_verbatim_argument.js.snap new file mode 100644 index 00000000000..bd97e72d2b3 --- /dev/null +++ b/crates/rome_js_formatter/tests/specs/js/module/expression/unary_expression_verbatim_argument.js.snap @@ -0,0 +1,44 @@ +--- +source: crates/rome_js_formatter/tests/spec_test.rs +expression: unary_expression_verbatim_argument.js +--- + +# Input + +```js +// https://github.com/rome/tools/issues/3735 + +!( + // rome-ignore format: Work around https://github.com/rome/tools/issues/3734 + a && b +); + + + +``` + + +============================= + +# Outputs + +## Output 1 + +----- +Indent style: Tab +Line width: 80 +Quote style: Double Quotes +Quote properties: As needed +Trailing comma: All +----- + +```js +// https://github.com/rome/tools/issues/3735 + +!( + // rome-ignore format: Work around https://github.com/rome/tools/issues/3734 + a && b +); +``` + + diff --git a/crates/rome_js_formatter/tests/specs/js/module/statement/return_verbatim_argument.js b/crates/rome_js_formatter/tests/specs/js/module/statement/return_verbatim_argument.js new file mode 100644 index 00000000000..a18a5413ac4 --- /dev/null +++ b/crates/rome_js_formatter/tests/specs/js/module/statement/return_verbatim_argument.js @@ -0,0 +1,25 @@ +// https://github.com/rome/tools/issues/3735 + +function supported1(){ + return ( + // rome-ignore format: Work around https://github.com/rome/tools/issues/3734 + // rome-ignore lint(style/useOptionalChain): Optional chaining creates more complicated ES2019 code + a && b + ); +} + +function supported2(){ + return !( + // rome-ignore format: Work around https://github.com/rome/tools/issues/3734 + // rome-ignore lint(style/useOptionalChain): Optional chaining creates more complicated ES2019 code + a && b + ); +} + +function supported3(){ + return ( + // rome-ignore format: + aVeryLongLogicalExpression && + thatBreaksOverMultipleLines + ); +} diff --git a/crates/rome_js_formatter/tests/specs/js/module/statement/return_verbatim_argument.js.snap b/crates/rome_js_formatter/tests/specs/js/module/statement/return_verbatim_argument.js.snap new file mode 100644 index 00000000000..a5ca485be34 --- /dev/null +++ b/crates/rome_js_formatter/tests/specs/js/module/statement/return_verbatim_argument.js.snap @@ -0,0 +1,86 @@ +--- +source: crates/rome_js_formatter/tests/spec_test.rs +expression: return_verbatim_argument.js +--- + +# Input + +```js +// https://github.com/rome/tools/issues/3735 + +function supported1(){ + return ( + // rome-ignore format: Work around https://github.com/rome/tools/issues/3734 + // rome-ignore lint(style/useOptionalChain): Optional chaining creates more complicated ES2019 code + a && b + ); +} + +function supported2(){ + return !( + // rome-ignore format: Work around https://github.com/rome/tools/issues/3734 + // rome-ignore lint(style/useOptionalChain): Optional chaining creates more complicated ES2019 code + a && b + ); +} + +function supported3(){ + return ( + // rome-ignore format: + aVeryLongLogicalExpression && + thatBreaksOverMultipleLines + ); +} + +``` + + +============================= + +# Outputs + +## Output 1 + +----- +Indent style: Tab +Line width: 80 +Quote style: Double Quotes +Quote properties: As needed +Trailing comma: All +----- + +```js +// https://github.com/rome/tools/issues/3735 + +function supported1() { + return ( + // rome-ignore format: Work around https://github.com/rome/tools/issues/3734 + // rome-ignore lint(style/useOptionalChain): Optional chaining creates more complicated ES2019 code + a && b + ); +} + +function supported2() { + return !( + // rome-ignore format: Work around https://github.com/rome/tools/issues/3734 + // rome-ignore lint(style/useOptionalChain): Optional chaining creates more complicated ES2019 code + a && b + ); +} + +function supported3() { + return ( + // rome-ignore format: + aVeryLongLogicalExpression && + thatBreaksOverMultipleLines + ); +} + + +## Lines exceeding width of 80 characters + + 6: // rome-ignore lint(style/useOptionalChain): Optional chaining creates more complicated ES2019 code + 14: // rome-ignore lint(style/useOptionalChain): Optional chaining creates more complicated ES2019 code +``` + +