-
Notifications
You must be signed in to change notification settings - Fork 659
fix(rome_js_formatter): change parenthesized expression formatting #2636
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,25 +16,10 @@ impl FormatNodeFields<JsSequenceExpression> for FormatNodeRule<JsSequenceExpress | |
// Return statement already does the indentation for us | ||
// Arrow function body can't have a sequence expression unless it's parenthesized, otherwise | ||
// would be a syntax error | ||
if matches!(parent.kind(), JsSyntaxKind::JS_RETURN_STATEMENT) { | ||
true | ||
} else if matches!(parent.kind(), JsSyntaxKind::JS_PARENTHESIZED_EXPRESSION) { | ||
// In case we are inside a sequence expression, we have to go up a level and see the great parent. | ||
// Arrow function body and return statements applying indentation for us, so we signal the | ||
// sequence expression to not add other indentation levels | ||
let great_parent = parent.parent().map(|gp| gp.kind()); | ||
|
||
matches!( | ||
great_parent, | ||
Some( | ||
JsSyntaxKind::JS_ARROW_FUNCTION_EXPRESSION | ||
| JsSyntaxKind::JS_RETURN_STATEMENT | ||
| JsSyntaxKind::JS_PROPERTY_OBJECT_MEMBER | ||
) | ||
) | ||
} else { | ||
false | ||
} | ||
matches!( | ||
parent.kind(), | ||
JsSyntaxKind::JS_RETURN_STATEMENT | JsSyntaxKind::JS_PARENTHESIZED_EXPRESSION | ||
) | ||
Comment on lines
-19
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m uncertain about this change, but it seems to increase Prettier alignment in our existing snapshots. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cases like this, but as you're changing the logic of the parenthesis, maybe it's not needed anymore. |
||
}); | ||
|
||
// Find the left most sequence expression | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
--- | ||
source: crates/rome_js_formatter/tests/spec_test.rs | ||
assertion_line: 244 | ||
assertion_line: 257 | ||
expression: sequence_expression.js | ||
--- | ||
# Input | ||
|
@@ -51,15 +51,13 @@ const f = () => ( | |
____________third | ||
); | ||
|
||
( | ||
____________first, | ||
____________second, | ||
____________third, | ||
____________third, | ||
____________third, | ||
____________third, | ||
____________third | ||
); | ||
(____________first, | ||
____________second, | ||
Comment on lines
+54
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wasn't this case handled by the code that you have now removed (and mentioned ema?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case, Prettier would just omit the parentheses. But if you force the parentheses to remain like in your original example, Prettier doesn't block indent the sequence expression. The indentation in your example (copied below) came from the binary expression.
Another demonstration is when you assign a sequence expression. I'll add a test case for that. Before this PR: foo =
(
____________first,
____________second,
____________third,
____________third,
____________third,
____________third,
____________third
); After this PR and also Prettier: foo =
(____________first,
____________second,
____________third,
____________third,
____________third,
____________third,
____________third); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I don't know. Given what we have and what prettier does, do we really need to match it? Doesn't seem that good honestly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I"m always in favor of deleting code :D I don't think it's that terrible, nice and compact. But to phrase this differently. I don't think it's that bad that it's reason enough for us to diverge, especially if it means we can delete code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I expect that this sort of code is uncommon enough in the wild that we don't need to be terribly concerned with how it looks, so I'm also in favor of just simplifying our rules here and matching Prettier. If we ever discover enough of an impact on real-world projects, we can reconsider in the future. |
||
____________third, | ||
____________third, | ||
____________third, | ||
____________third, | ||
____________third); | ||
|
||
function a() { | ||
return ( | ||
|
@@ -74,15 +72,13 @@ function a() { | |
} | ||
|
||
const object = { | ||
something: ( | ||
____________first, | ||
____________second, | ||
____________third, | ||
____________third, | ||
____________third, | ||
____________third, | ||
____________third | ||
), | ||
something: (____________first, | ||
____________second, | ||
____________third, | ||
____________third, | ||
____________third, | ||
____________third, | ||
____________third), | ||
}; | ||
Comment on lines
+75
to
82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn’t really an improvement, but trying to fix it in this PR could conflict with #2627 |
||
|
||
aLongIdentifierName, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: It should be possible to match directly on the node rather than going over syntax