-
Notifications
You must be signed in to change notification settings - Fork 656
Variable declarator formatting #2423
Comments
This would require some understand of Prettier's IR and source code, to understand how/when they decide to break variable declarations. |
Hi, |
Sure thing. I assigned it to you |
Need to check because Rome and Prettier have different IR for these cases: https://gist.github.com/MichaReiser/77799fe4363bcc48838a159e581d8229#jsassignmentunaryjs
|
@ematipico @MichaReiser It has Another conditional is
|
No, Rome doesn't have a similar IR element yet. Adding it should be straightforward. You add a new variant to the You then have to add a
@ematipico is looking into this as well. There are two ways: Ideal: We implement a fn write_element(&mut self, element: FormatElement) -> FormatResult<()> {
self.will_break = self.will_break || element.will_break();
self.inner.write_element(element);
} The fn finish(self) -> bool {
self.will_break
} Other alternatives are to use an ad-hoc inspector (results in a lot of repetition) let mut will_break = false;
{
let mut buffer = f.inspect(|element| {
will_break = will_break || element.will_break();
});
write!(buffer, [element])?;
}
// use `will_break Or write into a let mut buffer = VecBuffer::new();
write!(buffer, [content])?;
let element = buffer.into_element();
let will_break = element.will_break();
f.write_element(element)?; |
Could I try to add |
@MichaReiser
|
There are two ways, one is using let mut is_chain = false;
{
let mut buffer = f.inspect(|element| {
if let FormatElement::Label(label) = element {
is_chain = labelled.label() == "some-chain";
} else {
is_chain = false;
});
write!(buffer, [self.some_element.format()])?;
} This has the advantage that it avoids allocating a new vector and later copying all elements over into the final buffer. Something I wanted to explore but didn't get along yet is to provide a way that
I think this would provide the best ergonomics and has the added benefit that it avoids copying the elements because it only needs to write a single element, the interned element. Let me quickly try if I can write something along these lines |
This is almost completed. The only thing that remains is querying, using |
Yes, there is also one piece. I've implemented it but it didn't change tests.🤷🏻♂️ |
It might be some weird edge case. Usually I do |
@MichaReiser what's your stance here? I believe this issue can be closed. |
I'm OK with closing this for now until we are able to reproduce an example where the labelled member expression is relevant (isPoorlyBreakableMember...) |
Rome has no special formatting for variable declarations but Prettier formats them the same way as assignment expressions.
Examples
A few examples from the latest Prettier comparison report
Expected
Rome's output to match Prettier's
The text was updated successfully, but these errors were encountered: