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

feat: add support for .jsonc files #4734

Merged
merged 4 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ rome_parser = { version = "0.0.1", path = "./crates/rome_parser"
rome_rowan = { version = "0.0.1", path = "./crates/rome_rowan" }
rome_service = { path = "./crates/rome_service" }
rome_suppression = { version = "0.0.1", path = "./crates/rome_suppression" }
rome_test_utils = { path = "./crates/rome_test_utils" }
rome_text_edit = { version = "0.0.1", path = "./crates/rome_text_edit" }
rome_text_size = { version = "0.0.1", path = "./crates/rome_text_size" }
tests_macros = { path = "./crates/tests_macros" }
rome_test_utils = { path = "./crates/rome_test_utils" }

# Crates needed in the workspace
bitflags = "2.3.1"
Expand Down
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>
```


2 changes: 1 addition & 1 deletion crates/rome_css_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ version = "0.0.1"

[dependencies]
rome_console = { workspace = true }
rome_css_syntax = { workspace = true }
rome_css_factory = { workspace = true }
rome_css_syntax = { workspace = true }
rome_diagnostics = { workspace = true }
rome_js_unicode_table = { workspace = true }
rome_parser = { workspace = true }
Expand Down
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>
ematipico marked this conversation as resolved.
Show resolved Hide resolved
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
12 changes: 6 additions & 6 deletions crates/rome_js_analyze/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ serde_json = { workspace = true }
smallvec = { workspace = true }

[dev-dependencies]
countme = { workspace = true, features = ["enable"] }
insta = { workspace = true, features = ["glob"] }
rome_js_parser = { workspace = true, features = ["tests"] }
rome_text_edit = { workspace = true }
tests_macros = { workspace = true }
rome_test_utils = { workspace = true }
countme = { workspace = true, features = ["enable"] }
insta = { workspace = true, features = ["glob"] }
rome_js_parser = { workspace = true, features = ["tests"] }
rome_test_utils = { workspace = true }
rome_text_edit = { workspace = true }
tests_macros = { workspace = true }

[features]
schema = ["schemars", "rome_deserialize/schema"]
10 changes: 5 additions & 5 deletions crates/rome_js_transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ version = "0.1.0"
[dependencies]
lazy_static = { workspace = true }
rome_analyze = { workspace = true }
rome_console = { workspace = true }
rome_diagnostics = { workspace = true }
rome_js_factory = { workspace = true }
rome_js_syntax = { workspace = true }
rome_rowan = { workspace = true }
rome_console = {workspace= true}


[dev-dependencies]
insta = { workspace = true }
rome_analyze = { workspace = true }
rome_js_formatter = { workspace = true }
rome_js_parser = { workspace = true }
rome_analyze = { workspace = true }
rome_test_utils = { workspace = true }
tests_macros= { workspace = true }
insta = { workspace = true }
rome_test_utils = { workspace = true }
tests_macros = { workspace = true }
2 changes: 1 addition & 1 deletion crates/rome_json_analyze/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ insta = { workspace = true, features = ["glob"] }
rome_json_factory = { workspace = true }
rome_json_parser = { workspace = true }
rome_service = { workspace = true }
rome_test_utils = { workspace = true }
tests_macros = { workspace = true }
rome_test_utils = { workspace = true }
Loading