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

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

Merged
merged 3 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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![
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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![
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_js_formatter/src/utils/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
);


Original file line number Diff line number Diff line change
@@ -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
);
```


Original file line number Diff line number Diff line change
@@ -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
);
}
Original file line number Diff line number Diff line change
@@ -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
```