Skip to content

Commit

Permalink
refactor(parse/json): change fields in JsonParserOptions and `JsonP…
Browse files Browse the repository at this point in the history
…arserSettings` to `Option`
  • Loading branch information
dyc3 committed Jun 23, 2024
1 parent fc9b1eb commit adf5158
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 27 deletions.
4 changes: 2 additions & 2 deletions crates/biome_json_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ impl<'src> Lexer<'src> {
b'*' if self.peek_byte() == Some(b'/') => {
self.advance(2);

if !self.options.allow_comments {
if !self.options.allow_comments() {
self.diagnostics.push(ParseDiagnostic::new(
"JSON standard does not allow comments.",
start..self.text_position(),
Expand Down Expand Up @@ -777,7 +777,7 @@ impl<'src> Lexer<'src> {
}
}

if !self.options.allow_comments {
if !self.options.allow_comments() {
self.diagnostics.push(ParseDiagnostic::new(
"JSON standard does not allow comments.",
start..self.text_position(),
Expand Down
16 changes: 12 additions & 4 deletions crates/biome_json_parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,28 @@ pub(crate) struct JsonParser<'source> {

#[derive(Default, Debug, Clone, Copy)]
pub struct JsonParserOptions {
pub allow_comments: bool,
pub allow_trailing_commas: bool,
pub allow_comments: Option<bool>,
pub allow_trailing_commas: Option<bool>,
}

impl JsonParserOptions {
pub fn with_allow_comments(mut self) -> Self {
self.allow_comments = true;
self.allow_comments = Some(true);
self
}

pub fn with_allow_trailing_commas(mut self) -> Self {
self.allow_trailing_commas = true;
self.allow_trailing_commas = Some(true);
self
}

pub fn allow_comments(&self) -> bool {
self.allow_comments.unwrap_or_default()
}

pub fn allow_trailing_commas(&self) -> bool {
self.allow_trailing_commas.unwrap_or_default()
}
}

impl From<&JsonFileSource> for JsonParserOptions {
Expand Down
4 changes: 2 additions & 2 deletions crates/biome_json_parser/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ fn parse_sequence(p: &mut JsonParser, root_kind: SequenceKind) -> ParsedSyntax {

match current.parse_item(p) {
SequenceItem::Parsed(Absent) => {
if p.options().allow_trailing_commas && p.last() == Some(T![,]) {
if p.options().allow_trailing_commas() && p.last() == Some(T![,]) {
break;
}

Expand Down Expand Up @@ -261,7 +261,7 @@ fn parse_object_member(p: &mut JsonParser) -> SequenceItem {
let m = p.start();

if parse_member_name(p).is_absent() {
if !(p.options().allow_trailing_commas && p.last() == Some(T![,])) {
if !(p.options().allow_trailing_commas() && p.last() == Some(T![,])) {
p.error(expected_property(p, p.cur_range()));
}

Expand Down
2 changes: 1 addition & 1 deletion crates/biome_json_parser/src/token_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<'source> JsonTokenSource<'source> {
// Not trivia
break;
}
Ok(trivia_kind) if trivia_kind.is_comment() && !self.options.allow_comments => {
Ok(trivia_kind) if trivia_kind.is_comment() && !self.options.allow_comments() => {
self.set_current_token(token);

// Not trivia
Expand Down
8 changes: 4 additions & 4 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 {
allow_comments: test_directory.contains("allow_comments"),
allow_trailing_commas: test_directory.contains("allow_trailing_commas"),
let parse_config = JsonParserOptions {
allow_comments: Some(test_directory.contains("allow_comments")),
allow_trailing_commas: Some(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
14 changes: 8 additions & 6 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,12 @@ 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()
|| optional_json_file_source.map_or(false, |x| x.allow_comments()),
allow_trailing_commas: parser.map(|p| p.allow_trailing_commas).unwrap_or_default()
|| optional_json_file_source.map_or(false, |x| x.allow_trailing_commas()),
allow_comments: parser
.and_then(|p| p.allow_comments)
.or(optional_json_file_source.map(|x| x.allow_comments())),
allow_trailing_commas: parser
.and_then(|p| p.allow_trailing_commas)
.or(optional_json_file_source.map(|x| x.allow_trailing_commas())),
};
let options = if let Some(overrides) = overrides {
overrides.to_override_json_parser_options(biome_path, options)
Expand Down
22 changes: 14 additions & 8 deletions crates/biome_service/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,12 @@ impl From<JsonConfiguration> for LanguageSettings<JsonLanguage> {
fn from(json: JsonConfiguration) -> 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;
if json.parser.allow_comments {
language_setting.parser.allow_comments = Some(true);
}
if json.parser.allow_trailing_commas {
language_setting.parser.allow_trailing_commas = Some(true);
}
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;
Expand Down Expand Up @@ -1101,8 +1105,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 = Some(allow_comments);
}
if let Some(allow_trailing_commas) = json_parser.allow_trailing_commas {
options.allow_trailing_commas = Some(allow_trailing_commas);
}

if let Ok(mut writeonly_cache) = self.cached_json_parser_options.write() {
let options = *options;
Expand Down Expand Up @@ -1328,13 +1336,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

0 comments on commit adf5158

Please sign in to comment.