-
Notifications
You must be signed in to change notification settings - Fork 657
feat(rome_js_formatter): Template formatting #3063
Conversation
Congratulation! We reached 80%!!!! |
crates/rome_js_formatter/src/js/expressions/arrow_function_expression.rs
Show resolved
Hide resolved
crates/rome_js_formatter/src/js/expressions/template_chunk_element.rs
Outdated
Show resolved
Hide resolved
crates/rome_js_formatter/src/js/expressions/template_element.rs
Outdated
Show resolved
Hide resolved
crates/rome_js_formatter/src/js/expressions/template_element.rs
Outdated
Show resolved
Hide resolved
for _ in 1..level { | ||
indented = FormatElement::Indent(vec![indented].into_boxed_slice()); | ||
} |
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.
What's happening here?
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.
It nests indents for as long until it reaches the desired indent level, as explained in the comment at the top of the format width
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.
And we start from 1
because we checked 0
before? If level
is 1
the indentation is now 2
?
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 changed it to go from 0..level
, and remove the indent
around the initial element. That's probably easier to understand
This PR improves Rome's formatting of `JsTemplate`s and `TsTemplate`s to closer match Prettier's formatting. It mainly implements: * simple expressions that never break even if the template literal, as a result thereof, exceeds the line width * Aligning expressions in template literals with the last template chunk This PR does not implement Prettier's custom formatting of `Jest` specs, and it doesn't implement custom comments formatting. ## Tests I manually verified the snapshot changes. There are some remaining differences but they are rooted in the fact that some, expression formatting isn't compatible with prettier yet (mainly binary expression, call arguments)
f0cd774
to
8c57ad3
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.
Could you please add/update some example that reflects this new layout approach? By updating I mean just adding some comment saying the expected layout
crates/rome_js_formatter/src/js/expressions/template_element.rs
Outdated
Show resolved
Hide resolved
for _ in 1..level { | ||
indented = FormatElement::Indent(vec![indented].into_boxed_slice()); | ||
} |
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.
And we start from 1
because we checked 0
before? If level
is 1
the indentation is now 2
?
@@ -18,15 +18,15 @@ output | |||
`; | |||
|
|||
// don't break | |||
const bar =`but where will ${this.fanta} wrap ${baz} ${"hello"} template literal? ${bar.ff.sss} long long long long ${foo.[3]} long long long long long long`; | |||
const bar =`but where will ${this.fanta} wrap ${baz} ${"hello"} template literal? ${bar.ff.sss} long long long long ${foo[3]} long long long long long long`; |
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.
Why did you change the source of this line?
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.
Because it wasn't valid Javascript ${foo.[3]}
is not valid
@@ -7,7 +7,7 @@ repository = "https://github.com/rome/tools" | |||
license = "MIT" | |||
|
|||
[package.metadata.wasm-pack.profile.dev] | |||
# wasm-opt = ['-O1'] | |||
wasm-opt = ['-O1'] |
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.
Is this change intentional?
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.
It was unintentionall that I commited this commented out :D
This is needed because our playground stack overflows, even for very small samples if we don't perform at least some wasm optimisations.
9461237
to
ace8428
Compare
This PR improves Rome's formatting of
JsTemplate
s andTsTemplate
s to closer match Prettier's formatting.It mainly implements:
This PR does not implement Prettier's custom formatting of
Jest
specs, and it doesn't implement custom comments formatting.Closes #2446
Tests
I manually verified the snapshot changes. There are some remaining differences but they are rooted in the fact that some, expression formatting isn't compatible with prettier yet (mainly binary expression, call arguments), or that prettier supports formatting HTML, graphql, etc.
File Based Average Prettier Similarity: 79.75% -> 80.03%
Line Based Average Prettier Similarity: 76.52% -> 76.85%