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

Commit

Permalink
feat(rome_service): add support for JSON linter in the workspace (#4609)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Jun 26, 2023
1 parent 7f8c38b commit c9e3bb5
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 35 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ multiple files:

### Editors

#### Other changes

- The Rome LSP is now able to show diagnostics that belong to JSON lint rules.

### Formatter

- Added a new option called `--jsx-quote-style` to the formatter. This option allows you to choose between single and double quotes for JSX attributes. [#4486](https://github.com/rome/tools/issues/4486)
Expand Down Expand Up @@ -96,6 +100,8 @@ multiple files:
This rule proposes turning function expressions into arrow functions.
Function expressions that use `this` are ignored.

- [`noDuplicateJsonKeys`](https://docs.rome.tools/lint/rules/noDuplicateJsonKeys/)

#### Other changes

- [`noRedeclare`](https://docs.rome.tools/lint/rules/noredeclare/): allow redeclare of index signatures are in different type members [#4478](https://github.com/rome/tools/issues/4478)
Expand Down
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions crates/rome_cli/tests/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2398,3 +2398,46 @@ fn ignores_unknown_file() {
result,
));
}

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

let file_path1 = Path::new("test.json");
fs.insert(
file_path1.into(),
r#"{ "foo": true, "foo": true }"#.as_bytes(),
);

let configuration = Path::new("rome.json");
fs.insert(
configuration.into(),
r#"{
"linter": {
"rules": {
"nursery": {
"noDuplicateJsonKeys": "error"
}
}
}
}"#
.as_bytes(),
);

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

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

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

```json
{
"linter": {
"rules": {
"nursery": {
"noDuplicateJsonKeys": "error"
}
}
}
}
```

## `test.json`

```json
{ "foo": true, "foo": true }
```

# Termination Message

```block
internalError/io ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Some errors were emitted while running checks
```

# Emitted Messages

```block
test.json:1:3 lint/nursery/noDuplicateJsonKeys ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× The key foo was already declared.
> 1 │ { "foo": true, "foo": true }
│ ^^^^^
i This where a duplicated key was declared again.
> 1 │ { "foo": true, "foo": true }
│ ^^^^^
i If a key is defined multiple times, only the last definition takes effect. Previous definitions are ignored.
```

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


12 changes: 6 additions & 6 deletions crates/rome_deserialize/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ pub trait JsonDeserialize: Sized {
/// It accepts a JSON AST and a visitor. The visitor is the [default](Default) implementation of the data
/// type that implements this trait.
fn deserialize_from_ast(
root: JsonRoot,
root: &JsonRoot,
visitor: &mut impl VisitJsonNode,
diagnostics: &mut Vec<DeserializationDiagnostic>,
) -> Option<()>;
}

impl JsonDeserialize for () {
fn deserialize_from_ast(
_root: JsonRoot,
_root: &JsonRoot,
_visitor: &mut impl VisitJsonNode,
_diagnostics: &mut Vec<DeserializationDiagnostic>,
) -> Option<()> {
Expand Down Expand Up @@ -526,14 +526,14 @@ pub fn with_only_known_variants(
/// use rome_deserialize::Deserialized;
/// let mut output = Self::default();
/// let mut diagnostics = vec![];
/// NewConfiguration::deserialize_from_ast(root, &mut output, &mut diagnostics);
/// NewConfiguration::deserialize_from_ast(&root, &mut output, &mut diagnostics);
/// Deserialized::new(output, diagnostics)
/// }
/// }
///
///
/// impl JsonDeserialize for NewConfiguration {
/// fn deserialize_from_ast(root: JsonRoot, visitor: &mut impl VisitJsonNode, diagnostics: &mut Vec<DeserializationDiagnostic>) -> Option<()> {
/// fn deserialize_from_ast(root: &JsonRoot, visitor: &mut impl VisitJsonNode, diagnostics: &mut Vec<DeserializationDiagnostic>) -> Option<()> {
/// let object = root.value().ok()?;
/// let object = object.as_json_object_value()?;
/// for member in object.json_member_list() {
Expand Down Expand Up @@ -561,7 +561,7 @@ where
let mut output = Output::default();
let mut diagnostics = vec![];
let parse = parse_json(source);
Output::deserialize_from_ast(parse.tree(), &mut output, &mut diagnostics);
Output::deserialize_from_ast(&parse.tree(), &mut output, &mut diagnostics);
let mut errors = parse
.into_diagnostics()
.into_iter()
Expand All @@ -580,7 +580,7 @@ where
}

/// Attempts to deserialize a JSON AST, given the `Output`.
pub fn deserialize_from_json_ast<Output>(parse: JsonRoot) -> Deserialized<Output>
pub fn deserialize_from_json_ast<Output>(parse: &JsonRoot) -> Deserialized<Output>
where
Output: Default + VisitJsonNode + JsonDeserialize,
{
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_diagnostics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ schema = ["schemars", "rome_text_edit/schemars", "rome_diagnostics_categories/sc

[dev-dependencies]
serde_json = "1.0.74"
trybuild = "1.0"
trybuild = "1.0.80"
1 change: 1 addition & 0 deletions crates/rome_service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ rome_js_formatter = { workspace = true, features = ["serde"] }
rome_js_parser = { workspace = true }
rome_js_semantic = { workspace = true }
rome_js_syntax = { workspace = true, features = ["serde"] }
rome_json_analyze = { workspace = true }
rome_json_formatter = { workspace = true }
rome_json_parser = { workspace = true }
rome_json_syntax = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_service/src/configuration/parse/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use rome_rowan::AstNode;

impl JsonDeserialize for Configuration {
fn deserialize_from_ast(
root: JsonRoot,
root: &JsonRoot,
visitor: &mut impl VisitJsonNode,
diagnostics: &mut Vec<DeserializationDiagnostic>,
) -> Option<()> {
Expand Down
Loading

0 comments on commit c9e3bb5

Please sign in to comment.