+
Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_js_formatter): implement option quote_properties #3065

Merged
merged 11 commits into from
Aug 18, 2022

Conversation

bhbs
Copy link
Contributor

@bhbs bhbs commented Aug 16, 2022

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

  • Check new snapshots
    • crates/rome_js_formatter/tests/specs/js/module/string/properties_quotes.js.snap
    • crates/rome_js_formatter/tests/specs/ts/string/parameter_quotes.ts.snap

@bhbs
Copy link
Contributor Author

bhbs commented Aug 16, 2022

After this PR is merged, I’d like to implement this option to VSCode Extension and playground!

@bhbs bhbs marked this pull request as ready for review August 16, 2022 10:39
@bhbs bhbs requested a review from a team August 16, 2022 10:39
@ematipico
Copy link
Contributor

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 rome.json. We removed all the beta options. Only the playground needs it

Copy link
Contributor

@MichaReiser MichaReiser left a 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,
Copy link
Contributor

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

Copy link
Contributor Author

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,
Copy link
Contributor

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,
Copy link
Contributor

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)
Copy link
Contributor

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.

Comment on lines 200 to 205
#### `javascript.formatter.preserveQuotes`

Respect the input use of quotes in object properties.

> Default: `false`

Copy link
Contributor

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.

@bhbs
Copy link
Contributor Author

bhbs commented Aug 16, 2022

@MichaReiser @ematipico
Thank you for reviewing!
I addressed your feedback👍

@bhbs bhbs requested review from MichaReiser and ematipico August 16, 2022 16:09
Copy link
Contributor

@ematipico ematipico left a 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

Comment on lines 105 to 117
#[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
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[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!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool 🦀

Copy link
Contributor

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.

Copy link
Contributor Author

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

@MichaReiser MichaReiser requested a review from ematipico August 17, 2022 16:33
@bhbs
Copy link
Contributor Author

bhbs commented Aug 17, 2022

@ematipico
Did you intend like this? a6a89ab

@ematipico
Copy link
Contributor

@ematipico
Did you intend like this? a6a89ab

Yes, that's it :)

It's okay if it's empty, I plan to change it soon. At least it's ready and there

@bhbs bhbs changed the title feat(rome_js_formatter): implement option preserve_quotes feat(rome_js_formatter): implement option quote_properties Aug 18, 2022
Copy link
Contributor

@ematipico ematipico left a 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!

@ematipico ematipico merged commit c6d0450 into rome:main Aug 18, 2022
@bhbs bhbs deleted the feature/preserve-quotes branch August 20, 2022 02:32
IWANABETHATGUY pushed a commit to IWANABETHATGUY/tools that referenced this pull request Aug 22, 2022
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

📎 Implement option preserve_quotes
3 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载