-
Notifications
You must be signed in to change notification settings - Fork 666
refactor(rome_js_formatter): Introduce format_parenthesize
#2718
Conversation
Deploying with Cloudflare Pages
|
3b76367
to
227bad1
Compare
227bad1
to
28d6317
Compare
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.
Would it be possible to write this helper to make it support wrapping empty nodes in parenthesis transparently, instead of having to unwrap on the first and last token everywhere it's used ?
That's a good point. I guess we could do so for |
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.
No particular concerns, but I would like to set on stone one styling of formatting stuff. We are mixing write!
and .fmt()
. I suppose the latter is less to write but I think having "one style" is better, especially for external contributors that see two things and don't know which one to use
is_last_content_inline_comment, | ||
self.style, | ||
) { | ||
space_token().fmt(f)?; |
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: I believe we should set on a unified style across the board. Sometimes we use token("").fmt(f)?
and sometimes we use write!(f, [token("")])?;
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 started using fmt
because it's less to write when formatting a single element and may produce slightly simpler code. But I can see the point how it might be easier for new contributors.
Summary
This PR replaces the usages of
split_trivia
where it is used to move the leading and trailing trivia of a "to be parenthesised" expression withformat_parenthesize
that manually formats the leading / trailing comments as part of the parentheses.Formatting the comments of the content as part of the parentheses requires a way to signal to the formatter that these comments have already been formatted and that it shouldn't format the comments again when formatting the
"string"
token. This is done by storing the "manually" formatted comments inside of theFormatState
and checking for every comment if the comment has already been formatted.The advantage of this approach is that it enables format rules to manually format comments if there's a need to do so without going over
split_trivia
.New Dependencies
This PR adds a dependency on
indexmap
torome_formatter
. The reason to useIndexMap
overHashMap
is that the new map is part of theFormatState
that supports taking and restoring snapshots. Thus, it is important that the data structure offers a cheap mechanism to restore the "manual comments" map to a previous state.IndexMap
(orIndexSet
) offers that because it's possible to truncate theset
and by doing so, remove all elements that have been added since the snapshot was taken. The built-inHashMap
doesn't provide this functionality.indexmap
is only a new dependency forrome_formatter
. It's already used by the parser,lsp
crate and others.Next Steps
Remove the need for
split_trivia
inside ofgroup_elements
.Test Plan
cargo test
. I added a few new tests verifying the new behaviour.