From 0834d356b7c58f00428d4cdc70bd1651bfe974b2 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Tue, 5 Jul 2022 14:49:05 +0100 Subject: [PATCH 1/6] feat(rome_service): configuration --- Cargo.lock | 14 +++-- crates/rome_cli/tests/main.rs | 19 ++++++ crates/rome_formatter/src/lib.rs | 2 +- crates/rome_service/Cargo.toml | 3 +- .../src/configuration/formatter.rs | 63 +++++++++++++++++++ .../src/configuration/javascript.rs | 36 +++++++++++ crates/rome_service/src/configuration/mod.rs | 22 +++++++ crates/rome_service/src/lib.rs | 1 + crates/rome_service/tests/all_fields.json | 14 +++++ crates/rome_service/tests/empty.json | 1 + crates/rome_service/tests/mod.rs | 37 +++++++++++ 11 files changed, 204 insertions(+), 8 deletions(-) create mode 100644 crates/rome_service/src/configuration/formatter.rs create mode 100644 crates/rome_service/src/configuration/javascript.rs create mode 100644 crates/rome_service/src/configuration/mod.rs create mode 100644 crates/rome_service/tests/all_fields.json create mode 100644 crates/rome_service/tests/empty.json create mode 100644 crates/rome_service/tests/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 2edb74f2d06..85a8478be6f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1601,6 +1601,8 @@ dependencies = [ "rome_js_parser", "rome_js_syntax", "rome_rowan", + "serde", + "serde_json", ] [[package]] @@ -1691,9 +1693,9 @@ checksum = "568a8e6258aa33c13358f81fd834adb854c6f7c9468520910a9b1e8fac068012" [[package]] name = "serde" -version = "1.0.136" +version = "1.0.138" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" +checksum = "1578c6245786b9d168c5447eeacfb96856573ca56c9d68fdcf394be134882a47" dependencies = [ "serde_derive", ] @@ -1710,9 +1712,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.136" +version = "1.0.138" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" +checksum = "023e9b1467aef8a10fb88f25611870ada9800ef7e22afce356bb0d2387b6f27c" dependencies = [ "proc-macro2", "quote", @@ -1830,9 +1832,9 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.86" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" +checksum = "a07e33e919ebcd69113d5be0e4d70c5707004ff45188910106854f38b960df4a" dependencies = [ "proc-macro2", "quote", diff --git a/crates/rome_cli/tests/main.rs b/crates/rome_cli/tests/main.rs index ef69b8784e7..71e7f1ddf8d 100644 --- a/crates/rome_cli/tests/main.rs +++ b/crates/rome_cli/tests/main.rs @@ -29,6 +29,14 @@ var h; var i; "; +const CONFIG: &str = r#" +{ + "formatter": { + "enabled": false + } +} +"#; + mod check { use super::*; @@ -568,3 +576,14 @@ mod main { } } } + +mod configuration { + use std::env::current_dir; + + #[test] + fn test() { + let path = current_dir().unwrap(); + + println!("{}", path.display()) + } +} diff --git a/crates/rome_formatter/src/lib.rs b/crates/rome_formatter/src/lib.rs index d8f111ccbc8..1e18d7d0052 100644 --- a/crates/rome_formatter/src/lib.rs +++ b/crates/rome_formatter/src/lib.rs @@ -113,7 +113,7 @@ impl std::fmt::Display for IndentStyle { /// /// The allowed range of values is 1..=320 #[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub struct LineWidth(u16); +pub struct LineWidth(pub u16); impl LineWidth { /// Maximum allowed value for a valid [LineWidth] diff --git a/crates/rome_service/Cargo.toml b/crates/rome_service/Cargo.toml index 5eefdc5dac2..243ff96e23c 100644 --- a/crates/rome_service/Cargo.toml +++ b/crates/rome_service/Cargo.toml @@ -7,7 +7,8 @@ edition = "2021" [dependencies] dashmap = "5.2.0" - +serde = { version = "1.0.133", features = ["derive"] } +serde_json = "1.0.74" rome_analyze = { path = "../rome_analyze" } rome_console = { path = "../rome_console" } rome_diagnostics = { path = "../rome_diagnostics" } diff --git a/crates/rome_service/src/configuration/formatter.rs b/crates/rome_service/src/configuration/formatter.rs new file mode 100644 index 00000000000..66697e45b89 --- /dev/null +++ b/crates/rome_service/src/configuration/formatter.rs @@ -0,0 +1,63 @@ +use rome_formatter::{IndentStyle, LineWidth}; +use serde::Deserialize; +#[derive(Deserialize, Debug, Eq, PartialEq)] +#[serde(rename_all = "camelCase", default)] +pub struct FormatterConfiguration { + // if `false`, it disables the feature. `true` by default + pub enabled: bool, + + /// Stores whether formatting should be allowed to proceed if a given file + /// has syntax errors + pub format_with_errors: bool, + + /// The indent style. + pub indent_style: PlainIndentStyle, + + /// The size of the indentation, 2 by default + indent_size: u8, + + /// What's the max width of a line. Defaults to 80. + pub line_width: u16, +} + +impl From<&FormatterConfiguration> for IndentStyle { + fn from(c: &FormatterConfiguration) -> Self { + match c.indent_style { + PlainIndentStyle::Tab => IndentStyle::Tab, + PlainIndentStyle::Space => IndentStyle::Space(c.indent_size), + } + } +} + +impl From<&FormatterConfiguration> for LineWidth { + fn from(c: &FormatterConfiguration) -> Self { + LineWidth(c.line_width) + } +} + +impl Default for FormatterConfiguration { + fn default() -> Self { + Self { + enabled: true, + format_with_errors: false, + indent_size: 2, + indent_style: PlainIndentStyle::default(), + line_width: 80, + } + } +} + +#[derive(Deserialize, Debug, Eq, PartialEq)] +#[serde(rename_all = "camelCase")] +pub enum PlainIndentStyle { + /// Tab + Tab, + /// Space + Space, +} + +impl Default for PlainIndentStyle { + fn default() -> Self { + Self::Tab + } +} diff --git a/crates/rome_service/src/configuration/javascript.rs b/crates/rome_service/src/configuration/javascript.rs new file mode 100644 index 00000000000..e654103d130 --- /dev/null +++ b/crates/rome_service/src/configuration/javascript.rs @@ -0,0 +1,36 @@ +use rome_js_formatter::context::QuoteStyle; +use serde::Deserialize; + +#[derive(Default, Debug, Deserialize, Eq, PartialEq)] +pub struct JavascriptConfiguration { + pub formatter: JavascriptFormatter, +} + +#[derive(Default, Debug, Deserialize, Eq, PartialEq)] +#[serde(rename_all = "camelCase", default)] +pub struct JavascriptFormatter { + /// The style for quotes. Defaults to double. + pub quote_style: PlainQuoteStyle, +} + +#[derive(Deserialize, Debug, Eq, PartialEq)] +#[serde(rename_all = "camelCase")] +pub enum PlainQuoteStyle { + Double, + Single, +} + +impl Default for PlainQuoteStyle { + fn default() -> Self { + Self::Double + } +} + +impl From for QuoteStyle { + fn from(j: JavascriptConfiguration) -> Self { + match j.formatter.quote_style { + PlainQuoteStyle::Double => QuoteStyle::Double, + PlainQuoteStyle::Single => QuoteStyle::Single, + } + } +} diff --git a/crates/rome_service/src/configuration/mod.rs b/crates/rome_service/src/configuration/mod.rs new file mode 100644 index 00000000000..8bb50b5bf7a --- /dev/null +++ b/crates/rome_service/src/configuration/mod.rs @@ -0,0 +1,22 @@ +//! This module contains the configuration of `rome.json` +//! +//! The configuration is divided by "tool", and then it's possible to further customise it +//! by language. The language might further options divided by tool. + +use crate::configuration::formatter::FormatterConfiguration; +use crate::configuration::javascript::JavascriptConfiguration; +use serde::Deserialize; + +mod formatter; +mod javascript; + +/// The configuration that is contained inside the file `rome.json` +#[derive(Default, Debug, Eq, PartialEq, Deserialize)] +#[serde(default)] +pub struct Configuration { + /// The configuration of the formatter + pub formatter: FormatterConfiguration, + + /// Specific configuration for the JavaScript language + pub javascript: JavascriptConfiguration, +} diff --git a/crates/rome_service/src/lib.rs b/crates/rome_service/src/lib.rs index 7a90ab0f358..a5a73850d11 100644 --- a/crates/rome_service/src/lib.rs +++ b/crates/rome_service/src/lib.rs @@ -5,6 +5,7 @@ use std::error::Error; use std::fmt::{Debug, Display, Formatter}; use std::ops::{Deref, DerefMut}; +pub mod configuration; mod file_handlers; pub mod settings; pub mod workspace; diff --git a/crates/rome_service/tests/all_fields.json b/crates/rome_service/tests/all_fields.json new file mode 100644 index 00000000000..7b3c46c0fbb --- /dev/null +++ b/crates/rome_service/tests/all_fields.json @@ -0,0 +1,14 @@ +{ + "formatter": { + "enabled": true, + "formatWithErrors": true, + "indentStyle": "tab", + "indentSize": 2, + "lineWidth": 80 + }, + "javascript": { + "formatter": { + "quoteStyle": "double" + } + } +} \ No newline at end of file diff --git a/crates/rome_service/tests/empty.json b/crates/rome_service/tests/empty.json new file mode 100644 index 00000000000..9e26dfeeb6e --- /dev/null +++ b/crates/rome_service/tests/empty.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/crates/rome_service/tests/mod.rs b/crates/rome_service/tests/mod.rs new file mode 100644 index 00000000000..186eb189319 --- /dev/null +++ b/crates/rome_service/tests/mod.rs @@ -0,0 +1,37 @@ +mod configuration { + use rome_service::configuration::Configuration; + use std::env::current_dir; + use std::fs::read_to_string; + + #[test] + fn parse_all_fields() { + let mut working_dir = current_dir().unwrap(); + working_dir.push("tests"); + working_dir.push("all_fields.json"); + let content = read_to_string(working_dir).unwrap(); + + let configuration = serde_json::from_str::(&content); + + assert!(configuration.is_ok()) + } + + #[test] + fn parse_default_values() { + let mut working_dir = current_dir().unwrap(); + working_dir.push("tests"); + working_dir.push("empty.json"); + let content = read_to_string(working_dir).unwrap(); + + let configuration = serde_json::from_str::(&content); + + match configuration { + Ok(configuration) => { + assert_eq!(configuration, Configuration::default()); + } + Err(err) => { + println!("{err}"); + assert!(false); + } + } + } +} From 882f8f1eab4cea039411399dd818cc91cee55f66 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Tue, 5 Jul 2022 15:55:22 +0100 Subject: [PATCH 2/6] chore: remove not needed changes --- crates/rome_cli/tests/main.rs | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/crates/rome_cli/tests/main.rs b/crates/rome_cli/tests/main.rs index 71e7f1ddf8d..ef69b8784e7 100644 --- a/crates/rome_cli/tests/main.rs +++ b/crates/rome_cli/tests/main.rs @@ -29,14 +29,6 @@ var h; var i; "; -const CONFIG: &str = r#" -{ - "formatter": { - "enabled": false - } -} -"#; - mod check { use super::*; @@ -576,14 +568,3 @@ mod main { } } } - -mod configuration { - use std::env::current_dir; - - #[test] - fn test() { - let path = current_dir().unwrap(); - - println!("{}", path.display()) - } -} From 79d70cbcd8729d20504614ee10cbf528d57dc939 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Tue, 5 Jul 2022 16:04:05 +0100 Subject: [PATCH 3/6] chore: add `root` field --- crates/rome_service/src/configuration/mod.rs | 5 +++++ crates/rome_service/tests/all_fields.json | 1 + 2 files changed, 6 insertions(+) diff --git a/crates/rome_service/src/configuration/mod.rs b/crates/rome_service/src/configuration/mod.rs index 8bb50b5bf7a..781c5c0a0d9 100644 --- a/crates/rome_service/src/configuration/mod.rs +++ b/crates/rome_service/src/configuration/mod.rs @@ -14,6 +14,11 @@ mod javascript; #[derive(Default, Debug, Eq, PartialEq, Deserialize)] #[serde(default)] pub struct Configuration { + /// One root file should exist. Useful when `extends` comes into play. + /// + /// If `true`, this file should be the master configuration. + pub root: bool, + /// The configuration of the formatter pub formatter: FormatterConfiguration, diff --git a/crates/rome_service/tests/all_fields.json b/crates/rome_service/tests/all_fields.json index 7b3c46c0fbb..dd74978261b 100644 --- a/crates/rome_service/tests/all_fields.json +++ b/crates/rome_service/tests/all_fields.json @@ -1,4 +1,5 @@ { + "root": true, "formatter": { "enabled": true, "formatWithErrors": true, From 18bc4616965723dc0fb32bbb8b6e231c860d69c3 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Tue, 5 Jul 2022 16:30:35 +0100 Subject: [PATCH 4/6] chore: clippy --- crates/rome_service/tests/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/rome_service/tests/mod.rs b/crates/rome_service/tests/mod.rs index 186eb189319..f8920ee1f59 100644 --- a/crates/rome_service/tests/mod.rs +++ b/crates/rome_service/tests/mod.rs @@ -29,8 +29,7 @@ mod configuration { assert_eq!(configuration, Configuration::default()); } Err(err) => { - println!("{err}"); - assert!(false); + panic!("{err}"); } } } From 4180084a40f79758ec4fad84d388c3028c559bae Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Tue, 5 Jul 2022 17:45:10 +0100 Subject: [PATCH 5/6] chore: code review --- crates/rome_formatter/src/lib.rs | 2 +- crates/rome_service/src/configuration/formatter.rs | 11 ++++++----- crates/rome_service/tests/mod.rs | 12 ++++++++++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/crates/rome_formatter/src/lib.rs b/crates/rome_formatter/src/lib.rs index 1e18d7d0052..d8f111ccbc8 100644 --- a/crates/rome_formatter/src/lib.rs +++ b/crates/rome_formatter/src/lib.rs @@ -113,7 +113,7 @@ impl std::fmt::Display for IndentStyle { /// /// The allowed range of values is 1..=320 #[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub struct LineWidth(pub u16); +pub struct LineWidth(u16); impl LineWidth { /// Maximum allowed value for a valid [LineWidth] diff --git a/crates/rome_service/src/configuration/formatter.rs b/crates/rome_service/src/configuration/formatter.rs index 66697e45b89..51015ea8701 100644 --- a/crates/rome_service/src/configuration/formatter.rs +++ b/crates/rome_service/src/configuration/formatter.rs @@ -1,4 +1,4 @@ -use rome_formatter::{IndentStyle, LineWidth}; +use rome_formatter::{IndentStyle, LineWidth, LineWidthFromIntError}; use serde::Deserialize; #[derive(Deserialize, Debug, Eq, PartialEq)] #[serde(rename_all = "camelCase", default)] @@ -29,9 +29,10 @@ impl From<&FormatterConfiguration> for IndentStyle { } } -impl From<&FormatterConfiguration> for LineWidth { - fn from(c: &FormatterConfiguration) -> Self { - LineWidth(c.line_width) +impl TryFrom<&FormatterConfiguration> for LineWidth { + type Error = LineWidthFromIntError; + fn try_from(value: &FormatterConfiguration) -> Result { + LineWidth::try_from(value.line_width) } } @@ -42,7 +43,7 @@ impl Default for FormatterConfiguration { format_with_errors: false, indent_size: 2, indent_style: PlainIndentStyle::default(), - line_width: 80, + line_width: LineWidth::default().value(), } } } diff --git a/crates/rome_service/tests/mod.rs b/crates/rome_service/tests/mod.rs index f8920ee1f59..f43380f8cba 100644 --- a/crates/rome_service/tests/mod.rs +++ b/crates/rome_service/tests/mod.rs @@ -33,4 +33,16 @@ mod configuration { } } } + + #[test] + fn default_value() { + let mut working_dir = current_dir().unwrap(); + working_dir.push("tests"); + working_dir.push("all_fields.json"); + let content = read_to_string(working_dir).unwrap(); + + let configuration = serde_json::from_str::(&content); + + assert!(configuration.is_ok()) + } } From e2daaaa8cc28949143ce2a05c50a4cfc9e425a92 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Wed, 6 Jul 2022 08:28:48 +0100 Subject: [PATCH 6/6] chore: better error handling --- crates/rome_formatter/src/lib.rs | 11 +++++++++++ .../src/configuration/formatter.rs | 18 ++++++++++-------- crates/rome_service/tests/line_width.json | 6 ++++++ crates/rome_service/tests/mod.rs | 13 ++++++++++--- 4 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 crates/rome_service/tests/line_width.json diff --git a/crates/rome_formatter/src/lib.rs b/crates/rome_formatter/src/lib.rs index d8f111ccbc8..3a690202551 100644 --- a/crates/rome_formatter/src/lib.rs +++ b/crates/rome_formatter/src/lib.rs @@ -66,6 +66,7 @@ use rome_rowan::{ }; use std::error::Error; use std::fmt; +use std::fmt::Display; use std::num::ParseIntError; use std::str::FromStr; @@ -172,6 +173,16 @@ impl TryFrom for LineWidth { } } +impl Display for LineWidthFromIntError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + writeln!( + f, + "The line width exceeds the maximum value ({})", + LineWidth::MAX + ) + } +} + impl From for u16 { fn from(value: LineWidth) -> Self { value.0 diff --git a/crates/rome_service/src/configuration/formatter.rs b/crates/rome_service/src/configuration/formatter.rs index 51015ea8701..547557a05a4 100644 --- a/crates/rome_service/src/configuration/formatter.rs +++ b/crates/rome_service/src/configuration/formatter.rs @@ -1,4 +1,4 @@ -use rome_formatter::{IndentStyle, LineWidth, LineWidthFromIntError}; +use rome_formatter::{IndentStyle, LineWidth}; use serde::Deserialize; #[derive(Deserialize, Debug, Eq, PartialEq)] #[serde(rename_all = "camelCase", default)] @@ -17,7 +17,8 @@ pub struct FormatterConfiguration { indent_size: u8, /// What's the max width of a line. Defaults to 80. - pub line_width: u16, + #[serde(deserialize_with = "deserialize_line_width")] + pub line_width: LineWidth, } impl From<&FormatterConfiguration> for IndentStyle { @@ -29,11 +30,12 @@ impl From<&FormatterConfiguration> for IndentStyle { } } -impl TryFrom<&FormatterConfiguration> for LineWidth { - type Error = LineWidthFromIntError; - fn try_from(value: &FormatterConfiguration) -> Result { - LineWidth::try_from(value.line_width) - } +fn deserialize_line_width<'de, D>(deserializer: D) -> Result +where + D: serde::de::Deserializer<'de>, +{ + let value: u16 = Deserialize::deserialize(deserializer)?; + LineWidth::try_from(value).map_err(serde::de::Error::custom) } impl Default for FormatterConfiguration { @@ -43,7 +45,7 @@ impl Default for FormatterConfiguration { format_with_errors: false, indent_size: 2, indent_style: PlainIndentStyle::default(), - line_width: LineWidth::default().value(), + line_width: LineWidth::default(), } } } diff --git a/crates/rome_service/tests/line_width.json b/crates/rome_service/tests/line_width.json new file mode 100644 index 00000000000..b3d010b5d4b --- /dev/null +++ b/crates/rome_service/tests/line_width.json @@ -0,0 +1,6 @@ +{ + "root": true, + "formatter": { + "lineWidth": 450 + } +} \ No newline at end of file diff --git a/crates/rome_service/tests/mod.rs b/crates/rome_service/tests/mod.rs index f43380f8cba..2288f44cdcf 100644 --- a/crates/rome_service/tests/mod.rs +++ b/crates/rome_service/tests/mod.rs @@ -35,14 +35,21 @@ mod configuration { } #[test] - fn default_value() { + fn line_width_error() { let mut working_dir = current_dir().unwrap(); working_dir.push("tests"); - working_dir.push("all_fields.json"); + working_dir.push("line_width.json"); let content = read_to_string(working_dir).unwrap(); let configuration = serde_json::from_str::(&content); - assert!(configuration.is_ok()) + assert!(configuration.is_err()); + + if let Err(err) = configuration { + assert!(err + .to_string() + .as_str() + .contains("The line width exceeds the maximum value (320)"),) + } } }