-
Notifications
You must be signed in to change notification settings - Fork 656
feat(rome_js_formatter): JsAnyAssignmentLike node union for JsPropertyObjectMember and JsAssignmentExpression #2698
Conversation
…pertyObjectMember and JsAssignmentExpression
let width = write_member_name(&property.name()?, f)?; | ||
let text_width_for_break = | ||
(f.context().tab_width() + MIN_OVERLAP_FOR_BREAK) as usize; | ||
Ok(width < text_width_for_break) |
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.
Prettier checks short name only for ObjectMember
.
https://github.com/prettier/prettier/blob/2316e2fecd171335fc00f3ec97b7e46cc8964153/src/language-js/print/assignment.js#L413
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.
True, I noticed that too yesterday
If we choose this approach we'll need to add more variants for layout: |
This comment was marked as resolved.
This comment was marked as resolved.
} | ||
|
||
pub(crate) fn is_break_after_operator(right: &JsAnyExpression) -> SyntaxResult<bool> { | ||
if has_leading_newline(right.syntax()) && right.syntax().has_leading_comments() { |
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.
I'm not sure that it works properly, because after format comments can be(???) in another place.
https://github.com/prettier/prettier/blob/2316e2fecd171335fc00f3ec97b7e46cc8964153/src/language-js/print/assignment.js#L126
I think that's fine, as long as we document the data structure and explain what kind of situations it handles. |
# Conflicts: # crates/rome_js_formatter/tests/specs/prettier/js/assignment/lone-arg.js.snap
@ematipico Could you help me please? 🙏🏽 |
Oh, please don't. I would actually close mine. The problem is that I didn't see what issues this PR is closing. Could you please update the description? This is really important to us, it avoids double work. I will help of course! |
I'm sorry for that. I didn't check the issues before opening PR. My bad. 😓
I'm going to copy |
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.
Thank you for the great contribution!
About the next steps, here's what I had in mind. As you explained, prettier uses this way of formatting for many things, and eventually we will need to check the "left" and "right" part of an assignment like.
Which means that "left" and "right" should be grouped inside another unions, because the "left" part of an assignment like might differ based on what we are formatting. My idea was to create a LeftAssignmentLike
:
declare_node_union! {
pub(crate) LeftAssignmentLike = JsAnyAssignmentPattern | JsAnyObjectMemberName
}
If you're OK which this plan, I will work on #2416 and you can choose another one (maybe VariableDeclarator
?). Feel free to comment in the issue and I will assign that to you
crates/rome_js_formatter/src/js/objects/property_object_member.rs
Outdated
Show resolved
Hide resolved
let width = write_member_name(&property.name()?, f)?; | ||
let text_width_for_break = | ||
(f.context().tab_width() + MIN_OVERLAP_FOR_BREAK) as usize; | ||
Ok(width < text_width_for_break) |
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.
True, I noticed that too yesterday
I can definitely see that. And we should add variants while we implement other assignments. I already started implementing |
I noticed that to implement this variant we need to know |
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.
Great! Thank you very much! This contribution is great! We can rebase and merge it :)
# Conflicts: # crates/rome_js_formatter/src/utils/mod.rs # crates/rome_js_formatter/tests/specs/prettier/js/last-argument-expansion/edge_case.js.snap # crates/rome_js_formatter/tests/specs/prettier/js/method-chain/bracket_0.js.snap # crates/rome_js_formatter/tests/specs/prettier/js/new-expression/with-member-expression.js.snap # crates/rome_js_formatter/tests/specs/prettier/js/template-literals/expressions.js.snap # crates/rome_js_formatter/tests/specs/prettier/js/template/graphql.js.snap # crates/rome_js_formatter/tests/specs/prettier/typescript/as/assignment.ts.snap
That's because we assumed that each variant had a group, and we should not. I think the best implementation is: fn write_assignment_like() -> FormatResult<()> {
match layout {}
} And each variant |
Yes, but unfortunately we need to call |
I solved like this: match self {
JsAnyAssignmentLike::JsPropertyObjectMember(_) => {
write!(f, [group_elements(&format_content)])
}
JsAnyAssignmentLike::JsAssignmentExpression(_) => {
write!(f, [&format_content])
}
} At the very end of the function |
Hmm, but there are cases then assignment expression has |
Sorry, I guess I assumed too many things here 😅 I had to move all the code inside
|
@denbezrukov Please check #2728 |
Summary
This PR fixes #2410
This is the initial draft for
JsAnyAssignmentLike
.I'd like to propose
JsAnyAssignmentLike
node union to handle different cases.Prettier uses printAssignment for:
ObjectProperty
https://github.com/prettier/prettier/blob/2316e2fecd171335fc00f3ec97b7e46cc8964153/src/language-js/printer-estree.js#L388
AssignmentExpression
https://github.com/prettier/prettier/blob/2316e2fecd171335fc00f3ec97b7e46cc8964153/src/language-js/print/assignment.js#L79VariableDeclarator
https://github.com/prettier/prettier/blob/2316e2fecd171335fc00f3ec97b7e46cc8964153/src/language-js/print/assignment.js#L90TSTypeAliasDeclaration
https://github.com/prettier/prettier/blob/2316e2fecd171335fc00f3ec97b7e46cc8964153/src/language-js/print/type-annotation.js#L88
ClassAccessorProperty
,TSAbstractPropertyDefinition
https://github.com/prettier/prettier/blob/2316e2fecd171335fc00f3ec97b7e46cc8964153/src/language-js/print/class.js#L234
Open questions
I'm not sure about the name
JsAnyAssignmentLike
because some cases aren't assignment.Test Plan
Add new spec tests cases for
JsAssignmentExpression
.