fix(rome_js_formatter): Unnecessary parentheses for return/unary with verbatim argument
MODIFY
crates/rome_js_formatter/src/js/expressions/unary_expression.rs
crates/rome_js_formatter/src/js/expressions/unary_expression.rs
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<JsUnaryExpression> 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![

MODIFY
crates/rome_js_formatter/src/js/statements/return_statement.rs
crates/rome_js_formatter/src/js/statements/return_statement.rs
@@ -110,12 +110,14 @@ pub(super) struct FormatReturnOrThrowArgument<'a>(&'a JsAnyExpression);
impl Format<JsFormatContext> for FormatReturnOrThrowArgument<'_> {
fn fmt(&self, f: &mut Formatter<JsFormatContext>) -> 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![

MODIFY
crates/rome_js_formatter/src/utils/conditional.rs
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> {

ADD
crates/rome_js_formatter/tests/specs/js/module/expression/unary_expression_verbatim_argument.js
+// https://github.com/rome/tools/issues/3735
+
+!(
+ // rome-ignore format: Work around https://github.com/rome/tools/issues/3734
+ a && b
+);
+
+

ADD
crates/rome_js_formatter/tests/specs/js/module/expression/unary_expression_verbatim_argument.js.snap
+---
+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
+);
+```
+
+

ADD
crates/rome_js_formatter/tests/specs/js/module/statement/return_verbatim_argument.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
+ );
+}

ADD
crates/rome_js_formatter/tests/specs/js/module/statement/return_verbatim_argument.js.snap
+---
+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
+```
+
+

fix(rome_js_formatter): Unnecessary parentheses for return/unary with verbatim argument (#3736)