Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
feat: add support for .jsonc files
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Jul 28, 2023
1 parent c197360 commit 2e2d6f3
Show file tree
Hide file tree
Showing 14 changed files with 333 additions and 17 deletions.
2 changes: 1 addition & 1 deletion crates/rome_cli/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl LoadedConfiguration {
)

})?;
let deserialized = deserialize_from_json_str::<Configuration>(content.as_str());
let deserialized = deserialize_from_json_str::<Configuration>(content.as_str(), true);
deserialized_configurations.push(deserialized)
}
Ok(deserialized_configurations)
Expand Down
77 changes: 76 additions & 1 deletion crates/rome_cli/tests/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1866,7 +1866,7 @@ fn ignore_comments_error_when_allow_comments() {
Args::from([("format"), file_path.as_os_str().to_str().unwrap()].as_slice()),
);

// assert!(result.is_ok(), "run_cli returned {result:?}");
assert!(result.is_ok(), "run_cli returned {result:?}");

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
Expand All @@ -1876,3 +1876,78 @@ fn ignore_comments_error_when_allow_comments() {
result,
));
}

#[test]
fn format_jsonc_files() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let code = r#"
/*test*/ [
/* some other comment*/1, 2, 3]
"#;
let file_path = Path::new("file.jsonc");
fs.insert(file_path.into(), code.as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from([("format"), file_path.as_os_str().to_str().unwrap()].as_slice()),
);

assert!(result.is_ok(), "run_cli returned {result:?}");

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"format_jsonc_files",
fs,
console,
result,
));
}

#[test]
fn treat_known_json_files_as_jsonc_files() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let code = r#"
/*test*/ [
/* some other comment*/1, 2, 3]
"#;
let ts = Path::new("files/typescript.json");
fs.insert(ts.into(), code.as_bytes());
let eslint = Path::new("files/.eslintrc.json");
fs.insert(eslint.into(), code.as_bytes());
let jshint = Path::new("files/.jshintrc");
fs.insert(jshint.into(), code.as_bytes());
let babel = Path::new("files/.babelrc");
fs.insert(babel.into(), code.as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(
[
("format"),
ts.as_os_str().to_str().unwrap(),
eslint.as_os_str().to_str().unwrap(),
jshint.as_os_str().to_str().unwrap(),
babel.as_os_str().to_str().unwrap(),
]
.as_slice(),
),
);

assert!(result.is_ok(), "run_cli returned {result:?}");

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"treat_known_json_files_as_jsonc_files",
fs,
console,
result,
));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
source: crates/rome_cli/tests/snap_test.rs
expression: content
---
## `file.jsonc`

```jsonc

/*test*/ [

/* some other comment*/1, 2, 3]

```

# Emitted Messages

```block
file.jsonc format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
i Formatter would have printed the following content:
1 │ -
2 │ - /*test*/·[
3 │ -
4 │ - /*·some·other·comment*/1,·2,·3]
5 │ - →
1 │ + /*test*/·[/*·some·other·comment*/·1,·2,·3]
2 │ +
```

```block
Compared 1 file(s) in <TIME>
```


Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
---
source: crates/rome_cli/tests/snap_test.rs
expression: content
---
## `files/.babelrc`

```babelrc
/*test*/ [
/* some other comment*/1, 2, 3]
```

## `files/.eslintrc.json`

```json

/*test*/ [

/* some other comment*/1, 2, 3]

```

## `files/.jshintrc`

```jshintrc
/*test*/ [
/* some other comment*/1, 2, 3]
```

## `files/typescript.json`

```json

/*test*/ [

/* some other comment*/1, 2, 3]

```

# Emitted Messages

```block
files/typescript.json format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
i Formatter would have printed the following content:
1 │ -
2 │ - /*test*/·[
3 │ -
4 │ - /*·some·other·comment*/1,·2,·3]
5 │ - →
1 │ + /*test*/·[/*·some·other·comment*/·1,·2,·3]
2 │ +
```

```block
files/.eslintrc.json format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
i Formatter would have printed the following content:
1 │ -
2 │ - /*test*/·[
3 │ -
4 │ - /*·some·other·comment*/1,·2,·3]
5 │ - →
1 │ + /*test*/·[/*·some·other·comment*/·1,·2,·3]
2 │ +
```

