Skip to content

Commit

Permalink
fix(json_parser): fix broken JsonParserOptions::from(&JsonFileSource)…
Browse files Browse the repository at this point in the history
… implementation
  • Loading branch information
cr7pt0gr4ph7 committed Nov 14, 2024
1 parent 95faf70 commit 271bfbd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
29 changes: 26 additions & 3 deletions crates/biome_json_parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ pub struct JsonParserOptions {
}

impl JsonParserOptions {
#[must_use]
pub fn with_allow_comments(mut self) -> Self {
self.allow_comments = true;
self
}

#[must_use]
pub fn with_allow_trailing_commas(mut self) -> Self {
self.allow_trailing_commas = true;
self
Expand All @@ -32,12 +34,12 @@ impl JsonParserOptions {

impl From<&JsonFileSource> for JsonParserOptions {
fn from(file_source: &JsonFileSource) -> Self {
let options = Self::default();
let mut options = Self::default();
if file_source.allow_comments() {
options.with_allow_comments();
options = options.with_allow_comments();
}
if file_source.allow_trailing_commas() {
options.with_allow_trailing_commas();
options = options.with_allow_trailing_commas();
}
options
}
Expand Down Expand Up @@ -92,3 +94,24 @@ impl<'source> Parser for JsonParser<'source> {
&mut self.source
}
}

#[cfg(test)]
mod tests {
use crate::JsonParserOptions;
use biome_json_syntax::JsonFileSource;

#[test]
fn parse_options_from_json_file_source_respects_allow_comments_and_allow_trailing_comma() {
let p1 = JsonParserOptions::from(&JsonFileSource::json());
assert!(!p1.allow_comments);
assert!(p1.allow_trailing_commas);

let p2 = JsonParserOptions::from(&JsonFileSource::json_allow_comments());
assert!(p2.allow_comments);
assert!(!p2.allow_trailing_commas);

let p3 = JsonParserOptions::from(&JsonFileSource::json_allow_comments_and_trailing_commas());
assert!(p3.allow_comments);
assert!(p3.allow_trailing_commas);
}
}
3 changes: 3 additions & 0 deletions crates/biome_json_syntax/src/file_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,18 @@ impl JsonFileSource {
}
}

#[must_use]
pub fn with_allow_trailing_commas(mut self) -> Self {
self.allow_trailing_commas = true;
self
}

#[must_use]
pub fn allow_trailing_commas(&self) -> bool {
self.allow_trailing_commas
}

#[must_use]
pub fn with_allow_comments(mut self) -> Self {
self.allow_comments = true;
self
Expand Down

0 comments on commit 271bfbd

Please sign in to comment.