Skip to content

Commit

Permalink
refactor(parse/css): change fields in CssParserSettings to Option
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 committed Jun 26, 2024
1 parent 7935235 commit ca452cc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
4 changes: 2 additions & 2 deletions crates/biome_css_parser/tests/spec_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
8 changes: 4 additions & 4 deletions crates/biome_service/src/file_handlers/css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,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<bool>,
pub css_modules: Option<bool>,
}

impl ServiceLanguage for CssLanguage {
Expand Down Expand Up @@ -217,10 +217,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 {
Expand Down
53 changes: 31 additions & 22 deletions crates/biome_service/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -494,20 +494,25 @@ impl From<PartialJsonConfiguration> for LanguageSettings<JsonLanguage> {
}
}

impl From<CssConfiguration> for LanguageSettings<CssLanguage> {
fn from(css: CssConfiguration) -> Self {
impl From<PartialCssConfiguration> for LanguageSettings<CssLanguage> {
fn from(css: PartialCssConfiguration) -> Self {
let mut language_setting: LanguageSettings<CssLanguage> = 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 {
language_setting.formatter.enabled = formatter.enabled;
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 {
language_setting.linter.enabled = linter.enabled;
}

language_setting
}
Expand Down Expand Up @@ -1130,8 +1135,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;
Expand Down Expand Up @@ -1365,8 +1374,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
}
Expand Down

0 comments on commit ca452cc

Please sign in to comment.