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

feat(rome_js_formatter): object pattern formatting #2729

Merged
merged 11 commits into from
Jun 22, 2022

Conversation

ematipico
Copy link
Contributor

@ematipico ematipico commented Jun 17, 2022

Summary

This PR advances works around "assignments like", by partially implementing #2424

Here's the main works done:

  • prettier formats the object patterns const { b } = a; and const 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 and JsObjectBindingPattern. In this PR I created a new union called JsObjectPatternLike where we apply the same formatting for both patterns;
  • implemented two new layout modes: BreakLeftHandSide and ChainTailArrowChain. 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. When 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) Turns out the implementation was easier than expected, and it is now implemented!
  • created a new union inside JsAnyAssignmentLike, called RightAssignmentLike. This was needed because the right hand side of an assignment like can be an JsAnyExpression or JsAnyAssignmentPattern;
  • This PR doesn't cover all the cases of when deducting when to apply 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

@cloudflare-pages
Copy link

cloudflare-pages bot commented Jun 17, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 796332b
Status: ✅  Deploy successful!
Preview URL: https://952d1605.tools-8rn.pages.dev
Branch Preview URL: https://feature-object-pattern-forma.tools-8rn.pages.dev

View logs

Base automatically changed from feature/assignment-chain to main June 20, 2022 12:36
@ematipico ematipico force-pushed the feature/object-pattern-formatting branch from 8b68a73 to d10fcf5 Compare June 20, 2022 19:39
@ematipico ematipico temporarily deployed to aws June 20, 2022 19:39 Inactive
@github-actions
Copy link

github-actions bot commented Jun 20, 2022

@ematipico ematipico marked this pull request as ready for review June 20, 2022 19:52
@ematipico ematipico force-pushed the feature/object-pattern-formatting branch from d10fcf5 to 984337f Compare June 20, 2022 19:52
@ematipico ematipico temporarily deployed to aws June 20, 2022 19:52 Inactive
@ematipico ematipico temporarily deployed to aws June 20, 2022 20:07 Inactive
@ematipico ematipico temporarily deployed to aws June 21, 2022 13:45 Inactive
@ematipico ematipico temporarily deployed to aws June 21, 2022 16:30 Inactive
@ematipico ematipico force-pushed the feature/object-pattern-formatting branch from b0d27d0 to e76dc00 Compare June 21, 2022 16:31
@ematipico ematipico temporarily deployed to aws June 21, 2022 16:31 Inactive
@ematipico ematipico force-pushed the feature/object-pattern-formatting branch from 6cb4386 to b168b2e Compare June 22, 2022 11:20
@ematipico ematipico temporarily deployed to aws June 22, 2022 11:20 Inactive
@ematipico ematipico temporarily deployed to aws June 22, 2022 13:38 Inactive
@ematipico ematipico force-pushed the feature/object-pattern-formatting branch from 6ffab94 to 8ef485a Compare June 22, 2022 13:39
@ematipico ematipico temporarily deployed to aws June 22, 2022 13:39 Inactive
@ematipico ematipico force-pushed the feature/object-pattern-formatting branch from 8ef485a to bc1479a Compare June 22, 2022 13:40
@ematipico ematipico temporarily deployed to aws June 22, 2022 13:41 Inactive
// 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)?;
Copy link
Contributor

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 {
Copy link
Contributor

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.

Copy link
Contributor Author

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

@ematipico ematipico force-pushed the feature/object-pattern-formatting branch from bc1479a to 796332b Compare June 22, 2022 14:04
@ematipico ematipico temporarily deployed to aws June 22, 2022 14:04 Inactive
@ematipico ematipico merged commit 08633d8 into main Jun 22, 2022
@ematipico ematipico deleted the feature/object-pattern-formatting branch June 22, 2022 14:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants