-
Notifications
You must be signed in to change notification settings - Fork 656
refactor(formatter): Change binary like expression to format left to right #2640
Conversation
Deploying with
|
Latest commit: |
ea01272
|
Status: | ✅ Deploy successful! |
Preview URL: | https://0189a681.tools-8rn.pages.dev |
a464b8d
to
8ac52ad
Compare
bc3effd
to
5bdaae9
Compare
Parser conformance results on ubuntu-latestjs/262
jsx/babel
symbols/microsoft
💥 Failed to Panic (1):
ts/babel
ts/microsoft
|
!bench_formatter |
Formatter Benchmark Results
|
/// The left or right sub part of a binary expression. | ||
#[derive(Debug)] | ||
enum FlattenedExpression { | ||
/// The right hand sie of a binary expression. Needs to format the parent operator and the right expression |
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.
/// The right hand sie of a binary expression. Needs to format the parent operator and the right expression | |
/// The right hand side of a binary expression. Needs to format the parent operator and the right expression |
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.
Isn't "right expression"
self in this case? If not, I am really missing the point of this struct
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.
The point of this struct is that it represents this
tools/crates/rome_js_formatter/src/utils/binary_like_expression.rs
Lines 50 to 53 in 6e8a3cb
/// some && | |
/// thing && | |
/// elsewhere && | |
/// happy |
Except that our current formatting actually formats this differently: It creates the following groups
some
&& thing
&& elsewhere
&& happy
Not sure if that's intentional but this isn't something I'm changing as part of this PR.
// if none of the previous conditions is met, | ||
// we take take out the first element from the rest of group, then we hard group the "head" | ||
// and we indent the rest of the groups in a new line | ||
write!(f, [groups.next().unwrap()])?; |
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.
Could we add a // SAFETY
comment?
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.
Hmm maybe... That's copied 1:1, let me see if I can figure it out
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 missed this change unfortunately. But here we can safely tell that we always have at least two groups in a binary like expression.
Also, the comments seems to be outdated. We don't format the "head" in this branch.
Summary
Refactors the binary like expression formatting to lazily write the expressions rather than allocating vectors with the sub results.
The basic idea remains the same. The implementation flattens an expression. What's different to before is that this is now a two-stage process:
I think this also improves readability because it makes it clear what concepts exist in this algorithm (left hand side, right hand side, and a sub-group).
Test Plan
cargo test