From d3df127584ee7c5f8157ba84af9d78ae14ee8565 Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Tue, 18 Apr 2023 00:02:47 +0800 Subject: [PATCH 01/15] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20allow=20comments?= =?UTF-8?q?=20in=20json=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/rome_service/src/configuration/json.rs | 14 +++++++ crates/rome_service/src/configuration/mod.rs | 10 +++++ .../src/configuration/parse/json.rs | 1 + .../configuration/parse/json/configuration.rs | 6 +++ .../src/configuration/parse/json/json.rs | 39 +++++++++++++++++++ 5 files changed, 70 insertions(+) create mode 100644 crates/rome_service/src/configuration/json.rs create mode 100644 crates/rome_service/src/configuration/parse/json/json.rs diff --git a/crates/rome_service/src/configuration/json.rs b/crates/rome_service/src/configuration/json.rs new file mode 100644 index 00000000000..f16d2c170f4 --- /dev/null +++ b/crates/rome_service/src/configuration/json.rs @@ -0,0 +1,14 @@ +use indexmap::IndexSet; +use serde::{Deserialize, Serialize}; + +#[derive(Default, Debug, Deserialize, Serialize, Eq, PartialEq)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[serde(default, deny_unknown_fields)] +pub struct JsonConfiguration { + #[serde(skip_serializing_if = "Option::is_none")] + pub allow_comments: Option>, +} + +impl JsonConfiguration { + pub(crate) const KNOWN_KEYS: &'static [&'static str] = &["allowComments"]; +} diff --git a/crates/rome_service/src/configuration/mod.rs b/crates/rome_service/src/configuration/mod.rs index c907f2c8bcd..c5b5c1c2c03 100644 --- a/crates/rome_service/src/configuration/mod.rs +++ b/crates/rome_service/src/configuration/mod.rs @@ -16,6 +16,9 @@ pub mod diagnostics; pub mod formatter; mod generated; pub mod javascript; +mod javascript; +mod javascript; +mod json; pub mod linter; mod merge; pub mod organize_imports; @@ -40,6 +43,8 @@ use rome_js_analyze::metadata; use rome_json_formatter::context::JsonFormatOptions; use rome_json_parser::parse_json; +use self::json::JsonConfiguration; + /// The configuration that is contained inside the file `rome.json` #[derive(Debug, Deserialize, Serialize, Clone, Bpaf)] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] @@ -80,6 +85,10 @@ pub struct Configuration { #[serde(skip_serializing_if = "Option::is_none")] #[bpaf(external(javascript_configuration), optional)] pub javascript: Option, + + /// Specific configuration for the JavaScript language + #[serde(skip_serializing_if = "Option::is_none")] + pub json: Option, } impl Default for Configuration { @@ -95,6 +104,7 @@ impl Default for Configuration { javascript: None, schema: None, vcs: None, + json: None, } } } diff --git a/crates/rome_service/src/configuration/parse/json.rs b/crates/rome_service/src/configuration/parse/json.rs index 7ad7f02e55b..fbea04f7e0a 100644 --- a/crates/rome_service/src/configuration/parse/json.rs +++ b/crates/rome_service/src/configuration/parse/json.rs @@ -5,6 +5,7 @@ mod configuration; mod files; mod formatter; mod javascript; +mod json; mod linter; mod organize_imports; mod rules; diff --git a/crates/rome_service/src/configuration/parse/json/configuration.rs b/crates/rome_service/src/configuration/parse/json/configuration.rs index 982bfb4940d..98a514b412a 100644 --- a/crates/rome_service/src/configuration/parse/json/configuration.rs +++ b/crates/rome_service/src/configuration/parse/json/configuration.rs @@ -1,3 +1,4 @@ +use crate::configuration::json::JsonConfiguration; use crate::configuration::organize_imports::OrganizeImports; use crate::configuration::parse::json::vcs::validate_vcs_configuration; use crate::configuration::vcs::VcsConfiguration; @@ -59,6 +60,11 @@ impl VisitNode for Configuration { self.map_to_object(&value, name_text, &mut javascript, diagnostics)?; self.javascript = Some(javascript); } + "json" => { + let mut json = JsonConfiguration::default(); + self.map_to_object(&value, name_text, &mut json, diagnostics)?; + self.json = Some(json); + } "organizeImports" => { let mut organize_imports = OrganizeImports::default(); self.map_to_object(&value, name_text, &mut organize_imports, diagnostics)?; diff --git a/crates/rome_service/src/configuration/parse/json/json.rs b/crates/rome_service/src/configuration/parse/json/json.rs new file mode 100644 index 00000000000..2bc6eab4a58 --- /dev/null +++ b/crates/rome_service/src/configuration/parse/json/json.rs @@ -0,0 +1,39 @@ + +use crate::configuration::json::JsonConfiguration; + +use rome_deserialize::json::{has_only_known_keys, VisitJsonNode}; +use rome_deserialize::{DeserializationDiagnostic, VisitNode}; +use rome_json_syntax::{JsonLanguage, JsonSyntaxNode}; +use rome_rowan::SyntaxNode; + +impl VisitJsonNode for JsonConfiguration {} + +impl VisitNode for JsonConfiguration { + fn visit_member_name( + &mut self, + node: &JsonSyntaxNode, + diagnostics: &mut Vec, + ) -> Option<()> { + has_only_known_keys(node, JsonConfiguration::KNOWN_KEYS, diagnostics) + } + + fn visit_map( + &mut self, + key: &SyntaxNode, + value: &SyntaxNode, + diagnostics: &mut Vec, + ) -> Option<()> { + let (name, value) = self.get_key_and_value(key, value, diagnostics)?; + let name_text = name.text(); + + match name_text { + "allowComments" => { + let glob_array = self.map_to_index_set_string(&value, name_text, diagnostics)?; + self.allow_comments = Some(glob_array); + } + _ => {} + } + + Some(()) + } +} From 081a64eb1b3e767bc16fe50eff22607ae9595309 Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Tue, 18 Apr 2023 00:07:39 +0800 Subject: [PATCH 02/15] =?UTF-8?q?chore:=20=F0=9F=A4=96=20fmt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/rome_service/src/configuration/parse/json/json.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/rome_service/src/configuration/parse/json/json.rs b/crates/rome_service/src/configuration/parse/json/json.rs index 2bc6eab4a58..26a3c294db8 100644 --- a/crates/rome_service/src/configuration/parse/json/json.rs +++ b/crates/rome_service/src/configuration/parse/json/json.rs @@ -1,4 +1,3 @@ - use crate::configuration::json::JsonConfiguration; use rome_deserialize::json::{has_only_known_keys, VisitJsonNode}; From 1387b2e432f0c3209b0b46f026556efac6f67ed1 Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Tue, 18 Apr 2023 00:14:10 +0800 Subject: [PATCH 03/15] =?UTF-8?q?chore:=20=F0=9F=A4=96=20update=20schema?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- editors/vscode/configuration_schema.json | 18 ++++++++++++++++++ npm/rome/configuration_schema.json | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/editors/vscode/configuration_schema.json b/editors/vscode/configuration_schema.json index d11c88c2166..b635987a071 100644 --- a/editors/vscode/configuration_schema.json +++ b/editors/vscode/configuration_schema.json @@ -29,6 +29,13 @@ { "type": "null" } ] }, + "json": { + "description": "Specific configuration for the JavaScript language", + "anyOf": [ + { "$ref": "#/definitions/JsonConfiguration" }, + { "type": "null" } + ] + }, "linter": { "description": "The configuration for the linter", "anyOf": [ @@ -617,6 +624,17 @@ "type": "object", "additionalProperties": false }, + "JsonConfiguration": { + "type": "object", + "properties": { + "allow_comments": { + "type": ["array", "null"], + "items": { "type": "string" }, + "uniqueItems": true + } + }, + "additionalProperties": false + }, "LineWidth": { "description": "Validated value for the `line_width` formatter options\n\nThe allowed range of values is 1..=320", "type": "integer", diff --git a/npm/rome/configuration_schema.json b/npm/rome/configuration_schema.json index d11c88c2166..b635987a071 100644 --- a/npm/rome/configuration_schema.json +++ b/npm/rome/configuration_schema.json @@ -29,6 +29,13 @@ { "type": "null" } ] }, + "json": { + "description": "Specific configuration for the JavaScript language", + "anyOf": [ + { "$ref": "#/definitions/JsonConfiguration" }, + { "type": "null" } + ] + }, "linter": { "description": "The configuration for the linter", "anyOf": [ @@ -617,6 +624,17 @@ "type": "object", "additionalProperties": false }, + "JsonConfiguration": { + "type": "object", + "properties": { + "allow_comments": { + "type": ["array", "null"], + "items": { "type": "string" }, + "uniqueItems": true + } + }, + "additionalProperties": false + }, "LineWidth": { "description": "Validated value for the `line_width` formatter options\n\nThe allowed range of values is 1..=320", "type": "integer", From 028ab0e921cd690d38b8499eefe2aea3c816c40c Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Tue, 18 Apr 2023 01:06:25 +0800 Subject: [PATCH 04/15] =?UTF-8?q?chore:=20=F0=9F=A4=96=20lint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/rome_service/src/configuration/parse/json.rs | 2 +- .../parse/json/{json.rs => json_visitor.rs} | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) rename crates/rome_service/src/configuration/parse/json/{json.rs => json_visitor.rs} (79%) diff --git a/crates/rome_service/src/configuration/parse/json.rs b/crates/rome_service/src/configuration/parse/json.rs index fbea04f7e0a..28e7a510465 100644 --- a/crates/rome_service/src/configuration/parse/json.rs +++ b/crates/rome_service/src/configuration/parse/json.rs @@ -5,7 +5,7 @@ mod configuration; mod files; mod formatter; mod javascript; -mod json; +mod json_visitor; mod linter; mod organize_imports; mod rules; diff --git a/crates/rome_service/src/configuration/parse/json/json.rs b/crates/rome_service/src/configuration/parse/json/json_visitor.rs similarity index 79% rename from crates/rome_service/src/configuration/parse/json/json.rs rename to crates/rome_service/src/configuration/parse/json/json_visitor.rs index 26a3c294db8..a05a615100a 100644 --- a/crates/rome_service/src/configuration/parse/json/json.rs +++ b/crates/rome_service/src/configuration/parse/json/json_visitor.rs @@ -25,12 +25,9 @@ impl VisitNode for JsonConfiguration { let (name, value) = self.get_key_and_value(key, value, diagnostics)?; let name_text = name.text(); - match name_text { - "allowComments" => { - let glob_array = self.map_to_index_set_string(&value, name_text, diagnostics)?; - self.allow_comments = Some(glob_array); - } - _ => {} + if let "allowComments" = name_text { + let glob_array = self.map_to_index_set_string(&value, name_text, diagnostics)?; + self.allow_comments = Some(glob_array); } Some(()) From 54e894913104e386edfcf46f169105e05c14c485 Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Sat, 22 Apr 2023 12:51:30 +0800 Subject: [PATCH 05/15] =?UTF-8?q?chore:=20=F0=9F=A4=96=20update=20codegen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/rome_js_unicode_table/src/tables.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/crates/rome_js_unicode_table/src/tables.rs b/crates/rome_js_unicode_table/src/tables.rs index 3ed00b03214..863365853a9 100644 --- a/crates/rome_js_unicode_table/src/tables.rs +++ b/crates/rome_js_unicode_table/src/tables.rs @@ -787,9 +787,7 @@ pub mod derived_property { ('𱍐', '𲎯'), ('\u{e0100}', '\u{e01ef}'), ]; - pub fn ID_Continue(c: char) -> bool { - super::bsearch_range_table(c, ID_Continue_table) - } + pub fn ID_Continue(c: char) -> bool { super::bsearch_range_table(c, ID_Continue_table) } pub const ID_Start_table: &[(char, char)] = &[ ('A', 'Z'), ('a', 'z'), @@ -1451,7 +1449,5 @@ pub mod derived_property { ('𰀀', '𱍊'), ('𱍐', '𲎯'), ]; - pub fn ID_Start(c: char) -> bool { - super::bsearch_range_table(c, ID_Start_table) - } + pub fn ID_Start(c: char) -> bool { super::bsearch_range_table(c, ID_Start_table) } } From 16660c55e598891a2fae0b866d3aeacedbb34f96 Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Thu, 4 May 2023 00:02:26 +0800 Subject: [PATCH 06/15] =?UTF-8?q?chore:=20=F0=9F=A4=96=20pass=20all=20test?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/rome_cli/src/execute/migrate.rs | 3 ++- crates/rome_cli/tests/commands/init.rs | 9 ++++++--- crates/rome_cli/tests/snap_test.rs | 4 ++-- crates/rome_deserialize/src/json.rs | 2 +- crates/rome_json_formatter/src/lib.rs | 6 ++++-- crates/rome_json_formatter/tests/language.rs | 4 ++-- crates/rome_json_formatter/tests/quick_test.rs | 4 ++-- crates/rome_json_parser/src/lib.rs | 16 +++++++++++----- crates/rome_json_parser/src/parser.rs | 9 ++++++++- crates/rome_json_parser/tests/spec_test.rs | 4 ++-- crates/rome_migrate/src/lib.rs | 4 ++-- crates/rome_service/src/configuration/json.rs | 10 ++++++++-- crates/rome_service/src/configuration/mod.rs | 12 +++++------- .../src/configuration/parse/json/json_visitor.rs | 2 +- .../rome_service/src/configuration/string_set.rs | 6 ++++++ crates/rome_service/src/file_handlers/json.rs | 3 ++- xtask/bench/src/language.rs | 12 ++++++++++-- xtask/lintdoc/src/main.rs | 3 ++- 18 files changed, 76 insertions(+), 37 deletions(-) diff --git a/crates/rome_cli/src/execute/migrate.rs b/crates/rome_cli/src/execute/migrate.rs index f612dce9c2d..675b7250375 100644 --- a/crates/rome_cli/src/execute/migrate.rs +++ b/crates/rome_cli/src/execute/migrate.rs @@ -3,6 +3,7 @@ use crate::{CliDiagnostic, CliSession}; use rome_console::{markup, ConsoleExt}; use rome_diagnostics::PrintDiagnostic; use rome_fs::OpenOptions; +use rome_json_parser::JsonParserConfig; use rome_json_syntax::JsonRoot; use rome_migrate::{migrate_configuration, ControlFlow}; use rome_rowan::AstNode; @@ -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, JsonParserConfig::default()); let mut errors = 0; let mut tree = parsed.tree(); let mut actions = Vec::new(); diff --git a/crates/rome_cli/tests/commands/init.rs b/crates/rome_cli/tests/commands/init.rs index e93b0ed8ff3..afecd667597 100644 --- a/crates/rome_cli/tests/commands/init.rs +++ b/crates/rome_cli/tests/commands/init.rs @@ -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, JsonParserConfig}; use rome_service::DynRef; use std::path::Path; @@ -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, JsonParserConfig::default()); let formatted = rome_json_formatter::format_node(JsonFormatOptions::default(), &parsed.syntax()) .expect("valid format document") @@ -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, + JsonParserConfig::default(), + ); let formatted = rome_json_formatter::format_node(JsonFormatOptions::default(), &parsed.syntax()) .expect("valid format document") diff --git a/crates/rome_cli/tests/snap_test.rs b/crates/rome_cli/tests/snap_test.rs index 3807271d174..ac0cf74d7f1 100644 --- a/crates/rome_cli/tests/snap_test.rs +++ b/crates/rome_cli/tests/snap_test.rs @@ -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, JsonParserConfig}; use std::borrow::Cow; use std::collections::BTreeMap; use std::env::{current_exe, temp_dir}; @@ -49,7 +49,7 @@ 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), JsonParserConfig::default()); let formatted = format_node( JsonFormatOptions::default().with_indent_style(IndentStyle::Space(2)), &parsed.syntax(), diff --git a/crates/rome_deserialize/src/json.rs b/crates/rome_deserialize/src/json.rs index b050ba54089..6805df5ea2c 100644 --- a/crates/rome_deserialize/src/json.rs +++ b/crates/rome_deserialize/src/json.rs @@ -491,7 +491,7 @@ where { let mut output = Output::default(); let mut diagnostics = vec![]; - let parse = parse_json(source); + let parse = parse_json(source, rome_json_parser::JsonParserConfig::default()); Output::deserialize_from_ast(parse.tree(), &mut output, &mut diagnostics); let mut errors = parse .into_diagnostics() diff --git a/crates/rome_json_formatter/src/lib.rs b/crates/rome_json_formatter/src/lib.rs index 89070a1cc15..cddc546dc89 100644 --- a/crates/rome_json_formatter/src/lib.rs +++ b/crates/rome_json_formatter/src/lib.rs @@ -290,9 +290,11 @@ pub fn format_sub_tree(options: JsonFormatOptions, root: &JsonSyntaxNode) -> For #[cfg(test)] mod tests { + use std::default; + use crate::context::JsonFormatOptions; use crate::format_node; - use rome_json_parser::parse_json; + use rome_json_parser::{parse_json, JsonParserConfig}; #[test] fn smoke_test() { @@ -305,7 +307,7 @@ mod tests { "e": false } "#; - let parse = parse_json(src); + let parse = parse_json(src, JsonParserConfig::default()); let options = JsonFormatOptions::default(); let formatted = format_node(options, &parse.syntax()).unwrap(); assert_eq!( diff --git a/crates/rome_json_formatter/tests/language.rs b/crates/rome_json_formatter/tests/language.rs index ebbae1fd1b3..410fa000c3b 100644 --- a/crates/rome_json_formatter/tests/language.rs +++ b/crates/rome_json_formatter/tests/language.rs @@ -2,7 +2,7 @@ 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, JsonParserConfig}; use rome_json_syntax::JsonLanguage; use rome_parser::AnyParse; use rome_rowan::{SyntaxNode, TextRange}; @@ -18,7 +18,7 @@ impl TestFormatLanguage for JsonTestFormatLanguage { type FormatLanguage = JsonFormatLanguage; fn parse(&self, text: &str) -> AnyParse { - parse_json(text).into() + parse_json(text, JsonParserConfig::default()).into() } fn deserialize_format_options( diff --git a/crates/rome_json_formatter/tests/quick_test.rs b/crates/rome_json_formatter/tests/quick_test.rs index 7bd0a964782..ff8677caed8 100644 --- a/crates/rome_json_formatter/tests/quick_test.rs +++ b/crates/rome_json_formatter/tests/quick_test.rs @@ -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, JsonParse, JsonParserConfig}; mod language { include!("language.rs"); @@ -27,7 +27,7 @@ fn quick_test() { ] } "#; - let parse = parse_json(src); + let parse = parse_json(src, JsonParserConfig::default()); let options = JsonFormatOptions::default(); let result = format_node(options.clone(), &parse.syntax()) .unwrap() diff --git a/crates/rome_json_parser/src/lib.rs b/crates/rome_json_parser/src/lib.rs index dd3dd14b24d..68507e8afac 100644 --- a/crates/rome_json_parser/src/lib.rs +++ b/crates/rome_json_parser/src/lib.rs @@ -2,6 +2,7 @@ use crate::parser::JsonParser; use crate::syntax::parse_root; +pub use parser::JsonParserConfig; use rome_json_factory::JsonSyntaxFactory; use rome_json_syntax::{JsonLanguage, JsonRoot, JsonSyntaxNode}; pub use rome_parser::prelude::*; @@ -18,15 +19,19 @@ mod token_source; pub(crate) type JsonLosslessTreeSink<'source> = LosslessTreeSink<'source, JsonLanguage, JsonSyntaxFactory>; -pub fn parse_json(source: &str) -> JsonParse { +pub fn parse_json(source: &str, config: JsonParserConfig) -> JsonParse { let mut cache = NodeCache::default(); - parse_json_with_cache(source, &mut cache) + parse_json_with_cache(source, &mut cache, config) } /// Parses the provided string as JSON program using the provided node cache. -pub fn parse_json_with_cache(source: &str, cache: &mut NodeCache) -> JsonParse { +pub fn parse_json_with_cache( + source: &str, + cache: &mut NodeCache, + config: JsonParserConfig, +) -> JsonParse { tracing::debug_span!("parse").in_scope(move || { - let mut parser = JsonParser::new(source); + let mut parser = JsonParser::new(source, config); parse_root(&mut parser); @@ -61,7 +66,8 @@ impl JsonParse { /// /// # fn main() -> Result<(), SyntaxError> { /// use rome_json_syntax::JsonSyntaxKind; - /// let parse = parse_json(r#"["a", 1]"#); + /// use rome_json_parser::JsonParserConfig; + /// let parse = parse_json(r#"["a", 1]"#, JsonParserConfig::default()); /// /// // Get the root value /// let root_value = parse.tree().value()?; diff --git a/crates/rome_json_parser/src/parser.rs b/crates/rome_json_parser/src/parser.rs index 870bc5273df..65c3d522a1b 100644 --- a/crates/rome_json_parser/src/parser.rs +++ b/crates/rome_json_parser/src/parser.rs @@ -9,13 +9,20 @@ use rome_parser::ParserContext; pub(crate) struct JsonParser<'source> { context: ParserContext, source: JsonTokenSource<'source>, + config: JsonParserConfig, +} + +#[derive(Default)] +pub struct JsonParserConfig { + allow_comments: bool, } impl<'source> JsonParser<'source> { - pub fn new(source: &'source str) -> Self { + pub fn new(source: &'source str, config: JsonParserConfig) -> Self { Self { context: ParserContext::default(), source: JsonTokenSource::from_str(source), + config, } } diff --git a/crates/rome_json_parser/tests/spec_test.rs b/crates/rome_json_parser/tests/spec_test.rs index 43486932118..af2a82a21fa 100644 --- a/crates/rome_json_parser/tests/spec_test.rs +++ b/crates/rome_json_parser/tests/spec_test.rs @@ -3,7 +3,7 @@ use rome_console::markup; use rome_diagnostics::display::PrintDiagnostic; use rome_diagnostics::termcolor; use rome_diagnostics::DiagnosticExt; -use rome_json_parser::parse_json; +use rome_json_parser::{parse_json, JsonParserConfig}; use rome_rowan::SyntaxKind; use std::fmt::Write; use std::fs; @@ -35,7 +35,7 @@ pub fn run(test_case: &str, _snapshot_name: &str, test_directory: &str, outcome: let content = fs::read_to_string(test_case_path) .expect("Expected test path to be a readable file in UTF8 encoding"); - let parsed = parse_json(&content); + let parsed = parse_json(&content, JsonParserConfig::default()); let formatted_ast = format!("{:#?}", parsed.tree()); let mut snapshot = String::new(); diff --git a/crates/rome_migrate/src/lib.rs b/crates/rome_migrate/src/lib.rs index 3fa2af4c023..8c8599a4468 100644 --- a/crates/rome_migrate/src/lib.rs +++ b/crates/rome_migrate/src/lib.rs @@ -101,7 +101,7 @@ where mod test { use crate::migrate_configuration; use rome_analyze::{ControlFlow, Never}; - use rome_json_parser::parse_json; + use rome_json_parser::{parse_json, JsonParserConfig}; use std::path::Path; #[test] @@ -109,7 +109,7 @@ mod test { fn smoke() { let source = r#"{ "something": "else" }"#; - let parsed = parse_json(source); + let parsed = parse_json(source, JsonParserConfig::default()); migrate_configuration(&parsed.tree().value().unwrap(), Path::new(""), |signal| { for action in signal.actions() { diff --git a/crates/rome_service/src/configuration/json.rs b/crates/rome_service/src/configuration/json.rs index f16d2c170f4..4dcc10cff84 100644 --- a/crates/rome_service/src/configuration/json.rs +++ b/crates/rome_service/src/configuration/json.rs @@ -1,12 +1,18 @@ +use std::str::FromStr; + +use bpaf::Bpaf; use indexmap::IndexSet; use serde::{Deserialize, Serialize}; +use serde_json::from_str; -#[derive(Default, Debug, Deserialize, Serialize, Eq, PartialEq)] +use super::StringSet; +#[derive(Default, Debug, Deserialize, Serialize, Eq, PartialEq, Clone, Bpaf)] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[serde(default, deny_unknown_fields)] pub struct JsonConfiguration { #[serde(skip_serializing_if = "Option::is_none")] - pub allow_comments: Option>, + #[bpaf(hide)] + pub allow_comments: Option, } impl JsonConfiguration { diff --git a/crates/rome_service/src/configuration/mod.rs b/crates/rome_service/src/configuration/mod.rs index c5b5c1c2c03..bd411477603 100644 --- a/crates/rome_service/src/configuration/mod.rs +++ b/crates/rome_service/src/configuration/mod.rs @@ -16,8 +16,6 @@ pub mod diagnostics; pub mod formatter; mod generated; pub mod javascript; -mod javascript; -mod javascript; mod json; pub mod linter; mod merge; @@ -35,15 +33,14 @@ use crate::configuration::vcs::{vcs_configuration, VcsConfiguration}; use crate::settings::{LanguagesSettings, LinterSettings}; pub use formatter::{formatter_configuration, FormatterConfiguration, PlainIndentStyle}; pub use javascript::{javascript_configuration, JavascriptConfiguration, JavascriptFormatter}; +pub use json::{json_configuration, JsonConfiguration}; pub use linter::{linter_configuration, LinterConfiguration, RuleConfiguration, Rules}; use rome_analyze::{AnalyzerConfiguration, AnalyzerRules}; use rome_deserialize::json::deserialize_from_json_str; use rome_deserialize::Deserialized; use rome_js_analyze::metadata; use rome_json_formatter::context::JsonFormatOptions; -use rome_json_parser::parse_json; - -use self::json::JsonConfiguration; +use rome_json_parser::{parse_json, JsonParserConfig}; /// The configuration that is contained inside the file `rome.json` #[derive(Debug, Deserialize, Serialize, Clone, Bpaf)] @@ -86,8 +83,9 @@ pub struct Configuration { #[bpaf(external(javascript_configuration), optional)] pub javascript: Option, - /// Specific configuration for the JavaScript language + /// Specific configuration for the Json language #[serde(skip_serializing_if = "Option::is_none")] + #[bpaf(external(json_configuration), optional)] pub json: Option, } @@ -368,7 +366,7 @@ pub fn create_config( WorkspaceError::Configuration(ConfigurationDiagnostic::new_serialization_error()) })?; - let parsed = parse_json(&contents); + let parsed = parse_json(&contents, JsonParserConfig::default()); let formatted = rome_json_formatter::format_node(JsonFormatOptions::default(), &parsed.syntax())? .print() diff --git a/crates/rome_service/src/configuration/parse/json/json_visitor.rs b/crates/rome_service/src/configuration/parse/json/json_visitor.rs index a05a615100a..b1d12fc9fe0 100644 --- a/crates/rome_service/src/configuration/parse/json/json_visitor.rs +++ b/crates/rome_service/src/configuration/parse/json/json_visitor.rs @@ -27,7 +27,7 @@ impl VisitNode for JsonConfiguration { if let "allowComments" = name_text { let glob_array = self.map_to_index_set_string(&value, name_text, diagnostics)?; - self.allow_comments = Some(glob_array); + self.allow_comments = Some(glob_array.into()); } Some(()) diff --git a/crates/rome_service/src/configuration/string_set.rs b/crates/rome_service/src/configuration/string_set.rs index 1b09ad073f5..dbd5fd93138 100644 --- a/crates/rome_service/src/configuration/string_set.rs +++ b/crates/rome_service/src/configuration/string_set.rs @@ -104,3 +104,9 @@ impl FromStr for StringSet { Ok(StringSet::default()) } } + +impl From> for StringSet { + fn from(value: IndexSet) -> Self { + Self::new(value) + } +} diff --git a/crates/rome_service/src/file_handlers/json.rs b/crates/rome_service/src/file_handlers/json.rs index 7a891bbbbde..168bc1ea938 100644 --- a/crates/rome_service/src/file_handlers/json.rs +++ b/crates/rome_service/src/file_handlers/json.rs @@ -17,6 +17,7 @@ use rome_formatter::{FormatError, Printed}; use rome_fs::{RomePath, CONFIG_NAME}; use rome_json_formatter::context::JsonFormatOptions; use rome_json_formatter::format_node; +use rome_json_parser::JsonParserConfig; use rome_json_syntax::{JsonLanguage, JsonRoot, JsonSyntaxNode}; use rome_parser::AnyParse; use rome_rowan::{AstNode, NodeCache}; @@ -84,7 +85,7 @@ impl ExtensionHandler for JsonFileHandler { } fn parse(_: &RomePath, _: LanguageId, text: &str, cache: &mut NodeCache) -> AnyParse { - let parse = rome_json_parser::parse_json_with_cache(text, cache); + let parse = rome_json_parser::parse_json_with_cache(text, cache, JsonParserConfig::default()); AnyParse::from(parse) } diff --git a/xtask/bench/src/language.rs b/xtask/bench/src/language.rs index 2a68e478c97..b2b1cfe5974 100644 --- a/xtask/bench/src/language.rs +++ b/xtask/bench/src/language.rs @@ -6,6 +6,7 @@ use rome_js_analyze::analyze; use rome_js_formatter::context::{JsFormatContext, JsFormatOptions}; use rome_js_syntax::{AnyJsRoot, JsSyntaxNode, SourceType}; use rome_json_formatter::context::{JsonFormatContext, JsonFormatOptions}; +use rome_json_parser::JsonParserConfig; use rome_json_syntax::JsonSyntaxNode; use rome_parser::prelude::ParseDiagnostic; use rome_rowan::NodeCache; @@ -31,7 +32,10 @@ impl<'a> Parse<'a> { Parse::JavaScript(source_type, code) => { Parsed::JavaScript(rome_js_parser::parse(code, *source_type), *source_type) } - Parse::Json(code) => Parsed::Json(rome_json_parser::parse_json(code)), + Parse::Json(code) => Parsed::Json(rome_json_parser::parse_json( + code, + JsonParserConfig::default(), + )), } } @@ -41,7 +45,11 @@ impl<'a> Parse<'a> { rome_js_parser::parse_js_with_cache(code, *source_type, cache), *source_type, ), - Parse::Json(code) => Parsed::Json(rome_json_parser::parse_json_with_cache(code, cache)), + Parse::Json(code) => Parsed::Json(rome_json_parser::parse_json_with_cache( + code, + cache, + JsonParserConfig::default(), + )), } } } diff --git a/xtask/lintdoc/src/main.rs b/xtask/lintdoc/src/main.rs index 98043295e5f..12c0d480457 100644 --- a/xtask/lintdoc/src/main.rs +++ b/xtask/lintdoc/src/main.rs @@ -12,6 +12,7 @@ use rome_diagnostics::termcolor::NoColor; use rome_diagnostics::{Diagnostic, DiagnosticExt, PrintDiagnostic}; use rome_js_analyze::{analyze, visit_registry}; use rome_js_syntax::{JsLanguage, Language, LanguageVariant, ModuleKind, SourceType}; +use rome_json_parser::JsonParserConfig; use rome_service::settings::WorkspaceSettings; use std::{ collections::BTreeMap, @@ -595,7 +596,7 @@ fn assert_lint( } } BlockType::Json => { - let parse = rome_json_parser::parse_json(code); + let parse = rome_json_parser::parse_json(code, JsonParserConfig::default()); if parse.has_errors() { for diag in parse.into_diagnostics() { From d857bfbc9f7923d04aa7df9904f4735f98f5037e Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Thu, 4 May 2023 01:02:06 +0800 Subject: [PATCH 07/15] =?UTF-8?q?chore:=20=F0=9F=A4=96=20basic=20finish=20?= =?UTF-8?q?parse=20with=20comments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/rome_json_parser/src/lexer/mod.rs | 29 ++++++++--- crates/rome_json_parser/src/parser.rs | 31 +++++++++-- crates/rome_json_parser/src/token_source.rs | 19 ++++++- .../json_test_suite/allow_comments/basic.json | 1 + .../allow_comments/basic.json.snap | 52 +++++++++++++++++++ crates/rome_json_parser/tests/spec_test.rs | 12 +++-- crates/rome_json_parser/tests/spec_tests.rs | 5 ++ crates/rome_json_syntax/src/lib.rs | 7 +++ crates/rome_service/src/configuration/json.rs | 4 -- 9 files changed, 138 insertions(+), 22 deletions(-) create mode 100644 crates/rome_json_parser/tests/json_test_suite/allow_comments/basic.json create mode 100644 crates/rome_json_parser/tests/json_test_suite/allow_comments/basic.json.snap diff --git a/crates/rome_json_parser/src/lexer/mod.rs b/crates/rome_json_parser/src/lexer/mod.rs index 25fbbaaead3..2c3edd33d73 100644 --- a/crates/rome_json_parser/src/lexer/mod.rs +++ b/crates/rome_json_parser/src/lexer/mod.rs @@ -9,6 +9,8 @@ use rome_parser::diagnostic::ParseDiagnostic; use std::iter::FusedIterator; use std::ops::Add; +use crate::JsonParserConfig; + pub struct Token { kind: JsonSyntaxKind, range: TextRange, @@ -34,6 +36,7 @@ pub(crate) struct Lexer<'src> { position: usize, diagnostics: Vec, + config: JsonParserConfig, } impl<'src> Lexer<'src> { @@ -43,6 +46,7 @@ impl<'src> Lexer<'src> { source: string, position: 0, diagnostics: vec![], + config: JsonParserConfig::default(), } } @@ -695,10 +699,12 @@ impl<'src> Lexer<'src> { b'*' if self.peek_byte() == Some(b'/') => { self.advance(2); - self.diagnostics.push(ParseDiagnostic::new( - "JSON standard does not allow comments.", - start..self.text_position(), - )); + if !self.config.allow_comments { + self.diagnostics.push(ParseDiagnostic::new( + "JSON standard does not allow comments.", + start..self.text_position(), + )); + } if has_newline { return MULTILINE_COMMENT; @@ -739,16 +745,23 @@ impl<'src> Lexer<'src> { } } - self.diagnostics.push(ParseDiagnostic::new( - "JSON standard does not allow comments.", - start..self.text_position(), - )); + if !self.config.allow_comments { + self.diagnostics.push(ParseDiagnostic::new( + "JSON standard does not allow comments.", + start..self.text_position(), + )); + } COMMENT } _ => self.eat_unexpected_character(), } } + + pub(crate) fn with_config(mut self, config: JsonParserConfig) -> Self { + self.config = config; + self + } } impl Iterator for Lexer<'_> { diff --git a/crates/rome_json_parser/src/parser.rs b/crates/rome_json_parser/src/parser.rs index 65c3d522a1b..c5b0e7a79bf 100644 --- a/crates/rome_json_parser/src/parser.rs +++ b/crates/rome_json_parser/src/parser.rs @@ -1,6 +1,6 @@ use crate::token_source::JsonTokenSource; use rome_json_syntax::JsonSyntaxKind; -use rome_parser::diagnostic::merge_diagnostics; +use rome_parser::diagnostic::{expected_token, merge_diagnostics}; use rome_parser::event::Event; use rome_parser::prelude::*; use rome_parser::token_source::Trivia; @@ -12,16 +12,16 @@ pub(crate) struct JsonParser<'source> { config: JsonParserConfig, } -#[derive(Default)] +#[derive(Default, Debug, Clone, Copy)] pub struct JsonParserConfig { - allow_comments: bool, + pub allow_comments: bool, } impl<'source> JsonParser<'source> { pub fn new(source: &'source str, config: JsonParserConfig) -> Self { Self { context: ParserContext::default(), - source: JsonTokenSource::from_str(source), + source: JsonTokenSource::from_str(source, config), config, } } @@ -61,4 +61,27 @@ impl<'source> Parser for JsonParser<'source> { fn source_mut(&mut self) -> &mut Self::Source { &mut self.source } + + /// Try to eat a specific token kind, if the kind is not there then adds an error to the events stack. + fn expect(&mut self, kind: Self::Kind) -> bool { + if self.eat(kind) { + true + } else { + if self.config.allow_comments { + println!("something"); + while matches!( + kind, + JsonSyntaxKind::COMMENT | JsonSyntaxKind::MULTILINE_COMMENT + ) { + self.bump_any(); + } + } + if self.eat(kind) { + return true; + } + + self.error(expected_token(kind)); + false + } + } } diff --git a/crates/rome_json_parser/src/token_source.rs b/crates/rome_json_parser/src/token_source.rs index b2fd0fa8338..f8860bb7f56 100644 --- a/crates/rome_json_parser/src/token_source.rs +++ b/crates/rome_json_parser/src/token_source.rs @@ -1,4 +1,5 @@ use crate::lexer::Lexer; +use crate::JsonParserConfig; use rome_json_syntax::JsonSyntaxKind::{EOF, TOMBSTONE}; use rome_json_syntax::{JsonSyntaxKind, TextRange}; use rome_parser::diagnostic::ParseDiagnostic; @@ -12,11 +13,12 @@ pub(crate) struct JsonTokenSource<'source> { current: JsonSyntaxKind, current_range: TextRange, preceding_line_break: bool, + config: JsonParserConfig, } impl<'source> JsonTokenSource<'source> { - pub fn from_str(source: &'source str) -> Self { - let lexer = Lexer::from_str(source); + pub fn from_str(source: &'source str, config: JsonParserConfig) -> Self { + let lexer = Lexer::from_str(source).with_config(config); let mut source = Self { lexer, @@ -24,6 +26,7 @@ impl<'source> JsonTokenSource<'source> { current: TOMBSTONE, current_range: TextRange::default(), preceding_line_break: false, + config, }; source.next_non_trivia_token(true); @@ -39,6 +42,18 @@ impl<'source> JsonTokenSource<'source> { match trivia_kind { Err(_) => { + if self.config.allow_comments && token.kind().is_comments() { + dbg!(token.kind()); + let trivia_kind = match token.kind() { + JsonSyntaxKind::COMMENT => TriviaPieceKind::SingleLineComment, + JsonSyntaxKind::MULTILINE_COMMENT => TriviaPieceKind::MultiLineComment, + _ => unreachable!(), + }; + self.trivia + .push(Trivia::new(trivia_kind, token.range(), trailing)); + continue; + } + self.current = token.kind(); self.current_range = token.range(); diff --git a/crates/rome_json_parser/tests/json_test_suite/allow_comments/basic.json b/crates/rome_json_parser/tests/json_test_suite/allow_comments/basic.json new file mode 100644 index 00000000000..700cb8e4111 --- /dev/null +++ b/crates/rome_json_parser/tests/json_test_suite/allow_comments/basic.json @@ -0,0 +1 @@ +/*leading*/ [1,true] // trivial diff --git a/crates/rome_json_parser/tests/json_test_suite/allow_comments/basic.json.snap b/crates/rome_json_parser/tests/json_test_suite/allow_comments/basic.json.snap new file mode 100644 index 00000000000..9244bff5c10 --- /dev/null +++ b/crates/rome_json_parser/tests/json_test_suite/allow_comments/basic.json.snap @@ -0,0 +1,52 @@ +--- +source: crates/rome_json_parser/tests/spec_test.rs +expression: snapshot +--- + +## Input + +```json +/*leading*/ [1,true] // trivial + +``` + + +## AST + +``` +JsonRoot { + value: JsonArrayValue { + l_brack_token: L_BRACK@0..13 "[" [Comments("/*leading*/"), Whitespace(" ")] [], + elements: JsonArrayElementList [ + JsonNumberValue { + value_token: JSON_NUMBER_LITERAL@13..14 "1" [] [], + }, + COMMA@14..15 "," [] [], + JsonBooleanValue { + value_token: TRUE_KW@15..19 "true" [] [], + }, + ], + r_brack_token: R_BRACK@19..31 "]" [] [Whitespace(" "), Comments("// trivial")], + }, + eof_token: EOF@31..32 "" [Newline("\n")] [], +} +``` + +## CST + +``` +0: JSON_ROOT@0..32 + 0: JSON_ARRAY_VALUE@0..31 + 0: L_BRACK@0..13 "[" [Comments("/*leading*/"), Whitespace(" ")] [] + 1: JSON_ARRAY_ELEMENT_LIST@13..19 + 0: JSON_NUMBER_VALUE@13..14 + 0: JSON_NUMBER_LITERAL@13..14 "1" [] [] + 1: COMMA@14..15 "," [] [] + 2: JSON_BOOLEAN_VALUE@15..19 + 0: TRUE_KW@15..19 "true" [] [] + 2: R_BRACK@19..31 "]" [] [Whitespace(" "), Comments("// trivial")] + 1: EOF@31..32 "" [Newline("\n")] [] + +``` + + diff --git a/crates/rome_json_parser/tests/spec_test.rs b/crates/rome_json_parser/tests/spec_test.rs index af2a82a21fa..253dbd90c29 100644 --- a/crates/rome_json_parser/tests/spec_test.rs +++ b/crates/rome_json_parser/tests/spec_test.rs @@ -16,12 +16,13 @@ pub enum ExpectedOutcome { Undefined, } -pub fn run(test_case: &str, _snapshot_name: &str, test_directory: &str, outcome: &str) { - let outcome = match outcome { +pub fn run(test_case: &str, _snapshot_name: &str, test_directory: &str, outcome_str: &str) { + let outcome = match outcome_str { "ok" => ExpectedOutcome::Pass, "error" => ExpectedOutcome::Fail, "undefined" => ExpectedOutcome::Undefined, - _ => panic!("Invalid expected outcome {outcome}"), + "allow_comments" => ExpectedOutcome::Pass, + _ => panic!("Invalid expected outcome {outcome_str}"), }; let test_case_path = Path::new(test_case); @@ -35,7 +36,10 @@ pub fn run(test_case: &str, _snapshot_name: &str, test_directory: &str, outcome: let content = fs::read_to_string(test_case_path) .expect("Expected test path to be a readable file in UTF8 encoding"); - let parsed = parse_json(&content, JsonParserConfig::default()); + let parse_conifg = JsonParserConfig { + allow_comments: outcome_str == "allow_comments", + }; + let parsed = parse_json(&content, parse_conifg); let formatted_ast = format!("{:#?}", parsed.tree()); let mut snapshot = String::new(); diff --git a/crates/rome_json_parser/tests/spec_tests.rs b/crates/rome_json_parser/tests/spec_tests.rs index 27cc3c11d4f..e7322bdcdf7 100644 --- a/crates/rome_json_parser/tests/spec_tests.rs +++ b/crates/rome_json_parser/tests/spec_tests.rs @@ -16,3 +16,8 @@ mod undefined { //! parsers are free to accept or reject content tests_macros::gen_tests! {"tests/json_test_suite/undefined/*.json", crate::spec_test::run, "undefined"} } + +mod allow_comments { + //! Tests should pass even with comments in json + tests_macros::gen_tests! {"tests/json_test_suite/allow_comments/*.json", crate::spec_test::run, "allow_comments"} +} diff --git a/crates/rome_json_syntax/src/lib.rs b/crates/rome_json_syntax/src/lib.rs index abbd2ffbb18..f4bcc791c1a 100644 --- a/crates/rome_json_syntax/src/lib.rs +++ b/crates/rome_json_syntax/src/lib.rs @@ -28,6 +28,13 @@ impl JsonSyntaxKind { matches!(self, JsonSyntaxKind::NEWLINE | JsonSyntaxKind::WHITESPACE) } + pub fn is_comments(self) -> bool { + matches!( + self, + JsonSyntaxKind::COMMENT | JsonSyntaxKind::MULTILINE_COMMENT + ) + } + #[inline] pub const fn is_keyword(self) -> bool { matches!(self, T![null] | T![true] | T![false]) diff --git a/crates/rome_service/src/configuration/json.rs b/crates/rome_service/src/configuration/json.rs index 4dcc10cff84..3532d502d5b 100644 --- a/crates/rome_service/src/configuration/json.rs +++ b/crates/rome_service/src/configuration/json.rs @@ -1,9 +1,5 @@ -use std::str::FromStr; - use bpaf::Bpaf; -use indexmap::IndexSet; use serde::{Deserialize, Serialize}; -use serde_json::from_str; use super::StringSet; #[derive(Default, Debug, Deserialize, Serialize, Eq, PartialEq, Clone, Bpaf)] From b1d7e748e72ac925f0c9dc71a5c9bb2f85821fa9 Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Thu, 4 May 2023 22:07:33 +0800 Subject: [PATCH 08/15] =?UTF-8?q?chore:=20=F0=9F=A4=96=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/rome_js_unicode_table/src/tables.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/rome_js_unicode_table/src/tables.rs b/crates/rome_js_unicode_table/src/tables.rs index 863365853a9..3ed00b03214 100644 --- a/crates/rome_js_unicode_table/src/tables.rs +++ b/crates/rome_js_unicode_table/src/tables.rs @@ -787,7 +787,9 @@ pub mod derived_property { ('𱍐', '𲎯'), ('\u{e0100}', '\u{e01ef}'), ]; - pub fn ID_Continue(c: char) -> bool { super::bsearch_range_table(c, ID_Continue_table) } + pub fn ID_Continue(c: char) -> bool { + super::bsearch_range_table(c, ID_Continue_table) + } pub const ID_Start_table: &[(char, char)] = &[ ('A', 'Z'), ('a', 'z'), @@ -1449,5 +1451,7 @@ pub mod derived_property { ('𰀀', '𱍊'), ('𱍐', '𲎯'), ]; - pub fn ID_Start(c: char) -> bool { super::bsearch_range_table(c, ID_Start_table) } + pub fn ID_Start(c: char) -> bool { + super::bsearch_range_table(c, ID_Start_table) + } } From ab690e1f6e9877c2c17a330095faf4e9b2a13892 Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Thu, 4 May 2023 22:18:06 +0800 Subject: [PATCH 09/15] =?UTF-8?q?chore:=20=F0=9F=A4=96=20lint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/rome_json_formatter/src/lib.rs | 1 - crates/rome_json_formatter/tests/quick_test.rs | 2 +- crates/rome_json_parser/src/parser.rs | 3 +-- crates/rome_json_parser/src/token_source.rs | 1 - 4 files changed, 2 insertions(+), 5 deletions(-) diff --git a/crates/rome_json_formatter/src/lib.rs b/crates/rome_json_formatter/src/lib.rs index cddc546dc89..cab99766f4a 100644 --- a/crates/rome_json_formatter/src/lib.rs +++ b/crates/rome_json_formatter/src/lib.rs @@ -290,7 +290,6 @@ pub fn format_sub_tree(options: JsonFormatOptions, root: &JsonSyntaxNode) -> For #[cfg(test)] mod tests { - use std::default; use crate::context::JsonFormatOptions; use crate::format_node; diff --git a/crates/rome_json_formatter/tests/quick_test.rs b/crates/rome_json_formatter/tests/quick_test.rs index ff8677caed8..1a2e9474a9e 100644 --- a/crates/rome_json_formatter/tests/quick_test.rs +++ b/crates/rome_json_formatter/tests/quick_test.rs @@ -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, JsonParse, JsonParserConfig}; +use rome_json_parser::{parse_json, JsonParserConfig}; mod language { include!("language.rs"); diff --git a/crates/rome_json_parser/src/parser.rs b/crates/rome_json_parser/src/parser.rs index c5b0e7a79bf..0b8e306a7c8 100644 --- a/crates/rome_json_parser/src/parser.rs +++ b/crates/rome_json_parser/src/parser.rs @@ -68,9 +68,8 @@ impl<'source> Parser for JsonParser<'source> { true } else { if self.config.allow_comments { - println!("something"); while matches!( - kind, + self.cur(), JsonSyntaxKind::COMMENT | JsonSyntaxKind::MULTILINE_COMMENT ) { self.bump_any(); diff --git a/crates/rome_json_parser/src/token_source.rs b/crates/rome_json_parser/src/token_source.rs index f8860bb7f56..288cbee6dcd 100644 --- a/crates/rome_json_parser/src/token_source.rs +++ b/crates/rome_json_parser/src/token_source.rs @@ -43,7 +43,6 @@ impl<'source> JsonTokenSource<'source> { match trivia_kind { Err(_) => { if self.config.allow_comments && token.kind().is_comments() { - dbg!(token.kind()); let trivia_kind = match token.kind() { JsonSyntaxKind::COMMENT => TriviaPieceKind::SingleLineComment, JsonSyntaxKind::MULTILINE_COMMENT => TriviaPieceKind::MultiLineComment, From f4cedfa1a0c7836f97aa81e133c35c9204f9980b Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Fri, 5 May 2023 01:00:42 +0800 Subject: [PATCH 10/15] =?UTF-8?q?fix:=20=F0=9F=90=9B=20compile=20erorr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xtask/codegen/src/generate_schema.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xtask/codegen/src/generate_schema.rs b/xtask/codegen/src/generate_schema.rs index 79e4e9fdceb..16c5f70f02a 100644 --- a/xtask/codegen/src/generate_schema.rs +++ b/xtask/codegen/src/generate_schema.rs @@ -1,5 +1,5 @@ use rome_json_formatter::context::JsonFormatOptions; -use rome_json_parser::parse_json; +use rome_json_parser::{parse_json, JsonParserConfig}; use rome_service::Configuration; use schemars::schema_for; use serde_json::to_string; @@ -13,7 +13,7 @@ pub(crate) fn generate_configuration_schema(mode: Mode) -> Result<()> { let schema = schema_for!(Configuration); let json_schema = to_string(&schema)?; - let parsed = parse_json(&json_schema); + let parsed = parse_json(&json_schema, JsonParserConfig::default()); let formatted = rome_json_formatter::format_node(JsonFormatOptions::default(), &parsed.syntax()) .unwrap() From 0abca9faa4b8338a1f7596110c68d431ee158ce0 Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Sat, 6 May 2023 00:26:47 +0800 Subject: [PATCH 11/15] =?UTF-8?q?chore:=20=F0=9F=A4=96=20add=20matcher?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 2 ++ crates/rome_cli/Cargo.toml | 1 + crates/rome_service/Cargo.toml | 1 + .../src/file_handlers/javascript.rs | 3 ++- crates/rome_service/src/file_handlers/json.rs | 24 +++++++++++++++---- crates/rome_service/src/file_handlers/mod.rs | 2 +- crates/rome_service/src/settings.rs | 4 ++++ crates/rome_service/src/workspace/server.rs | 2 ++ 8 files changed, 33 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e7527eb085f..416bf54a045 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1680,6 +1680,7 @@ dependencies = [ "bpaf", "crossbeam", "dashmap", + "globset", "hdrhistogram", "indexmap", "insta", @@ -2122,6 +2123,7 @@ version = "0.0.0" dependencies = [ "bpaf", "dashmap", + "globset", "indexmap", "insta", "rome_analyze", diff --git a/crates/rome_cli/Cargo.toml b/crates/rome_cli/Cargo.toml index f2d4fdbb798..ee6f03a8eef 100644 --- a/crates/rome_cli/Cargo.toml +++ b/crates/rome_cli/Cargo.toml @@ -43,6 +43,7 @@ rome_json_syntax = { path = "../rome_json_syntax" } rome_migrate = { path = "../rome_migrate" } rome_rowan = { path = "../rome_rowan" } indexmap = { workspace = true } +globset = "0.4.10" [target.'cfg(unix)'.dependencies] libc = "0.2.127" diff --git a/crates/rome_service/Cargo.toml b/crates/rome_service/Cargo.toml index bc83dc3ae6c..00b7223f94b 100644 --- a/crates/rome_service/Cargo.toml +++ b/crates/rome_service/Cargo.toml @@ -35,6 +35,7 @@ indexmap = { workspace = true, features = ["serde"] } schemars = { version = "0.8.10", features = ["indexmap1"], optional = true } tracing = { workspace = true, features = ["attributes"] } bpaf = { workspace = true } +globset = "0.4.10" [features] schemars = ["dep:schemars", "rome_formatter/serde", "rome_js_factory", "rome_text_edit/schemars"] diff --git a/crates/rome_service/src/file_handlers/javascript.rs b/crates/rome_service/src/file_handlers/javascript.rs index 6d7e41a1a97..d9ce86a132a 100644 --- a/crates/rome_service/src/file_handlers/javascript.rs +++ b/crates/rome_service/src/file_handlers/javascript.rs @@ -64,7 +64,7 @@ impl Language for JsLanguage { type LinterSettings = JsLinterSettings; type FormatOptions = JsFormatOptions; type OrganizeImportsSettings = JsOrganizeImportsSettings; - + type AllowCommentsOptions = (); fn lookup_settings(languages: &LanguagesSettings) -> &LanguageSettings { &languages.javascript } @@ -129,6 +129,7 @@ fn parse( language_hint: LanguageId, text: &str, cache: &mut NodeCache, + _: SettingsHandle, ) -> AnyParse { let source_type = SourceType::try_from(rome_path.as_path()).unwrap_or_else(|_| match language_hint { diff --git a/crates/rome_service/src/file_handlers/json.rs b/crates/rome_service/src/file_handlers/json.rs index 168bc1ea938..bb4ed987d8c 100644 --- a/crates/rome_service/src/file_handlers/json.rs +++ b/crates/rome_service/src/file_handlers/json.rs @@ -10,7 +10,7 @@ use crate::settings::{ use crate::workspace::{ FixFileResult, GetSyntaxTreeResult, OrganizeImportsResult, PullActionsResult, }; -use crate::{Configuration, Rules, WorkspaceError}; +use crate::{Configuration, Matcher, Rules, WorkspaceError}; use rome_deserialize::json::deserialize_from_json_ast; use rome_diagnostics::{Diagnostic, Severity}; use rome_formatter::{FormatError, Printed}; @@ -28,7 +28,7 @@ impl Language for JsonLanguage { type LinterSettings = (); type FormatOptions = JsonFormatOptions; type OrganizeImportsSettings = (); - + type AllowCommentsOptions = Option; fn lookup_settings(language: &LanguagesSettings) -> &LanguageSettings { &language.json } @@ -84,8 +84,24 @@ impl ExtensionHandler for JsonFileHandler { } } -fn parse(_: &RomePath, _: LanguageId, text: &str, cache: &mut NodeCache) -> AnyParse { - let parse = rome_json_parser::parse_json_with_cache(text, cache, JsonParserConfig::default()); +fn parse( + path: &RomePath, + _: LanguageId, + text: &str, + cache: &mut NodeCache, + settings: SettingsHandle, +) -> AnyParse { + let allow_comment_matcher = &settings.as_ref().languages.json.allow_comments; + let parse = rome_json_parser::parse_json_with_cache( + text, + cache, + JsonParserConfig { + allow_comments: allow_comment_matcher + .as_ref() + .map(|matcher| matcher.matches_path(&*path)) + .unwrap_or(false), + }, + ); AnyParse::from(parse) } diff --git a/crates/rome_service/src/file_handlers/mod.rs b/crates/rome_service/src/file_handlers/mod.rs index f91a998fa37..6ae7cd1d6db 100644 --- a/crates/rome_service/src/file_handlers/mod.rs +++ b/crates/rome_service/src/file_handlers/mod.rs @@ -159,7 +159,7 @@ pub(crate) struct Capabilities { pub(crate) formatter: FormatterCapabilities, } -type Parse = fn(&RomePath, Language, &str, &mut NodeCache) -> AnyParse; +type Parse = fn(&RomePath, Language, &str, &mut NodeCache, SettingsHandle) -> AnyParse; #[derive(Default)] pub(crate) struct ParserCapabilities { diff --git a/crates/rome_service/src/settings.rs b/crates/rome_service/src/settings.rs index 5017e049009..09bb2d94c36 100644 --- a/crates/rome_service/src/settings.rs +++ b/crates/rome_service/src/settings.rs @@ -206,6 +206,8 @@ pub trait Language: rome_rowan::Language { /// Fully resolved formatter options type for this language type FormatOptions: rome_formatter::FormatOptions; + type AllowCommentsOptions: Default; + /// Read the settings type for this language from the [LanguagesSettings] map fn lookup_settings(languages: &LanguagesSettings) -> &LanguageSettings; @@ -231,6 +233,8 @@ pub struct LanguageSettings { /// Organize imports settings for this language pub organize_imports: L::OrganizeImportsSettings, + + pub allow_comments: L::AllowCommentsOptions, } /// Filesystem settings for the entire workspace diff --git a/crates/rome_service/src/workspace/server.rs b/crates/rome_service/src/workspace/server.rs index c358067cad0..2fa3f0e9e8c 100644 --- a/crates/rome_service/src/workspace/server.rs +++ b/crates/rome_service/src/workspace/server.rs @@ -125,6 +125,7 @@ impl WorkspaceServer { &self, rome_path: RomePath, feature: Option, + // settings: SettingsHandle, ) -> Result { let ignored = if let Some(feature) = feature { self.is_path_ignored(IsPathIgnoredParams { @@ -180,6 +181,7 @@ impl WorkspaceServer { document.language_hint, document.content.as_str(), &mut document.node_cache, + self.settings(), ); Ok(entry.insert(parsed).clone()) From b000dd18b1ba9d12ac075eed85c63d26360d8865 Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Sat, 6 May 2023 00:54:58 +0800 Subject: [PATCH 12/15] =?UTF-8?q?chore:=20=F0=9F=A4=96=20add=20extra=20tes?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/rome_cli/tests/commands/format.rs | 39 ++++++++++ ...re_comments_error_when_allow_comments.snap | 32 +++++++++ ...omments_error_when_allow_comments.snap.new | 71 +++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 crates/rome_cli/tests/snapshots/main_commands_format/ignore_comments_error_when_allow_comments.snap create mode 100644 crates/rome_cli/tests/snapshots/main_commands_format/ignore_comments_error_when_allow_comments.snap.new diff --git a/crates/rome_cli/tests/commands/format.rs b/crates/rome_cli/tests/commands/format.rs index 0b5ac88e3fe..a245c95d5af 100644 --- a/crates/rome_cli/tests/commands/format.rs +++ b/crates/rome_cli/tests/commands/format.rs @@ -1608,3 +1608,42 @@ file2.js result, )); } + +#[test] +fn ignore_comments_error_when_allow_comments() { + let mut fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let config_json = r#" + +{ + "json": { + "allow_comments": ["*.json"] + } +} + + "#; + let rome_config = "rome.json"; + let code = r#" +[] + "#; + 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()]), + ); + + assert!(result.is_ok(), "run_cli returned {result:?}"); + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "ignore_comments_error_when_allow_comments", + fs, + console, + result, + )); +} diff --git a/crates/rome_cli/tests/snapshots/main_commands_format/ignore_comments_error_when_allow_comments.snap b/crates/rome_cli/tests/snapshots/main_commands_format/ignore_comments_error_when_allow_comments.snap new file mode 100644 index 00000000000..abd731f9fb4 --- /dev/null +++ b/crates/rome_cli/tests/snapshots/main_commands_format/ignore_comments_error_when_allow_comments.snap @@ -0,0 +1,32 @@ +--- +source: crates/rome_cli/tests/snap_test.rs +expression: content +--- +## `format.json` + +```json + +[] + +``` + +# Emitted Messages + +```block +format.json format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + i Formatter would have printed the following content: + + 1 │ - + 2 1 │ [] + 3 │ - → + 2 │ + + + +``` + +```block +Compared 1 file(s) in