-
Notifications
You must be signed in to change notification settings - Fork 653
feat(rome_js_formatter): implement option quote_properties #3065
Conversation
crates/rome_js_formatter/tests/specs/js/module/assignment/assignment.js.snap
Show resolved
Hide resolved
After this PR is merged, I’d like to implement this option to VSCode Extension and playground! |
The VSCode extension now always reads the configuration from |
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.
Overall looking good to me. My main suggestion is to change the option to an enum so that it can be extended in the future
@@ -21,6 +21,9 @@ pub struct JsFormatContext { | |||
/// The style for quotes. Defaults to double. | |||
quote_style: QuoteStyle, | |||
|
|||
/// Respect the input use of quotes in object properties. | |||
preserve_quotes: bool, |
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 would prefer an enum over a bool so that it becomes possible to support all variants that prettier supports without a breaking change:
- preserve
- as-needed (what Rome does today. Default)
- consistent (out of scope)
I would also rename the setting to quote-properties
to closer match prettier's naming
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.
Thanks for the review. I will change it!
@@ -83,6 +83,7 @@ pub struct JavascriptFormatter { | |||
/// The style for quotes. Defaults to double. | |||
#[serde(with = "PlainQuoteStyle")] | |||
pub quote_style: QuoteStyle, | |||
pub preserve_quotes: bool, |
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 add some documentation, please?
@@ -36,6 +36,7 @@ use std::fmt::Debug; | |||
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] | |||
pub struct JsFormatSettings { | |||
pub quote_style: Option<QuoteStyle>, | |||
pub preserve_quotes: bool, |
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.
This information should be wrapped under a Option
. This will be useful in the future when serializing the configuration. If it's Option::is_none
, it won't be serialized.
@@ -58,6 +58,7 @@ OPTIONS: | |||
--indent-size <number> If the indentation style is set to spaces, determine how many spaces should be used for indentation (default: 2) | |||
--line-width <number> Determine how many characters the formatter is allowed to print in a single line (default: 80) | |||
--quote-style <single|double> Determine whether the formatter should use single or double quotes for strings (default: double) | |||
--preserve-quotes Determine whether the formatter should preserve quotes in object properties (default: false) |
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 revert this change, please? Usually we update the website right before a release, so we don't document stuff that it's unreleased.
#### `javascript.formatter.preserveQuotes` | ||
|
||
Respect the input use of quotes in object properties. | ||
|
||
> Default: `false` | ||
|
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.
Same here, this change should be reverted.
@MichaReiser @ematipico |
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 add a new test inside rome_cli/tests/
where we test an error when passing a wrong value for --quote-properties
? The test should go inside mod formatter
. And we can have a snapshot of it, so we can actually see the error message
#[derive(Deserialize, Serialize, Debug, Eq, PartialEq)] | ||
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] | ||
#[serde(rename_all = "camelCase", remote = "QuoteProperties")] | ||
pub enum PlainQuoteProperties { | ||
AsNeeded, | ||
Preserve, | ||
} | ||
|
||
impl Default for PlainQuoteProperties { | ||
fn default() -> Self { | ||
Self::AsNeeded | ||
} | ||
} |
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.
#[derive(Deserialize, Serialize, Debug, Eq, PartialEq)] | |
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] | |
#[serde(rename_all = "camelCase", remote = "QuoteProperties")] | |
pub enum PlainQuoteProperties { | |
AsNeeded, | |
Preserve, | |
} | |
impl Default for PlainQuoteProperties { | |
fn default() -> Self { | |
Self::AsNeeded | |
} | |
} | |
#[derive(Deserialize, Default, Serialize, Debug, Eq, PartialEq)] | |
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] | |
#[serde(rename_all = "camelCase", remote = "QuoteProperties")] | |
pub enum PlainQuoteProperties { | |
#[default] | |
AsNeeded, | |
Preserve, | |
} |
We can now use macros to assign defaults to enum!
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.
Cool 🦀
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 may be better to call QuoteStyle::default
here to make sure these two are always in sync. Not sure if there's a From
implementation that easily allows you to do so.
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.
Sorry, I'm not sure, please tell me how to do it
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
@ematipico |
Yes, that's it :) It's okay if it's empty, I plan to change it soon. At least it's ready and there |
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.
This is a great contribution! Thank you very much!
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
Summary
This PR implements option
preserve_quotes
quote_properties
following Prettier. (docs)I referred to
--quote-style
for the implementation of the option.Close #2482
Test Plan