From 6c28d0953f10cf3ee392d798e8c08183a7d18f10 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Thu, 15 Jun 2023 12:19:40 +0300 Subject: [PATCH 1/3] feat(rome_service): add parser option for unsafe decorators --- .../src/configuration/javascript.rs | 34 +++++++++++++++++++ .../configuration/parse/json/javascript.rs | 32 ++++++++++++++++- editors/vscode/configuration_schema.json | 21 +++++++++--- npm/backend-jsonrpc/src/workspace.ts | 9 +++++ npm/rome/configuration_schema.json | 21 +++++++++--- 5 files changed, 106 insertions(+), 11 deletions(-) diff --git a/crates/rome_service/src/configuration/javascript.rs b/crates/rome_service/src/configuration/javascript.rs index a1e0748a5ef..ec8b2530650 100644 --- a/crates/rome_service/src/configuration/javascript.rs +++ b/crates/rome_service/src/configuration/javascript.rs @@ -14,6 +14,10 @@ pub struct JavascriptConfiguration { #[bpaf(external(javascript_formatter), optional)] pub formatter: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[bpaf(external(javascript_parser), optional)] + pub parser: Option, + /// A list of global bindings that should be ignored by the analyzers /// /// If defined here, they should not emit diagnostics. @@ -66,18 +70,23 @@ impl JavascriptConfiguration { pub struct JavascriptFormatter { /// The style for quotes. Defaults to double. #[bpaf(long("quote-style"), argument("double|single"), optional)] + #[serde(skip_serializing_if = "Option::is_none")] pub quote_style: Option, /// The style for JSX quotes. Defaults to double. #[bpaf(long("jsx-quote-style"), argument("double|single"), optional)] + #[serde(skip_serializing_if = "Option::is_none")] pub jsx_quote_style: Option, /// When properties in objects are quoted. Defaults to asNeeded. #[bpaf(long("quote-properties"), argument("preserve|as-needed"), optional)] + #[serde(skip_serializing_if = "Option::is_none")] pub quote_properties: Option, /// Print trailing commas wherever possible in multi-line comma-separated syntactic structures. Defaults to "all". #[bpaf(long("trailing-comma"), argument("all|es5|none"), optional)] + #[serde(skip_serializing_if = "Option::is_none")] pub trailing_comma: Option, /// Whether the formatter prints semicolons for all statements or only in for statements where it is necessary because of ASI. #[bpaf(long("semicolons"), argument("always|as-needed"), optional)] + #[serde(skip_serializing_if = "Option::is_none")] pub semicolons: Option, } @@ -115,3 +124,28 @@ impl MergeWith for JavascriptFormatter { #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[serde(default, deny_unknown_fields)] pub struct JavascriptOrganizeImports {} + +#[derive(Default, Debug, Deserialize, Serialize, Eq, PartialEq, Clone, Bpaf)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[serde(rename_all = "camelCase", default, deny_unknown_fields)] +pub struct JavascriptParser { + #[bpaf(hide)] + #[serde(skip_serializing_if = "Option::is_none")] + /// It enables the experimental and unsafe parsing of parameter decorators + /// + /// These decorators belong to an old proposal, and they are subject to change. + pub unsafe_parameter_decorators_enabled: Option, +} + +impl JavascriptParser { + pub(crate) const KNOWN_KEYS: &'static [&'static str] = &["unsafeParameterDecoratorsEnabled"]; +} + +impl MergeWith for JavascriptParser { + fn merge_with(&mut self, other: JavascriptParser) { + if let Some(unsafe_parameter_decorators_enabled) = other.unsafe_parameter_decorators_enabled + { + self.unsafe_parameter_decorators_enabled = Some(unsafe_parameter_decorators_enabled); + } + } +} diff --git a/crates/rome_service/src/configuration/parse/json/javascript.rs b/crates/rome_service/src/configuration/parse/json/javascript.rs index a60aa17cccd..a0a86c27a29 100644 --- a/crates/rome_service/src/configuration/parse/json/javascript.rs +++ b/crates/rome_service/src/configuration/parse/json/javascript.rs @@ -1,4 +1,4 @@ -use crate::configuration::javascript::JavascriptOrganizeImports; +use crate::configuration::javascript::{JavascriptOrganizeImports, JavascriptParser}; use crate::configuration::string_set::StringSet; use crate::configuration::{JavascriptConfiguration, JavascriptFormatter}; use rome_deserialize::json::{has_only_known_keys, VisitJsonNode}; @@ -118,3 +118,33 @@ impl VisitNode for JavascriptOrganizeImports { Some(()) } } + +impl VisitJsonNode for JavascriptParser {} +impl VisitNode for JavascriptParser { + fn visit_member_name( + &mut self, + node: &JsonSyntaxNode, + diagnostics: &mut Vec, + ) -> Option<()> { + has_only_known_keys(node, JavascriptParser::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 { + "unsafeParameterDecoratorsEnabled" => { + self.unsafe_parameter_decorators_enabled = + self.map_to_boolean(&value, name_text, diagnostics); + } + _ => {} + } + + Some(()) + } +} diff --git a/editors/vscode/configuration_schema.json b/editors/vscode/configuration_schema.json index a52b19a56ee..fc33f5ea3c3 100644 --- a/editors/vscode/configuration_schema.json +++ b/editors/vscode/configuration_schema.json @@ -622,6 +622,12 @@ { "$ref": "#/definitions/JavascriptOrganizeImports" }, { "type": "null" } ] + }, + "parser": { + "anyOf": [ + { "$ref": "#/definitions/JavascriptParser" }, + { "type": "null" } + ] } }, "additionalProperties": false @@ -631,12 +637,10 @@ "properties": { "jsxQuoteStyle": { "description": "The style for JSX quotes. Defaults to double.", - "default": null, "anyOf": [{ "$ref": "#/definitions/QuoteStyle" }, { "type": "null" }] }, "quoteProperties": { "description": "When properties in objects are quoted. Defaults to asNeeded.", - "default": null, "anyOf": [ { "$ref": "#/definitions/QuoteProperties" }, { "type": "null" } @@ -644,17 +648,14 @@ }, "quoteStyle": { "description": "The style for quotes. Defaults to double.", - "default": null, "anyOf": [{ "$ref": "#/definitions/QuoteStyle" }, { "type": "null" }] }, "semicolons": { "description": "Whether the formatter prints semicolons for all statements or only in for statements where it is necessary because of ASI.", - "default": null, "anyOf": [{ "$ref": "#/definitions/Semicolons" }, { "type": "null" }] }, "trailingComma": { "description": "Print trailing commas wherever possible in multi-line comma-separated syntactic structures. Defaults to \"all\".", - "default": null, "anyOf": [ { "$ref": "#/definitions/TrailingComma" }, { "type": "null" } @@ -667,6 +668,16 @@ "type": "object", "additionalProperties": false }, + "JavascriptParser": { + "type": "object", + "properties": { + "unsafeParameterDecoratorsEnabled": { + "description": "It enables the experimental and unsafe parsing of parameter decorators\n\nThese decorators belong to an old proposal, and they are subject to change.", + "type": ["boolean", "null"] + } + }, + "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/backend-jsonrpc/src/workspace.ts b/npm/backend-jsonrpc/src/workspace.ts index 84b415bc8eb..a15266ec395 100644 --- a/npm/backend-jsonrpc/src/workspace.ts +++ b/npm/backend-jsonrpc/src/workspace.ts @@ -104,6 +104,7 @@ If defined here, they should not emit diagnostics. */ globals?: StringSet; organize_imports?: JavascriptOrganizeImports; + parser?: JavascriptParser; } export interface LinterConfiguration { /** @@ -183,6 +184,14 @@ export interface JavascriptFormatter { trailingComma?: TrailingComma; } export interface JavascriptOrganizeImports {} +export interface JavascriptParser { + /** + * It enables the experimental and unsafe parsing of parameter decorators + +These decorators belong to an old proposal, and they are subject to change. + */ + unsafeParameterDecoratorsEnabled?: boolean; +} export interface Rules { a11y?: A11y; /** diff --git a/npm/rome/configuration_schema.json b/npm/rome/configuration_schema.json index a52b19a56ee..fc33f5ea3c3 100644 --- a/npm/rome/configuration_schema.json +++ b/npm/rome/configuration_schema.json @@ -622,6 +622,12 @@ { "$ref": "#/definitions/JavascriptOrganizeImports" }, { "type": "null" } ] + }, + "parser": { + "anyOf": [ + { "$ref": "#/definitions/JavascriptParser" }, + { "type": "null" } + ] } }, "additionalProperties": false @@ -631,12 +637,10 @@ "properties": { "jsxQuoteStyle": { "description": "The style for JSX quotes. Defaults to double.", - "default": null, "anyOf": [{ "$ref": "#/definitions/QuoteStyle" }, { "type": "null" }] }, "quoteProperties": { "description": "When properties in objects are quoted. Defaults to asNeeded.", - "default": null, "anyOf": [ { "$ref": "#/definitions/QuoteProperties" }, { "type": "null" } @@ -644,17 +648,14 @@ }, "quoteStyle": { "description": "The style for quotes. Defaults to double.", - "default": null, "anyOf": [{ "$ref": "#/definitions/QuoteStyle" }, { "type": "null" }] }, "semicolons": { "description": "Whether the formatter prints semicolons for all statements or only in for statements where it is necessary because of ASI.", - "default": null, "anyOf": [{ "$ref": "#/definitions/Semicolons" }, { "type": "null" }] }, "trailingComma": { "description": "Print trailing commas wherever possible in multi-line comma-separated syntactic structures. Defaults to \"all\".", - "default": null, "anyOf": [ { "$ref": "#/definitions/TrailingComma" }, { "type": "null" } @@ -667,6 +668,16 @@ "type": "object", "additionalProperties": false }, + "JavascriptParser": { + "type": "object", + "properties": { + "unsafeParameterDecoratorsEnabled": { + "description": "It enables the experimental and unsafe parsing of parameter decorators\n\nThese decorators belong to an old proposal, and they are subject to change.", + "type": ["boolean", "null"] + } + }, + "additionalProperties": false + }, "LineWidth": { "description": "Validated value for the `line_width` formatter options\n\nThe allowed range of values is 1..=320", "type": "integer", From e0416a64d97923995c90b3b0447e33f4cd8a1232 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Thu, 15 Jun 2023 12:23:01 +0300 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c3ac703ec4..3bd6ff6feac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,19 @@ ``` Doing so, Rome won't emit diagnostics for file that it doesn't know how to handle. +- Add a new `"javascript"` option to support the usafe/experimental +parameter decorators: + + ```json + { + "javascript": { + "parser": { + "unsafeParameterDecoratorsEnabled": true + } + } + } + ``` + ### Editors ### Formatter From 8decfffca9fd235f9625ee37e36177c8f9a7d257 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Thu, 15 Jun 2023 12:56:35 +0300 Subject: [PATCH 3/3] clippy --- .../src/configuration/parse/json/javascript.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/crates/rome_service/src/configuration/parse/json/javascript.rs b/crates/rome_service/src/configuration/parse/json/javascript.rs index a0a86c27a29..3a75e9b4faa 100644 --- a/crates/rome_service/src/configuration/parse/json/javascript.rs +++ b/crates/rome_service/src/configuration/parse/json/javascript.rs @@ -137,12 +137,9 @@ impl VisitNode for JavascriptParser { ) -> Option<()> { let (name, value) = self.get_key_and_value(key, value, diagnostics)?; let name_text = name.text(); - match name_text { - "unsafeParameterDecoratorsEnabled" => { - self.unsafe_parameter_decorators_enabled = - self.map_to_boolean(&value, name_text, diagnostics); - } - _ => {} + if name_text == "unsafeParameterDecoratorsEnabled" { + self.unsafe_parameter_decorators_enabled = + self.map_to_boolean(&value, name_text, diagnostics); } Some(())