From 91eca899c1adf5eb49b6a1ad7dca097c3d4bfde8 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Wed, 26 Jun 2024 23:46:54 -0400 Subject: [PATCH] refactor(parse/css): change fields in `CssParserSettings` to `Option` (#3273) Co-authored-by: Ze-Zheng Wu --- crates/biome_css_parser/tests/spec_test.rs | 4 +- crates/biome_service/src/file_handlers/css.rs | 8 +-- crates/biome_service/src/settings.rs | 55 +++++++++++-------- 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/crates/biome_css_parser/tests/spec_test.rs b/crates/biome_css_parser/tests/spec_test.rs index 65c60978beeb..e3864eb5ee8c 100644 --- a/crates/biome_css_parser/tests/spec_test.rs +++ b/crates/biome_css_parser/tests/spec_test.rs @@ -60,11 +60,11 @@ pub fn run(test_case: &str, _snapshot_name: &str, test_directory: &str, outcome_ let settings = settings.languages.css.parser; - if settings.css_modules { + if settings.css_modules.unwrap_or_default() { options = options.allow_css_modules(); } - if settings.allow_wrong_line_comments { + if settings.allow_wrong_line_comments.unwrap_or_default() { options = options.allow_wrong_line_comments(); } diff --git a/crates/biome_service/src/file_handlers/css.rs b/crates/biome_service/src/file_handlers/css.rs index a10030bdfefe..8c97c0bb76f4 100644 --- a/crates/biome_service/src/file_handlers/css.rs +++ b/crates/biome_service/src/file_handlers/css.rs @@ -79,8 +79,8 @@ impl Default for CssLinterSettings { #[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)] #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct CssParserSettings { - pub allow_wrong_line_comments: bool, - pub css_modules: bool, + pub allow_wrong_line_comments: Option, + pub css_modules: Option, } impl ServiceLanguage for CssLanguage { @@ -215,10 +215,10 @@ fn parse( ) -> ParseResult { let mut options = CssParserOptions { allow_wrong_line_comments: settings - .map(|s| s.languages.css.parser.allow_wrong_line_comments) + .and_then(|s| s.languages.css.parser.allow_wrong_line_comments) .unwrap_or_default(), css_modules: settings - .map(|s| s.languages.css.parser.css_modules) + .and_then(|s| s.languages.css.parser.css_modules) .unwrap_or_default(), }; if let Some(settings) = settings { diff --git a/crates/biome_service/src/settings.rs b/crates/biome_service/src/settings.rs index e8ffdb4afecf..dcd3176aec83 100644 --- a/crates/biome_service/src/settings.rs +++ b/crates/biome_service/src/settings.rs @@ -5,11 +5,11 @@ use biome_configuration::diagnostics::InvalidIgnorePattern; use biome_configuration::javascript::JsxRuntime; use biome_configuration::organize_imports::OrganizeImports; use biome_configuration::{ - push_to_analyzer_rules, BiomeDiagnostic, CssConfiguration, FilesConfiguration, - FormatterConfiguration, JavascriptConfiguration, LinterConfiguration, - OverrideFormatterConfiguration, OverrideLinterConfiguration, - OverrideOrganizeImportsConfiguration, Overrides, PartialConfiguration, PartialCssConfiguration, - PartialJavascriptConfiguration, PartialJsonConfiguration, PlainIndentStyle, Rules, + push_to_analyzer_rules, BiomeDiagnostic, FilesConfiguration, FormatterConfiguration, + JavascriptConfiguration, LinterConfiguration, OverrideFormatterConfiguration, + OverrideLinterConfiguration, OverrideOrganizeImportsConfiguration, Overrides, + PartialConfiguration, PartialCssConfiguration, PartialJavascriptConfiguration, + PartialJsonConfiguration, PlainIndentStyle, Rules, }; use biome_css_formatter::context::CssFormatOptions; use biome_css_parser::CssParserOptions; @@ -216,7 +216,7 @@ impl Settings { } // css settings if let Some(css) = configuration.css { - self.languages.css = CssConfiguration::from(css).into(); + self.languages.css = css.into(); } // NOTE: keep this last. Computing the overrides require reading the settings computed by the parent settings. @@ -494,20 +494,27 @@ impl From for LanguageSettings { } } -impl From for LanguageSettings { - fn from(css: CssConfiguration) -> Self { +impl From for LanguageSettings { + fn from(css: PartialCssConfiguration) -> Self { let mut language_setting: LanguageSettings = LanguageSettings::default(); - language_setting.parser.allow_wrong_line_comments = css.parser.allow_wrong_line_comments; - language_setting.parser.css_modules = css.parser.css_modules; - - language_setting.formatter.enabled = Some(css.formatter.enabled); - language_setting.formatter.indent_width = css.formatter.indent_width; - language_setting.formatter.indent_style = css.formatter.indent_style.map(Into::into); - language_setting.formatter.line_width = css.formatter.line_width; - language_setting.formatter.line_ending = css.formatter.line_ending; - language_setting.formatter.quote_style = Some(css.formatter.quote_style); - language_setting.linter.enabled = Some(css.linter.enabled); + if let Some(parser) = css.parser { + language_setting.parser.allow_wrong_line_comments = parser.allow_wrong_line_comments; + language_setting.parser.css_modules = parser.css_modules; + } + if let Some(formatter) = css.formatter { + // TODO: change RHS to `formatter.enabled` when css formatting is enabled by default + language_setting.formatter.enabled = Some(formatter.enabled.unwrap_or_default()); + language_setting.formatter.indent_width = formatter.indent_width; + language_setting.formatter.indent_style = formatter.indent_style.map(Into::into); + language_setting.formatter.line_width = formatter.line_width; + language_setting.formatter.line_ending = formatter.line_ending; + language_setting.formatter.quote_style = formatter.quote_style; + } + if let Some(linter) = css.linter { + // TODO: change RHS to `linter.enabled` when css linting is enabled by default + language_setting.linter.enabled = Some(linter.enabled.unwrap_or_default()); + } language_setting } @@ -1130,8 +1137,12 @@ impl OverrideSettingPattern { let css_parser = &self.languages.css.parser; - options.allow_wrong_line_comments = css_parser.allow_wrong_line_comments; - options.css_modules = css_parser.css_modules; + if let Some(allow_wrong_line_comments) = css_parser.allow_wrong_line_comments { + options.allow_wrong_line_comments = allow_wrong_line_comments; + } + if let Some(css_modules) = css_parser.css_modules { + options.css_modules = css_modules; + } if let Ok(mut writeonly_cache) = self.cached_css_parser_options.write() { let options = *options; @@ -1365,8 +1376,8 @@ fn to_css_language_settings( let parent_parser = &parent_settings.parser; language_setting.parser.allow_wrong_line_comments = parser .allow_wrong_line_comments - .unwrap_or(parent_parser.allow_wrong_line_comments); - language_setting.parser.css_modules = parser.css_modules.unwrap_or(parent_parser.css_modules); + .or(parent_parser.allow_wrong_line_comments); + language_setting.parser.css_modules = parser.css_modules.or(parent_parser.css_modules); language_setting }