Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(parse/json): change fields in JsonParserSettings to Option #3272

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/biome_json_parser/tests/spec_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ pub fn run(test_case: &str, _snapshot_name: &str, test_directory: &str, outcome_
let content = fs::read_to_string(test_case_path)
.expect("Expected test path to be a readable file in UTF8 encoding");

let parse_conifg = JsonParserOptions {
let parse_config = JsonParserOptions {
allow_comments: test_directory.contains("allow_comments"),
allow_trailing_commas: test_directory.contains("allow_trailing_commas"),
};
let parsed = parse_json(&content, parse_conifg);
let parsed = parse_json(&content, parse_config);
let formatted_ast = format!("{:#?}", parsed.tree());

let mut snapshot = String::new();
Expand Down
12 changes: 8 additions & 4 deletions crates/biome_service/src/file_handlers/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ pub struct JsonFormatterSettings {
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct JsonParserSettings {
pub allow_comments: bool,
pub allow_trailing_commas: bool,
pub allow_comments: Option<bool>,
pub allow_trailing_commas: Option<bool>,
}

#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
Expand Down Expand Up @@ -179,10 +179,14 @@ fn parse(
let overrides = settings.map(|s| &s.override_settings);
let optional_json_file_source = file_source.to_json_file_source();
let options = JsonParserOptions {
allow_comments: parser.map(|p| p.allow_comments).unwrap_or_default()
allow_comments: parser.and_then(|p| p.allow_comments).map_or_else(
|| optional_json_file_source.map_or(false, |x| x.allow_comments()),
allow_trailing_commas: parser.map(|p| p.allow_trailing_commas).unwrap_or_default()
|value| value,
),
allow_trailing_commas: parser.and_then(|p| p.allow_trailing_commas).map_or_else(
|| optional_json_file_source.map_or(false, |x| x.allow_trailing_commas()),
|value| value,
),
};
let options = if let Some(overrides) = overrides {
overrides.to_override_json_parser_options(biome_path, options)
Expand Down
44 changes: 26 additions & 18 deletions crates/biome_service/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use biome_configuration::javascript::JsxRuntime;
use biome_configuration::organize_imports::OrganizeImports;
use biome_configuration::{
push_to_analyzer_rules, BiomeDiagnostic, CssConfiguration, FilesConfiguration,
FormatterConfiguration, JavascriptConfiguration, JsonConfiguration, LinterConfiguration,
FormatterConfiguration, JavascriptConfiguration, LinterConfiguration,
OverrideFormatterConfiguration, OverrideLinterConfiguration,
OverrideOrganizeImportsConfiguration, Overrides, PartialConfiguration, PartialCssConfiguration,
PartialJavascriptConfiguration, PartialJsonConfiguration, PlainIndentStyle, Rules,
Expand Down Expand Up @@ -212,7 +212,7 @@ impl Settings {
}
// json settings
if let Some(json) = configuration.json {
self.languages.json = JsonConfiguration::from(json).into();
self.languages.json = json.into();
}
// css settings
if let Some(css) = configuration.css {
Expand Down Expand Up @@ -471,18 +471,24 @@ impl From<JavascriptConfiguration> for LanguageSettings<JsLanguage> {
}
}

impl From<JsonConfiguration> for LanguageSettings<JsonLanguage> {
fn from(json: JsonConfiguration) -> Self {
impl From<PartialJsonConfiguration> for LanguageSettings<JsonLanguage> {
fn from(json: PartialJsonConfiguration) -> Self {
let mut language_setting: LanguageSettings<JsonLanguage> = LanguageSettings::default();

language_setting.parser.allow_comments = json.parser.allow_comments;
language_setting.parser.allow_trailing_commas = json.parser.allow_trailing_commas;
language_setting.formatter.trailing_commas = json.formatter.trailing_commas;
language_setting.formatter.enabled = Some(json.formatter.enabled);
language_setting.formatter.line_width = json.formatter.line_width;
language_setting.formatter.indent_width = json.formatter.indent_width.map(Into::into);
language_setting.formatter.indent_style = json.formatter.indent_style.map(Into::into);
language_setting.linter.enabled = Some(json.linter.enabled);
if let Some(parser) = json.parser {
language_setting.parser.allow_comments = parser.allow_comments;
language_setting.parser.allow_trailing_commas = parser.allow_trailing_commas;
}
if let Some(formatter) = json.formatter {
language_setting.formatter.trailing_commas = formatter.trailing_commas;
language_setting.formatter.enabled = formatter.enabled;
language_setting.formatter.line_width = formatter.line_width;
language_setting.formatter.indent_width = formatter.indent_width.map(Into::into);
language_setting.formatter.indent_style = formatter.indent_style.map(Into::into);
}
if let Some(linter) = json.linter {
language_setting.linter.enabled = linter.enabled;
}

language_setting
}
Expand Down Expand Up @@ -1101,8 +1107,12 @@ impl OverrideSettingPattern {

let json_parser = &self.languages.json.parser;

options.allow_trailing_commas = json_parser.allow_trailing_commas;
options.allow_comments = json_parser.allow_comments;
if let Some(allow_comments) = json_parser.allow_comments {
options.allow_comments = allow_comments;
}
if let Some(allow_trailing_commas) = json_parser.allow_trailing_commas {
options.allow_trailing_commas = allow_trailing_commas;
}

if let Ok(mut writeonly_cache) = self.cached_json_parser_options.write() {
let options = *options;
Expand Down Expand Up @@ -1328,13 +1338,11 @@ fn to_json_language_settings(

let parser = conf.parser.take().unwrap_or_default();
let parent_parser = &parent_settings.parser;
language_setting.parser.allow_comments = parser
.allow_comments
.unwrap_or(parent_parser.allow_comments);
language_setting.parser.allow_comments = parser.allow_comments.or(parent_parser.allow_comments);

language_setting.parser.allow_trailing_commas = parser
.allow_trailing_commas
.unwrap_or(parent_parser.allow_trailing_commas);
.or(parent_parser.allow_trailing_commas);

language_setting
}
Expand Down