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
…biomejs#3273)

Co-authored-by: Ze-Zheng Wu <zezhengwu@proton.me>
  • Loading branch information
2 people authored and rishabh3112 committed Jul 6, 2024
1 parent ff354db commit 26ae5c3
Show file tree
Hide file tree
Showing 3 changed files with 39 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 @@ -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<bool>,
pub css_modules: Option<bool>,
}

impl ServiceLanguage for CssLanguage {
Expand Down Expand Up @@ -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 {
Expand Down
55 changes: 33 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,27 @@ 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 {
// 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
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 26ae5c3

Please sign in to comment.