-
Notifications
You must be signed in to change notification settings - Fork 656
feat(rome_js_formatter): object pattern formatting #2729
Conversation
Deploying with Cloudflare Pages
|
8b68a73
to
d10fcf5
Compare
d10fcf5
to
984337f
Compare
b0d27d0
to
e76dc00
Compare
6cb4386
to
b168b2e
Compare
6ffab94
to
8ef485a
Compare
8ef485a
to
bc1479a
Compare
// 3. we compute the layout | ||
// 4. we write the left node inside the main buffer based on the layout | ||
let mut buffer = VecBuffer::new(f.state_mut()); | ||
let is_left_short = self.write_left(&mut buffer)?; |
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.
You can do
write!(buffer, [format_with(|f| self.write_left(f)])?;
to get a formatter for a buffer
let layout = self.layout(is_left_short)?; | ||
|
||
let formatted_element = buffer.into_element(); | ||
|
||
if layout == AssignmentLikeLayout::BreakLeftHandSide { |
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.
This works fine but has the downside (isn't specific to here, we have this all over the place), that the implementation needs to copy all elements that have been written to the VecBuffer
. I'll spend a few minutes looking into a InternBuffer
that returns an interned element.
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 just copied what we already do in other places :D
bc1479a
to
796332b
Compare
Summary
This PR advances works around "assignments like", by partially implementing #2424
Here's the main works done:
const { b } = a;
andconst a = { b }
using the same function, this is because in their AST they are treated with the same AST nodes. Rome used two different nodes:JsObjectAssignmentPattern
andJsObjectBindingPattern
. In this PR I created a new union calledJsObjectPatternLike
where we apply the same formatting for both patterns;BreakLeftHandSide
andChainTailArrowChain
. The first had required some changes inside the object pattern formatting, which are included in this PR, the second one won't have the desidered changes because of how things are built.WhenTurns out the implementation was easier than expected, and it is now implemented!ChainTailArrowChain
, prettier signals this layout to another function, which reads it and applies another layout. Here's the code that reads the layout. I'd prefer to figure this out later, outside of this PR (fortunately we don't break any existing code)JsAnyAssignmentLike
, calledRightAssignmentLike
. This was needed because the right hand side of an assignment like can be anJsAnyExpression
orJsAnyAssignmentPattern
;BreakLeftHandSide
, because the rest of the checks are done on other types of nodes (type aliases and type parameters), which are not covered in this PR.Test Plan
I created new test cases that should cover the new layouts