-
Notifications
You must be signed in to change notification settings - Fork 659
feat(rome_js_formatter): Parenthesize TypeScript types #3108
Conversation
234c161
to
e6730da
Compare
cd962f2
to
9ef7297
Compare
Deploying with Cloudflare Pages
|
@@ -716,6 +789,86 @@ impl NeedsParentheses for JsAnyAssignmentPattern { | |||
} | |||
} | |||
|
|||
impl NeedsParentheses for TsType { |
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.
These union implementation isn't used anywhere but I think it will be useful to remind everyone adding a new TsType
to also implement NeedsParentheses
@@ -47,11 +47,11 @@ export type a = | |||
- // prettier-ignore |
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.
We can't support these prettier ignore comments at the moment (were ignored before). We'll only be able to support these ignore comments with the new comments infrastructure
@@ -11,60 +11,56 @@ impl FormatRule<JsVariableDeclaratorList> for FormatJsVariableDeclaratorList { | |||
type Context = JsFormatContext; | |||
|
|||
fn fmt(&self, node: &JsVariableDeclaratorList, f: &mut JsFormatter) -> FormatResult<()> { | |||
let format_inner = format_with(|f| { | |||
let length = node.len(); | |||
let length = node.len(); |
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 moved the group outside into the FormatJsVariableDeclaration
to match prettier's formatting
*Conent
APIs to WASM bindings (#3097)9ef7297
to
c7a8c36
Compare
This PR implements the rules for when parentheses around TypeScript types are needed or can be removed without changing the semantics of the program. The majority of the PR is to implement `NeedsParentheses` for every `TsType`. This PR further fixes an instability issue with the syntax rewriter. The rewriter used to remove all whitespace between the `(` paren and the token (or first comment and skipped token trivia). This is necessary or the formatter otherwise inserts an extra blank line before nodes that are parenthesized: ``` a ( Long && Longer && ) ``` becomes ``` a ( Long && Longer && ) ``` Notice, the added new line. What this logic didn't account for is that it should not remove a leading new line before a comment because we want to keep the comment on its own line and this requires that there's a leading new line trivia. The last fix is in the source map that so far assumed that all ranges are added in increasing `end` order ``` correct: 2..3, 2..4, 2..5 (notice how the ranges are sorted by end) incorrect: 2..4, 2..3, 2..5 ``` This PR updates the sorting of the text ranges to also account the end ranges. I added unit tests for all rules where parentheses are required and reviewed the updated snapshots. There are a few new changes but these unrelated to parentheses but instead problems with the formatting of the specific syntax. Average compatibility: 83.70 -> 84.11 Compatible lines: 80.79 -> 81.42
f960d6b
to
8ae1f8e
Compare
!bench_formatter |
Formatter Benchmark Results
|
This PR implements the rules for when parentheses around TypeScript types are needed or can be removed without changing the semantics of the program.
The majority of the PR is to implement
NeedsParentheses
for everyTsType
.Tests
I added unit tests for all rules where parentheses are required and reviewed the updated snapshots.
There are a few new changes but these unrelated to parentheses but instead problems with the formatting of the specific syntax.
Average compatibility: 84.00 -> 84.41
Compatible lines: 81.75 -> 82.38