-
-
Notifications
You must be signed in to change notification settings - Fork 508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(js_formatter): provide the JS formatter with the ability to format other languages #3403
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
pub mod trailing_commas; | ||
|
||
use crate::comments::{FormatJsLeadingComment, JsCommentStyle, JsComments}; | ||
use crate::JsForeignLanguageFormatter; | ||
use biome_deserialize_macros::{Deserializable, Merge}; | ||
use biome_formatter::printer::PrinterOptions; | ||
use biome_formatter::{ | ||
|
@@ -44,12 +45,19 @@ pub struct JsFormatContext { | |
cached_function_body: Option<(AnyJsFunctionBody, FormatElement)>, | ||
|
||
source_map: Option<TransformSourceMap>, | ||
|
||
foreign_language_formatter: Rc<dyn JsForeignLanguageFormatter>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm just curious why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't think about it deeply and just followed the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I also noticed the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, sorry, you mean Initially, I wanted to use a closure, but I encountered a problem with deriving the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Thank you! I will think about it! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I got it. Indeed, if we need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems Still need to think about it, convert this PR to a draft temporarily There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If we attach a generic type to The rust book mentions that
This comment was marked as off-topic.
Sorry, something went wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It looks like the overhead will be totally acceptable in this case, so I’d just stick with the Generally in Rust, don’t worry too much about performance unless you’re making clones all over the place or something is becoming noticeably slow :) |
||
} | ||
|
||
impl JsFormatContext { | ||
pub fn new(options: JsFormatOptions, comments: JsComments) -> Self { | ||
pub fn new( | ||
options: JsFormatOptions, | ||
foreign_language_formatter: Rc<dyn JsForeignLanguageFormatter>, | ||
comments: JsComments, | ||
) -> Self { | ||
Self { | ||
options, | ||
foreign_language_formatter, | ||
comments: Rc::new(comments), | ||
cached_function_body: None, | ||
source_map: None, | ||
|
@@ -90,6 +98,10 @@ impl JsFormatContext { | |
self.source_map = source_map; | ||
self | ||
} | ||
|
||
pub fn get_foreign_language_formatter(&self) -> &dyn JsForeignLanguageFormatter { | ||
self.foreign_language_formatter.as_ref() | ||
} | ||
} | ||
|
||
#[derive(Eq, PartialEq, Debug, Copy, Clone, Hash)] | ||
|
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'm wondering if it could be a circular dependency problem at some point.
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.
Actually, I dont know. I'm not familiar with
cargo
. Does a dev dependency participate in the build phase? If there is a cycle, willcargo
report it?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.
Cycles aren't allowed, and rust won't compile until it's resolved.
As for dev deps, I believe that's the same because the code is compiled, even for tests.
We have options:
biome_service
The first option is maybe the best one. I would also argue that embedded languages are an esoteric area and they should not be part of the testing of a specific language - in this example, the JavaScript language
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.
Thank you! TIL
Agree
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 more I think about it, the more I am inclined to believe that it may be okay to have CSS dependencies in the dev dependencies for tests? It's simpler and prevents the JS formatter tests from being spread across the project. If we move it to another crate, how will we collect statistics about Prettier compatibility?