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

Commit

Permalink
feat: 🎸 allow comments in json file (#4714)
Browse files Browse the repository at this point in the history
Co-authored-by: IWANABETHATGUY <iwanabethatguy@qq.com>
  • Loading branch information
ematipico and IWANABETHATGUY authored Jul 20, 2023
1 parent 72fe1c7 commit 4795727
Show file tree
Hide file tree
Showing 43 changed files with 587 additions and 119 deletions.
38 changes: 19 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ rome_aria_metadata = { path = "./crates/rome_aria_metadata" }
rome_cli = { path = "./crates/rome_cli" }
rome_console = { version = "0.0.1", path = "./crates/rome_console" }
rome_control_flow = { path = "./crates/rome_control_flow" }
rome_css_factory = { path = "./crates/rome_css_factory" }
rome_css_parser = { path = "./crates/rome_css_parser" }
rome_css_syntax = { path = "./crates/rome_css_syntax" }
rome_deserialize = { version = "0.0.0", path = "./crates/rome_deserialize" }
rome_diagnostics = { version = "0.0.1", path = "./crates/rome_diagnostics" }
rome_diagnostics_categories = { version = "0.0.1", path = "./crates/rome_diagnostics_categories" }
Expand All @@ -51,9 +54,6 @@ rome_json_factory = { version = "0.0.1", path = "./crates/rome_json_fa
rome_json_formatter = { path = "./crates/rome_json_formatter" }
rome_json_parser = { path = "./crates/rome_json_parser" }
rome_json_syntax = { version = "0.0.1", path = "./crates/rome_json_syntax" }
rome_css_factory = { path = "./crates/rome_css_factory" }
rome_css_parser = { path = "./crates/rome_css_parser" }
rome_css_syntax = { path = "./crates/rome_css_syntax" }
rome_lsp = { path = "./crates/rome_lsp" }
rome_markup = { version = "0.0.1", path = "./crates/rome_markup" }
rome_migrate = { path = "./crates/rome_migrate" }
Expand All @@ -65,24 +65,24 @@ rome_text_size = { version = "0.0.1", path = "./crates/rome_text_si
tests_macros = { path = "./crates/tests_macros" }

# Crates needed in the workspace
bitflags = "2.3.1"
bpaf = { version = "0.9.2", features = ["derive"] }
countme = "3.0.1"
dashmap = "5.4.0"
indexmap = "1.9.3"
insta = "1.29.0"
lazy_static = "1.4.0"
quickcheck = "1.0.3"
quickcheck_macros = "1.0.0"
quickcheck = "1.0.3"
bitflags = "2.3.1"
bpaf = { version = "0.9.2", features = ["derive"] }
countme = "3.0.1"
dashmap = "5.4.0"
indexmap = "1.9.3"
insta = "1.29.0"
lazy_static = "1.4.0"
quote = { version = "1.0.28" }
rustc-hash = "1.1.0"
schemars = { version = "0.8.12" }
serde = { version = "1.0.163", features = ["derive"], default-features = false }
serde_json = "1.0.96"
smallvec = { version = "1.10.0", features = ["union", "const_new"] }
tracing = { version = "0.1.37", default-features = false, features = ["std"] }
quote = { version = "1.0.28" }
rustc-hash = "1.1.0"
schemars = { version = "0.8.12" }
serde = { version = "1.0.163", features = ["derive"], default-features = false }
serde_json = "1.0.96"
smallvec = { version = "1.10.0", features = ["union", "const_new"] }
tracing = { version = "0.1.37", default-features = false, features = ["std"] }
# pinning to version 1.18 to avoid multiple versions of windows-sys as dependency
tokio = { version = "~1.18.5" }
tokio = { version = "~1.18.5" }


[profile.dev.package.rome_wasm]
Expand Down
3 changes: 2 additions & 1 deletion crates/rome_cli/src/execute/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{CliDiagnostic, CliSession};
use rome_console::{markup, ConsoleExt};
use rome_diagnostics::{category, PrintDiagnostic};
use rome_fs::OpenOptions;
use rome_json_parser::JsonParserOptions;
use rome_json_syntax::JsonRoot;
use rome_migrate::{migrate_configuration, ControlFlow};
use rome_rowan::AstNode;
Expand All @@ -26,7 +27,7 @@ pub(crate) fn run(
fs.open_with_options(configuration_path.as_path(), open_options)?;
let mut configuration_content = String::new();
configuration_file.read_to_string(&mut configuration_content)?;
let parsed = rome_json_parser::parse_json(&configuration_content);
let parsed = rome_json_parser::parse_json(&configuration_content, JsonParserOptions::default());
let mut errors = 0;
let mut tree = parsed.tree();
let mut actions = Vec::new();
Expand Down
1 change: 0 additions & 1 deletion crates/rome_cli/src/execute/traverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ pub(crate) fn traverse(
inputs: Vec<OsString>,
) -> Result<(), CliDiagnostic> {
init_thread_pool();

if inputs.is_empty() && execution.as_stdin_file().is_none() {
return Err(CliDiagnostic::missing_argument(
"<INPUT>",
Expand Down
37 changes: 37 additions & 0 deletions crates/rome_cli/tests/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1839,3 +1839,40 @@ fn doesnt_error_if_no_files_were_processed() {
result,
));
}

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

let config_json = r#"{
"json": {
"parser": { "allowComments": true }
}
}
"#;
let rome_config = "rome.json";
let code = r#"
/*test*/ [1, 2, 3]
"#;
let file_path = Path::new("tsconfig.json");
fs.insert(file_path.into(), code.as_bytes());
fs.insert(rome_config.into(), config_json);

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!(),
"ignore_comments_error_when_allow_comments",
fs,
console,
result,
));
}
9 changes: 6 additions & 3 deletions crates/rome_cli/tests/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bpaf::Args;
use rome_console::BufferConsole;
use rome_fs::{FileSystemExt, MemoryFileSystem};
use rome_json_formatter::context::JsonFormatOptions;
use rome_json_parser::parse_json;
use rome_json_parser::{parse_json, JsonParserOptions};
use rome_service::DynRef;
use std::path::Path;

Expand Down Expand Up @@ -52,7 +52,7 @@ fn creates_config_file() {
let mut content = String::new();
file.read_to_string(&mut content)
.expect("failed to read file from memory FS");
let parsed = parse_json(CONFIG_INIT_DEFAULT);
let parsed = parse_json(CONFIG_INIT_DEFAULT, JsonParserOptions::default());
let formatted =
rome_json_formatter::format_node(JsonFormatOptions::default(), &parsed.syntax())
.expect("valid format document")
Expand Down Expand Up @@ -95,7 +95,10 @@ fn creates_config_file_when_rome_installed_via_package_manager() {
let mut content = String::new();
file.read_to_string(&mut content)
.expect("failed to read file from memory FS");
let parsed = parse_json(CONFIG_INIT_DEFAULT_WHEN_INSTALLED);
let parsed = parse_json(
CONFIG_INIT_DEFAULT_WHEN_INSTALLED,
JsonParserOptions::default(),
);
let formatted =
rome_json_formatter::format_node(JsonFormatOptions::default(), &parsed.syntax())
.expect("valid format document")
Expand Down
7 changes: 5 additions & 2 deletions crates/rome_cli/tests/snap_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rome_formatter::IndentStyle;
use rome_fs::{FileSystemExt, MemoryFileSystem};
use rome_json_formatter::context::JsonFormatOptions;
use rome_json_formatter::format_node;
use rome_json_parser::parse_json;
use rome_json_parser::{parse_json, JsonParserOptions};
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::env::{current_exe, temp_dir};
Expand Down Expand Up @@ -49,7 +49,10 @@ impl CliSnapshot {
let mut content = String::new();

if let Some(configuration) = &self.configuration {
let parsed = parse_json(&redact_snapshot(configuration));
let parsed = parse_json(
&redact_snapshot(configuration),
JsonParserOptions::default(),
);
let formatted = format_node(
JsonFormatOptions::default().with_indent_style(IndentStyle::Space(2)),
&parsed.syntax(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
source: crates/rome_cli/tests/snap_test.rs
expression: content
---
## `rome.json`

```json
{
"json": {
"parser": { "allowComments": true }
}
}
```

## `tsconfig.json`

```json

/*test*/ [1, 2, 3]

```

# Termination Message

```block
internalError/io ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× No files were processed in the specified paths.
```

# Emitted Messages

```block
tsconfig.json format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Format with errors is disabled.
```

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

```block
Skipped 1 file(s)
```


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,9 +12,9 @@ version = "0.0.1"

[dependencies]
rome_console = { workspace = true }
rome_css_syntax = { workspace = true }
rome_diagnostics = { workspace = true }
rome_js_unicode_table = { workspace = true }
rome_css_syntax = { workspace = true }
rome_parser = { workspace = true }
rome_rowan = { workspace = true }
tracing = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/rome_deserialize/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{DeserializationDiagnostic, Deserialized, VisitNode};
use indexmap::IndexSet;
use rome_console::markup;
use rome_diagnostics::{DiagnosticExt, Error};
use rome_json_parser::parse_json;
use rome_json_parser::{parse_json, JsonParserOptions};
use rome_json_syntax::{
AnyJsonValue, JsonArrayValue, JsonBooleanValue, JsonLanguage, JsonMemberName, JsonNumberValue,
JsonObjectValue, JsonRoot, JsonStringValue, JsonSyntaxNode,
Expand Down Expand Up @@ -560,7 +560,7 @@ where
{
let mut output = Output::default();
let mut diagnostics = vec![];
let parse = parse_json(source);
let parse = parse_json(source, JsonParserOptions::default());
Output::deserialize_from_ast(&parse.tree(), &mut output, &mut diagnostics);
let mut errors = parse
.into_diagnostics()
Expand Down
4 changes: 2 additions & 2 deletions crates/rome_json_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ mod tests {
use rome_console::{markup, Markup};
use rome_diagnostics::termcolor::NoColor;
use rome_diagnostics::{Diagnostic, DiagnosticExt, PrintDiagnostic, Severity};
use rome_json_parser::parse_json;
use rome_json_parser::{parse_json, JsonParserOptions};
use rome_json_syntax::TextRange;
use std::slice;

Expand All @@ -130,7 +130,7 @@ mod tests {
}
"#;

let parsed = parse_json(SOURCE);
let parsed = parse_json(SOURCE, JsonParserOptions::default());

let mut error_ranges: Vec<TextRange> = Vec::new();
let rule_filter = RuleFilter::Rule("nursery", "noDuplicateKeys");
Expand Down
6 changes: 3 additions & 3 deletions crates/rome_json_analyze/tests/spec_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rome_deserialize::json::deserialize_from_json_str;
use rome_diagnostics::advice::CodeSuggestionAdvice;
use rome_diagnostics::termcolor::{Buffer, NoColor};
use rome_diagnostics::{DiagnosticExt, Error, PrintDiagnostic, Severity};
use rome_json_parser::{parse_json, JsonParse};
use rome_json_parser::{parse_json, JsonParse, JsonParserOptions};
use rome_json_syntax::{JsonLanguage, JsonSyntaxNode};
use rome_rowan::SyntaxKind;
use rome_rowan::SyntaxSlot;
Expand Down Expand Up @@ -77,7 +77,7 @@ pub(crate) fn write_analysis_to_snapshot(
file_name: &str,
input_file: &Path,
) -> usize {
let parsed = parse_json(input_code);
let parsed = parse_json(input_code, JsonParserOptions::default());
let root = parsed.tree();

let mut diagnostics = Vec::new();
Expand Down Expand Up @@ -236,7 +236,7 @@ fn check_code_action(path: &Path, source: &str, action: &AnalyzerAction<JsonLang
}

// Re-parse the modified code and panic if the resulting tree has syntax errors
let re_parse = parse_json(&output);
let re_parse = parse_json(&output, JsonParserOptions::default());
assert_errors_are_absent(&re_parse, path);
}

Expand Down
5 changes: 3 additions & 2 deletions crates/rome_json_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,10 @@ pub fn format_sub_tree(options: JsonFormatOptions, root: &JsonSyntaxNode) -> For

#[cfg(test)]
mod tests {

use crate::context::JsonFormatOptions;
use crate::format_node;
use rome_json_parser::parse_json;
use rome_json_parser::{parse_json, JsonParserOptions};

#[test]
fn smoke_test() {
Expand All @@ -305,7 +306,7 @@ mod tests {
"e": false
}
"#;
let parse = parse_json(src);
let parse = parse_json(src, JsonParserOptions::default());
let options = JsonFormatOptions::default();
let formatted = format_node(options, &parse.syntax()).unwrap();
assert_eq!(
Expand Down
11 changes: 7 additions & 4 deletions crates/rome_json_formatter/tests/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ use rome_formatter::{FormatContext, FormatResult, Formatted, IndentStyle, LineWi
use rome_formatter_test::TestFormatLanguage;
use rome_json_formatter::context::{JsonFormatContext, JsonFormatOptions};
use rome_json_formatter::{format_node, format_range, JsonFormatLanguage};
use rome_json_parser::parse_json;
use rome_json_parser::{parse_json, JsonParserOptions};
use rome_json_syntax::{JsonFileSource, JsonLanguage};
use rome_parser::AnyParse;
use rome_rowan::{FileSource, SyntaxNode, TextRange};
use serde::{Deserialize, Serialize};

#[derive(Default)]
pub struct JsonTestFormatLanguage;
pub struct JsonTestFormatLanguage {
source_type: JsonFileSource,
}

impl TestFormatLanguage for JsonTestFormatLanguage {
type SyntaxLanguage = JsonLanguage;
Expand All @@ -18,11 +20,12 @@ impl TestFormatLanguage for JsonTestFormatLanguage {
type FormatLanguage = JsonFormatLanguage;

fn parse(&self, text: &str) -> AnyParse {
let parse = parse_json(text);
let parse = parse_json(text, JsonParserOptions::default());

AnyParse::new(
parse.syntax().as_send().unwrap(),
parse.into_diagnostics(),
JsonFileSource::json().as_any_file_source(),
self.source_type.as_any_file_source(),
)
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rome_json_formatter/tests/quick_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rome_formatter_test::check_reformat::CheckReformat;
use rome_json_formatter::context::JsonFormatOptions;
use rome_json_formatter::format_node;
use rome_json_parser::parse_json;
use rome_json_parser::{parse_json, JsonParserOptions};

mod language {
include!("language.rs");
Expand All @@ -27,7 +27,7 @@ fn quick_test() {
]
}
"#;
let parse = parse_json(src);
let parse = parse_json(src, JsonParserOptions::default());
let options = JsonFormatOptions::default();
let result = format_node(options.clone(), &parse.syntax())
.unwrap()
Expand Down
Loading

0 comments on commit 4795727

Please sign in to comment.