```block
files/.jshintrc format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
i Formatter would have printed the following content:
1 │ -
2 │ - /*test*/·[
3 │ -
4 │ - /*·some·other·comment*/1,·2,·3]
5 │ - →
1 │ + /*test*/·[/*·some·other·comment*/·1,·2,·3]
2 │ +
```

```block
files/.babelrc format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
i Formatter would have printed the following content:
1 │ -
2 │ - /*test*/·[
3 │ -
4 │ - /*·some·other·comment*/1,·2,·3]
5 │ - →
1 │ + /*test*/·[/*·some·other·comment*/·1,·2,·3]
2 │ +
```

```block
Compared 4 file(s) in <TIME>
```


Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
source: crates/rome_cli/tests/snap_test.rs
expression: content
---
## `typescript.json`

```json

/*test*/ [

/* some other comment*/1, 2, 3]

```

# Emitted Messages

```block
typescript.json format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
i Formatter would have printed the following content:
1 │ -
2 │ - /*test*/·[
3 │ -
4 │ - /*·some·other·comment*/1,·2,·3]
5 │ - →
1 │ + /*test*/·[/*·some·other·comment*/·1,·2,·3]
2 │ +
```

```block
Compared 1 file(s) in <TIME>
```


13 changes: 9 additions & 4 deletions crates/rome_deserialize/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,22 +588,27 @@ pub fn with_only_known_variants(
/// }
///
/// # fn main() -> Result<(), DeserializationDiagnostic> {
/// let source = r#"{ "lorem": true }"#;
/// let deserialized = deserialize_from_json_str::<NewConfiguration>(&source);
/// let source = r#"{ "lorem": true }"#;
/// let deserialized = deserialize_from_json_str::<NewConfiguration>(&source, false);
/// assert!(!deserialized.has_errors());
/// assert_eq!(deserialized.into_deserialized(), NewConfiguration { lorem: true });
/// # Ok(())
/// # }
///
///
/// ```
pub fn deserialize_from_json_str<Output>(source: &str) -> Deserialized<Output>
pub fn deserialize_from_json_str<Output>(source: &str, allow_comments: bool) -> Deserialized<Output>
where
Output: Default + VisitJsonNode + JsonDeserialize,
{
let mut output = Output::default();
let mut diagnostics = vec![];
let parse = parse_json(source, JsonParserOptions::default());
let options = if allow_comments {
JsonParserOptions::default().with_allow_comments()
} else {
JsonParserOptions::default()
};
let parse = parse_json(source, options);
Output::deserialize_from_ast(&parse.tree(), &mut output, &mut diagnostics);
let mut errors = parse
.into_diagnostics()
Expand Down
4 changes: 4 additions & 0 deletions crates/rome_json_syntax/src/file_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ impl JsonFileSource {
variant: JsonVariant::Jsonc,
}
}

pub const fn is_jsonc(&self) -> bool {
matches!(self.variant, JsonVariant::Jsonc)
}
}

impl<'a> FileSource<'a, JsonLanguage> for JsonFileSource {}
Expand Down
5 changes: 3 additions & 2 deletions crates/rome_service/src/configuration/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ mod test {
#[test]
fn deserialization_error() {
let content = "{ \n\n\"formatter\" }";
let result = deserialize_from_json_str::<Configuration>(content);
let result = deserialize_from_json_str::<Configuration>(content, false);

assert!(result.has_errors());
for diagnostic in result.into_diagnostics() {
Expand All @@ -363,6 +363,7 @@ mod test {
}
}
}"#;
let _result = deserialize_from_json_str::<Configuration>(content).into_deserialized();
let _result =
deserialize_from_json_str::<Configuration>(content, false).into_deserialized();
}
}
2 changes: 1 addition & 1 deletion crates/rome_service/src/configuration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ pub fn load_config(
directory_path,
file_path,
} = auto_search_result;
let deserialized = deserialize_from_json_str::<Configuration>(&content);
let deserialized = deserialize_from_json_str::<Configuration>(&content, true);
Ok(Some(ConfigurationPayload {
deserialized,
configuration_file_path: file_path,
Expand Down
Loading

0 comments on commit 2e2d6f3

Please sign in to comment.