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 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
7 changes: 5 additions & 2 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ jobs:
uses: moonrepo/setup-rust@v0
with:
components: rustfmt
- name: Run rustfmt
run: cargo fmt --all --check
bins: taplo-cli
- name: Run format
run: |
cargo fmt --all --check
taplo fmt -- --locked

lint:
name: Lint Rust Files
Expand Down
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
6 changes: 5 additions & 1 deletion crates/rome_cli/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rome_deserialize::json::deserialize_from_json_str;
use rome_deserialize::Deserialized;
use rome_diagnostics::{DiagnosticExt, Error, PrintDiagnostic};
use rome_fs::{FileSystem, OpenOptions};
use rome_json_parser::JsonParserOptions;
use rome_service::configuration::diagnostics::CantLoadExtendFile;
use rome_service::configuration::ConfigurationPayload;
use rome_service::{
Expand Down Expand Up @@ -72,7 +73,10 @@ impl LoadedConfiguration {
)

})?;
let deserialized = deserialize_from_json_str::<Configuration>(content.as_str());
let deserialized = deserialize_from_json_str::<Configuration>(
content.as_str(),
JsonParserOptions::default(),
);
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
10 changes: 7 additions & 3 deletions crates/rome_deserialize/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,23 +587,27 @@ pub fn with_only_known_variants(
/// }
/// }
///
/// use rome_json_parser::JsonParserOptions;
/// # fn main() -> Result<(), DeserializationDiagnostic> {
/// let source = r#"{ "lorem": true }"#;
/// let deserialized = deserialize_from_json_str::<NewConfiguration>(&source);
/// let deserialized = deserialize_from_json_str::<NewConfiguration>(&source, JsonParserOptions::default());
/// 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,
options: JsonParserOptions,
) -> Deserialized<Output>
where
Output: Default + VisitJsonNode + JsonDeserialize,
{
let mut output = Output::default();
let mut diagnostics = vec![];
let parse = parse_json(source, 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"]
Loading