This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 664
refactor(Formatter): Move Format
, Formatter
, to rome_formatter
#2559
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Deploying with Cloudflare Pages
|
MichaReiser
force-pushed
the
refactor/into-format-element
branch
from
May 9, 2022 13:59
af2b8a7
to
0c81af3
Compare
!bench_formatter |
Formatter Benchmark Results
|
!bench_formatter |
Formatter Benchmark Results
|
MichaReiser
force-pushed
the
refactor/into-format-element
branch
from
May 9, 2022 16:04
5c81163
to
16abd60
Compare
5 tasks
MichaReiser
force-pushed
the
refactor/into-format-element
branch
from
May 10, 2022 11:51
2b38bf3
to
c344075
Compare
MichaReiser
force-pushed
the
refactor/formatted
branch
from
May 10, 2022 11:51
1535d37
to
19f4e01
Compare
MichaReiser
force-pushed
the
refactor/into-format-element
branch
from
May 10, 2022 12:34
637f06b
to
60a18b9
Compare
MichaReiser
force-pushed
the
refactor/into-format-element
branch
from
May 11, 2022 07:54
60a18b9
to
94adc68
Compare
Parser conformance results on ubuntu-latestjs/262
jsx/babel
ts/babel
ts/microsoft
|
leops
approved these changes
May 11, 2022
MichaReiser
commented
May 11, 2022
use rome_formatter::{FormatOwnedWithRule, FormatRefWithRule}; | ||
|
||
use crate::{AsFormat, FormatNodeRule, IntoFormat}; | ||
impl<'a> AsFormat<'a> for rome_js_syntax::JsScript { |
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 tried to have a
trait HasFormatRule: Sized {
type Rule: FormatRule<Self>;
}
trait and then have generic AsFormat
and IntoFormat
implementations. That worked well except that formatting any Option<&HasFormatRule>
didn't work :( So I went forward with just generating that code.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR moves the shared traits
Format
,Formatter
,IntoFormatElement
, and the extension traits torome_formatter
.I introduce two new traits to work around Rust's orphan rule.
AsFormat
: Has a singleformat()
method that returns an object that knows how to formatself
(by reference)IntoFormat
: Has a singleinto_format()
that returns an object that knows how to formatself
(takes ownership)An object that isn't able to implement
Format
itself because of Rust's orphan rule can implementAsFormat
andIntoFormat
.This PR changes the node formatting by making them implement
AsFormat
andIntoFormat
instead of implementingFormat
. The formatting is implemented by aFormat*
new-type that has a single static::format
method. This new type has been necessary to have a way to share the formatting logic betweenAsFormat
andIntoFormat
.Usage
The syntax for formatting a node doesn't differ much from how it was before the refactor:
The benefit of returning a
Format
object byAsFormat
andIntoFormat
is that any extension trait or helper functions continue to work as before:Note: It would be possible to have a trait providing the
format_with
and other helpers but I decided against it for the time being to not have even more indirection.Open Question
It can currently be a bit confusing that
Format
andAsFormat
provide aformat
method. Should we:AsFormat.format
toas_format
? I decided to useformat()
to matchPath.display
.Format.format
tofmt
(similar toDisplay
/Debug
)Test Plan
cargo test
Deleted a
And ran the code gen. Verified that the code compiles correctly