diff --git a/Cargo.lock b/Cargo.lock index 1d29440b4d8..bb04e1cd194 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1780,6 +1780,7 @@ dependencies = [ "rome_json_parser", "rome_json_syntax", "rome_rowan", + "schemars", "serde", "serde_json", "tracing", diff --git a/crates/rome_deserialize/Cargo.toml b/crates/rome_deserialize/Cargo.toml index 8e668e491dc..49e3cc21237 100644 --- a/crates/rome_deserialize/Cargo.toml +++ b/crates/rome_deserialize/Cargo.toml @@ -9,12 +9,16 @@ version = "0.0.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -indexmap = { workspace = true } +indexmap = { workspace = true, features = ["serde"] } rome_console = { workspace = true } rome_diagnostics = { workspace = true } rome_json_parser = { workspace = true } rome_json_syntax = { workspace = true } rome_rowan = { workspace = true } +schemars = { workspace = true, optional = true } serde = { workspace = true } serde_json = { workspace = true } tracing = { workspace = true } + +[features] +schema = ["schemars", "schemars/indexmap"] diff --git a/crates/rome_deserialize/src/lib.rs b/crates/rome_deserialize/src/lib.rs index 1765f3706d2..36d522ab3c1 100644 --- a/crates/rome_deserialize/src/lib.rs +++ b/crates/rome_deserialize/src/lib.rs @@ -2,8 +2,11 @@ mod diagnostics; mod visitor; pub mod json; +mod string_set; + pub use diagnostics::{DeserializationAdvice, DeserializationDiagnostic}; use rome_diagnostics::Error; +pub use string_set::{deserialize_string_set, serialize_string_set, StringSet}; pub use visitor::VisitNode; /// A small type to interrogate the result of a JSON deserialization diff --git a/crates/rome_service/src/configuration/string_set.rs b/crates/rome_deserialize/src/string_set.rs similarity index 97% rename from crates/rome_service/src/configuration/string_set.rs rename to crates/rome_deserialize/src/string_set.rs index dbd5fd93138..489da594021 100644 --- a/crates/rome_service/src/configuration/string_set.rs +++ b/crates/rome_deserialize/src/string_set.rs @@ -6,7 +6,7 @@ use std::marker::PhantomData; use std::str::FromStr; #[derive(Default, Debug, Deserialize, Serialize, Clone, Eq, PartialEq)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct StringSet( #[serde( diff --git a/crates/rome_js_analyze/Cargo.toml b/crates/rome_js_analyze/Cargo.toml index bd5d8997506..1f6c1d14618 100644 --- a/crates/rome_js_analyze/Cargo.toml +++ b/crates/rome_js_analyze/Cargo.toml @@ -42,4 +42,4 @@ similar = "2.1.0" tests_macros = { workspace = true } [features] -schemars = ["dep:schemars"] +schema = ["schemars", "rome_deserialize/schema"] diff --git a/crates/rome_js_analyze/src/analyzers/nursery/no_excessive_complexity.rs b/crates/rome_js_analyze/src/analyzers/nursery/no_excessive_complexity.rs index 1b34da901d9..ad07dea9556 100644 --- a/crates/rome_js_analyze/src/analyzers/nursery/no_excessive_complexity.rs +++ b/crates/rome_js_analyze/src/analyzers/nursery/no_excessive_complexity.rs @@ -369,7 +369,7 @@ pub struct ComplexityScore { /// Options for the rule `noNestedModuleImports`. #[derive(Deserialize, Serialize, Debug, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(JsonSchema))] +#[cfg_attr(feature = "schema", derive(JsonSchema))] #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct ComplexityOptions { /// The maximum complexity score that we allow. Anything higher is considered excessive. diff --git a/crates/rome_js_analyze/src/analyzers/nursery/use_import_restrictions.rs b/crates/rome_js_analyze/src/analyzers/nursery/use_import_restrictions.rs index 3c7d69d0475..dd4b5a6cdef 100644 --- a/crates/rome_js_analyze/src/analyzers/nursery/use_import_restrictions.rs +++ b/crates/rome_js_analyze/src/analyzers/nursery/use_import_restrictions.rs @@ -35,13 +35,18 @@ declare_rule! { /// ```js,expect_diagnostic /// // Attempt to import from `foo.js` from outside its `sub` module. /// import { fooPackageVariable } from "./sub/foo.js"; - /// + /// ``` + /// ```js,expect_diagnostic /// // Attempt to import from `bar.ts` from outside its `aunt` module. /// import { barPackageVariable } from "../aunt/bar.ts"; + /// ``` /// + /// ```js,expect_diagnostic /// // Assumed to resolve to a JS/TS file. /// import { fooPackageVariable } from "./sub/foo"; + /// ``` /// + /// ```js,expect_diagnostic /// // If the `sub/foo` module is inaccessible, so is its index file. /// import { fooPackageVariable } from "./sub/foo/index.js"; /// ``` diff --git a/crates/rome_js_analyze/src/options.rs b/crates/rome_js_analyze/src/options.rs index 02f4c7e6e03..f591aaf73fb 100644 --- a/crates/rome_js_analyze/src/options.rs +++ b/crates/rome_js_analyze/src/options.rs @@ -7,6 +7,9 @@ use crate::semantic_analyzers::nursery::use_exhaustive_dependencies::{ use crate::semantic_analyzers::nursery::use_naming_convention::{ naming_convention_options, NamingConventionOptions, }; +use crate::semantic_analyzers::style::no_restricted_globals::{ + restricted_globals, RestrictedGlobals, +}; use bpaf::Bpaf; use rome_analyze::options::RuleOptions; use rome_analyze::RuleKey; @@ -29,6 +32,8 @@ pub enum PossibleOptions { Hooks(#[bpaf(external(hooks_options), hide)] HooksOptions), /// Options for `useNamingConvention` rule NamingConvention(#[bpaf(external(naming_convention_options), hide)] NamingConventionOptions), + /// Options for `noRestrictedGlobals` rule + RestrictedGlobals(#[bpaf(external(restricted_globals), hide)] RestrictedGlobals), /// No options available #[default] NoOptions, @@ -61,11 +66,18 @@ impl PossibleOptions { } "useNamingConvention" => { let options = match self { - PossibleOptions::NamingConvention(options) => *options, + PossibleOptions::NamingConvention(options) => options.clone(), _ => NamingConventionOptions::default(), }; RuleOptions::new(options) } + "noRestrictedGlobals" => { + let options = match self { + PossibleOptions::RestrictedGlobals(options) => options.clone(), + _ => RestrictedGlobals::default(), + }; + RuleOptions::new(options) + } // TODO: review error _ => panic!("This rule {:?} doesn't have options", rule_key), } @@ -107,13 +119,19 @@ impl PossibleOptions { } "strictCase" | "enumMemberCase" => { let mut options = match self { - PossibleOptions::NamingConvention(options) => *options, + PossibleOptions::NamingConvention(options) => options.clone(), _ => NamingConventionOptions::default(), }; options.visit_map(key.syntax(), value.syntax(), diagnostics)?; *self = PossibleOptions::NamingConvention(options); } + "deniedGlobals" => { + let mut options = RestrictedGlobals::default(); + options.visit_map(key.syntax(), value.syntax(), diagnostics)?; + *self = PossibleOptions::RestrictedGlobals(options); + } + _ => (), } } @@ -157,6 +175,15 @@ impl PossibleOptions { )); } } + "noRestrictedGlobals" => { + if !matches!(key_name, "deniedGlobals") { + diagnostics.push(DeserializationDiagnostic::new_unknown_key( + key_name, + node.range(), + &["deniedGlobals"], + )); + } + } _ => {} } diff --git a/crates/rome_js_analyze/src/semantic_analyzers/nursery/use_naming_convention.rs b/crates/rome_js_analyze/src/semantic_analyzers/nursery/use_naming_convention.rs index 1e85b9d0e26..d807788251d 100644 --- a/crates/rome_js_analyze/src/semantic_analyzers/nursery/use_naming_convention.rs +++ b/crates/rome_js_analyze/src/semantic_analyzers/nursery/use_naming_convention.rs @@ -423,7 +423,7 @@ pub(crate) struct State { } /// Rule's options. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize, Bpaf)] +#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, Bpaf)] #[cfg_attr(feature = "schemars", derive(JsonSchema))] #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct NamingConventionOptions { diff --git a/crates/rome_js_analyze/src/semantic_analyzers/style/no_restricted_globals.rs b/crates/rome_js_analyze/src/semantic_analyzers/style/no_restricted_globals.rs index 0755810cf59..be4ef885607 100644 --- a/crates/rome_js_analyze/src/semantic_analyzers/style/no_restricted_globals.rs +++ b/crates/rome_js_analyze/src/semantic_analyzers/style/no_restricted_globals.rs @@ -1,12 +1,18 @@ use crate::semantic_services::SemanticServices; +use bpaf::Bpaf; use rome_analyze::context::RuleContext; use rome_analyze::{declare_rule, Rule, RuleDiagnostic}; use rome_console::markup; +use rome_deserialize::json::{has_only_known_keys, VisitJsonNode}; +use rome_deserialize::{DeserializationDiagnostic, StringSet, VisitNode}; use rome_js_semantic::{Binding, BindingExtensions}; use rome_js_syntax::{ JsIdentifierAssignment, JsReferenceIdentifier, JsxReferenceIdentifier, TextRange, }; -use rome_rowan::{declare_node_union, AstNode}; +use rome_json_syntax::JsonLanguage; +use rome_rowan::{declare_node_union, AstNode, SyntaxNode}; +use serde::{Deserialize, Serialize}; +use std::str::FromStr; declare_rule! { /// This rule allows you to specify global variable names that you don’t want to use in your application. @@ -28,6 +34,23 @@ declare_rule! { /// console.log(event) /// } /// ``` + /// ## Options + /// + /// Use the options to specify additional globals that you want to restrict in your + /// source code. + /// + /// ```json + /// { + /// "//": "...", + /// "options": { + /// "deniedGlobals": ["$", "MooTools"] + /// } + /// } + /// ``` + /// + /// In the example above, the rule will emit a diagnostics if tried to use `$` or `MooTools` without + /// creating a local variable. + /// pub(crate) NoRestrictedGlobals { version: "0.10.0", name: "noRestrictedGlobals", @@ -41,14 +64,70 @@ declare_node_union! { const RESTRICTED_GLOBALS: [&str; 2] = ["event", "error"]; +/// Options for the rule `noRestrictedGlobals`. +#[derive(Default, Deserialize, Serialize, Debug, Clone, Bpaf)] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub struct RestrictedGlobals { + /// A list of names that should trigger the rule + #[bpaf(hide)] + #[serde(skip_serializing_if = "StringSet::is_empty")] + denied_globals: StringSet, +} + +impl RestrictedGlobals { + pub const KNOWN_KEYS: &'static [&'static str] = &["deniedGlobals"]; +} + +// Required by [Bpaf]. +impl FromStr for RestrictedGlobals { + type Err = &'static str; + + fn from_str(_s: &str) -> Result { + // WARNING: should not be used. + Ok(Self::default()) + } +} + +impl VisitJsonNode for RestrictedGlobals {} +impl VisitNode for RestrictedGlobals { + fn visit_member_name( + &mut self, + node: &SyntaxNode, + diagnostics: &mut Vec, + ) -> Option<()> { + has_only_known_keys(node, Self::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 { + "deniedGlobals" => { + self.denied_globals = + StringSet::new(self.map_to_index_set_string(&value, name_text, diagnostics)?); + } + + _ => (), + } + Some(()) + } +} + impl Rule for NoRestrictedGlobals { type Query = SemanticServices; type State = (TextRange, String); type Signals = Vec; - type Options = (); + type Options = RestrictedGlobals; fn run(ctx: &RuleContext) -> Self::Signals { let model = ctx.model(); + let options = ctx.options(); let unresolved_reference_nodes = model .all_unresolved_references() @@ -74,7 +153,18 @@ impl Rule for NoRestrictedGlobals { }; let token = token.ok()?; let text = token.text_trimmed(); - is_restricted(text, binding).map(|text| (token.text_trimmed_range(), text)) + is_restricted( + text, + binding, + options + .denied_globals + .index_set() + .iter() + .map(AsRef::as_ref) + .collect::>() + .as_slice(), + ) + .map(|text| (token.text_trimmed_range(), text)) }) .collect() } @@ -95,8 +185,8 @@ impl Rule for NoRestrictedGlobals { } } -fn is_restricted(name: &str, binding: Option) -> Option { - if binding.is_none() && RESTRICTED_GLOBALS.contains(&name) { +fn is_restricted(name: &str, binding: Option, denied_globals: &[&str]) -> Option { + if binding.is_none() && (RESTRICTED_GLOBALS.contains(&name) || denied_globals.contains(&name)) { Some(name.to_string()) } else { None diff --git a/crates/rome_js_analyze/tests/specs/style/noRestrictedGlobals/additionalGlobal.js b/crates/rome_js_analyze/tests/specs/style/noRestrictedGlobals/additionalGlobal.js new file mode 100644 index 00000000000..77eb3141d40 --- /dev/null +++ b/crates/rome_js_analyze/tests/specs/style/noRestrictedGlobals/additionalGlobal.js @@ -0,0 +1 @@ +console.log($); diff --git a/crates/rome_js_analyze/tests/specs/style/noRestrictedGlobals/additionalGlobal.js.snap b/crates/rome_js_analyze/tests/specs/style/noRestrictedGlobals/additionalGlobal.js.snap new file mode 100644 index 00000000000..2f05724726b --- /dev/null +++ b/crates/rome_js_analyze/tests/specs/style/noRestrictedGlobals/additionalGlobal.js.snap @@ -0,0 +1,26 @@ +--- +source: crates/rome_js_analyze/tests/spec_tests.rs +expression: additionalGlobal.js +--- +# Input +```js +console.log($); + +``` + +# Diagnostics +``` +additionalGlobal.js:1:13 lint/style/noRestrictedGlobals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + ! Do not use the global variable $. + + > 1 │ console.log($); + │ ^ + 2 │ + + i Use a local variable instead. + + +``` + + diff --git a/crates/rome_js_analyze/tests/specs/style/noRestrictedGlobals/additionalGlobal.options.json b/crates/rome_js_analyze/tests/specs/style/noRestrictedGlobals/additionalGlobal.options.json new file mode 100644 index 00000000000..bc1d2a06a63 --- /dev/null +++ b/crates/rome_js_analyze/tests/specs/style/noRestrictedGlobals/additionalGlobal.options.json @@ -0,0 +1,15 @@ +{ + "$schema": "../../../../../../npm/rome/configuration_schema.json", + "linter": { + "rules": { + "style": { + "noRestrictedGlobals": { + "level": "error", + "options": { + "deniedGlobals": ["$"] + } + } + } + } + } +} diff --git a/crates/rome_service/Cargo.toml b/crates/rome_service/Cargo.toml index 7cc6793c6dd..0bd18107dae 100644 --- a/crates/rome_service/Cargo.toml +++ b/crates/rome_service/Cargo.toml @@ -38,9 +38,9 @@ serde_json = { workspace = true, features = ["raw_value"] } tracing = { workspace = true, features = ["attributes"] } [features] -schemars = [ +schema = [ "dep:schemars", - "rome_js_analyze/schemars", + "rome_js_analyze/schema", "rome_formatter/serde", "rome_js_factory", "rome_text_edit/schemars", diff --git a/crates/rome_service/src/configuration/formatter.rs b/crates/rome_service/src/configuration/formatter.rs index 8a000d063db..a1b9b9f935e 100644 --- a/crates/rome_service/src/configuration/formatter.rs +++ b/crates/rome_service/src/configuration/formatter.rs @@ -1,15 +1,15 @@ use crate::configuration::merge::MergeWith; -use crate::configuration::string_set::StringSet; use crate::settings::FormatSettings; use crate::{ConfigurationDiagnostic, MatchOptions, Matcher, WorkspaceError}; use bpaf::Bpaf; +use rome_deserialize::StringSet; use rome_formatter::{IndentStyle, LineWidth}; use serde::{Deserialize, Serialize}; use std::str::FromStr; /// Options applied to the formatter #[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[serde(rename_all = "camelCase", default, deny_unknown_fields)] pub struct FormatterConfiguration { // if `false`, it disables the feature. `true` by default @@ -153,7 +153,7 @@ where } #[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Clone, Default)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[serde(rename_all = "camelCase")] pub enum PlainIndentStyle { /// Tab diff --git a/crates/rome_service/src/configuration/javascript.rs b/crates/rome_service/src/configuration/javascript.rs index b3391b162f8..e2cecd8df46 100644 --- a/crates/rome_service/src/configuration/javascript.rs +++ b/crates/rome_service/src/configuration/javascript.rs @@ -1,13 +1,13 @@ use crate::configuration::merge::MergeWith; -use crate::configuration::string_set::StringSet; use bpaf::Bpaf; +use rome_deserialize::StringSet; use rome_js_formatter::context::{ trailing_comma::TrailingComma, ArrowParentheses, QuoteProperties, QuoteStyle, Semicolons, }; use serde::{Deserialize, Serialize}; #[derive(Default, Debug, Deserialize, Serialize, Eq, PartialEq, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[serde(default, deny_unknown_fields)] pub struct JavascriptConfiguration { #[serde(skip_serializing_if = "Option::is_none")] @@ -65,7 +65,7 @@ impl JavascriptConfiguration { } #[derive(Default, Debug, Deserialize, Serialize, Eq, PartialEq, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[serde(rename_all = "camelCase", default, deny_unknown_fields)] pub struct JavascriptFormatter { /// The style for quotes. Defaults to double. @@ -129,12 +129,12 @@ impl MergeWith for JavascriptFormatter { } #[derive(Debug, Default, Deserialize, Serialize, Eq, PartialEq, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", 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))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[serde(rename_all = "camelCase", default, deny_unknown_fields)] pub struct JavascriptParser { #[bpaf(hide)] diff --git a/crates/rome_service/src/configuration/json.rs b/crates/rome_service/src/configuration/json.rs index bd52ee9b71d..0c9f7c3e6e3 100644 --- a/crates/rome_service/src/configuration/json.rs +++ b/crates/rome_service/src/configuration/json.rs @@ -3,7 +3,7 @@ use bpaf::Bpaf; use serde::{Deserialize, Serialize}; #[derive(Default, Debug, Deserialize, Serialize, Eq, PartialEq, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[serde(default, deny_unknown_fields)] pub struct JsonConfiguration { #[serde(skip_serializing_if = "Option::is_none")] @@ -25,12 +25,12 @@ impl MergeWith for JsonConfiguration { } #[derive(Debug, Default, Deserialize, Serialize, Eq, PartialEq, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", 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))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[serde(rename_all = "camelCase", default, deny_unknown_fields)] pub struct JsonParser { #[bpaf(hide)] diff --git a/crates/rome_service/src/configuration/linter/mod.rs b/crates/rome_service/src/configuration/linter/mod.rs index 811679f270a..e3751c2a394 100644 --- a/crates/rome_service/src/configuration/linter/mod.rs +++ b/crates/rome_service/src/configuration/linter/mod.rs @@ -3,20 +3,20 @@ mod rules; pub use crate::configuration::linter::rules::{rules, Rules}; use crate::configuration::merge::MergeWith; -use crate::configuration::string_set::StringSet; use crate::settings::LinterSettings; use crate::{ConfigurationDiagnostic, MatchOptions, Matcher, WorkspaceError}; use bpaf::Bpaf; +use rome_deserialize::StringSet; use rome_diagnostics::Severity; use rome_js_analyze::options::{possible_options, PossibleOptions}; pub use rules::*; -#[cfg(feature = "schemars")] +#[cfg(feature = "schema")] use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use std::str::FromStr; #[derive(Deserialize, Serialize, Debug, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[serde(rename_all = "camelCase", default, deny_unknown_fields)] pub struct LinterConfiguration { /// if `false`, it disables the feature and the linter won't be executed. `true` by default @@ -91,7 +91,7 @@ impl TryFrom for LinterSettings { } #[derive(Deserialize, Serialize, Debug, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(JsonSchema))] +#[cfg_attr(feature = "schema", derive(JsonSchema))] #[serde(rename_all = "camelCase", deny_unknown_fields, untagged)] pub enum RuleConfiguration { Plain(RulePlainConfiguration), @@ -157,7 +157,7 @@ impl From<&RulePlainConfiguration> for Severity { } #[derive(Default, Deserialize, Serialize, Debug, Eq, PartialEq, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(JsonSchema))] +#[cfg_attr(feature = "schema", derive(JsonSchema))] #[serde(rename_all = "camelCase")] pub enum RulePlainConfiguration { #[default] @@ -184,7 +184,7 @@ impl FromStr for RulePlainConfiguration { } #[derive(Default, Deserialize, Serialize, Debug, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(JsonSchema))] +#[cfg_attr(feature = "schema", derive(JsonSchema))] #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct RuleWithOptions { pub level: RulePlainConfiguration, diff --git a/crates/rome_service/src/configuration/linter/rules.rs b/crates/rome_service/src/configuration/linter/rules.rs index cd9ec5f201f..a6ac8523c89 100644 --- a/crates/rome_service/src/configuration/linter/rules.rs +++ b/crates/rome_service/src/configuration/linter/rules.rs @@ -5,11 +5,11 @@ use bpaf::Bpaf; use indexmap::IndexSet; use rome_analyze::RuleFilter; use rome_diagnostics::{Category, Severity}; -#[cfg(feature = "schemars")] +#[cfg(feature = "schema")] use schemars::JsonSchema; use serde::{Deserialize, Serialize}; #[derive(Deserialize, Serialize, Debug, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(JsonSchema))] +#[cfg_attr(feature = "schema", derive(JsonSchema))] #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct Rules { #[doc = r" It enables the lint rules recommended by Rome. `true` by default."] @@ -335,7 +335,7 @@ impl Rules { } } #[derive(Deserialize, Default, Serialize, Debug, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(JsonSchema))] +#[cfg_attr(feature = "schema", derive(JsonSchema))] #[serde(rename_all = "camelCase", default)] #[doc = r" A list of rules that belong to this group"] pub struct A11y { @@ -832,7 +832,7 @@ impl A11y { } } #[derive(Deserialize, Default, Serialize, Debug, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(JsonSchema))] +#[cfg_attr(feature = "schema", derive(JsonSchema))] #[serde(rename_all = "camelCase", default)] #[doc = r" A list of rules that belong to this group"] pub struct Complexity { @@ -1181,7 +1181,7 @@ impl Complexity { } } #[derive(Deserialize, Default, Serialize, Debug, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(JsonSchema))] +#[cfg_attr(feature = "schema", derive(JsonSchema))] #[serde(rename_all = "camelCase", default)] #[doc = r" A list of rules that belong to this group"] pub struct Correctness { @@ -1775,7 +1775,7 @@ impl Correctness { } } #[derive(Deserialize, Default, Serialize, Debug, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(JsonSchema))] +#[cfg_attr(feature = "schema", derive(JsonSchema))] #[serde(rename_all = "camelCase", default)] #[doc = r" A list of rules that belong to this group"] pub struct Nursery { @@ -2558,7 +2558,7 @@ impl Nursery { } } #[derive(Deserialize, Default, Serialize, Debug, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(JsonSchema))] +#[cfg_attr(feature = "schema", derive(JsonSchema))] #[serde(rename_all = "camelCase", default)] #[doc = r" A list of rules that belong to this group"] pub struct Performance { @@ -2644,7 +2644,7 @@ impl Performance { } } #[derive(Deserialize, Default, Serialize, Debug, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(JsonSchema))] +#[cfg_attr(feature = "schema", derive(JsonSchema))] #[serde(rename_all = "camelCase", default)] #[doc = r" A list of rules that belong to this group"] pub struct Security { @@ -2767,7 +2767,7 @@ impl Security { } } #[derive(Deserialize, Default, Serialize, Debug, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(JsonSchema))] +#[cfg_attr(feature = "schema", derive(JsonSchema))] #[serde(rename_all = "camelCase", default)] #[doc = r" A list of rules that belong to this group"] pub struct Style { @@ -3352,7 +3352,7 @@ impl Style { } } #[derive(Deserialize, Default, Serialize, Debug, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(JsonSchema))] +#[cfg_attr(feature = "schema", derive(JsonSchema))] #[serde(rename_all = "camelCase", default)] #[doc = r" A list of rules that belong to this group"] pub struct Suspicious { diff --git a/crates/rome_service/src/configuration/mod.rs b/crates/rome_service/src/configuration/mod.rs index 93848330826..62574d4ff92 100644 --- a/crates/rome_service/src/configuration/mod.rs +++ b/crates/rome_service/src/configuration/mod.rs @@ -11,14 +11,12 @@ pub mod linter; mod merge; pub mod organize_imports; mod parse; -pub mod string_set; pub mod vcs; pub use crate::configuration::diagnostics::ConfigurationDiagnostic; use crate::configuration::generated::push_to_analyzer_rules; pub use crate::configuration::merge::MergeWith; use crate::configuration::organize_imports::{organize_imports, OrganizeImports}; -pub use crate::configuration::string_set::StringSet; use crate::configuration::vcs::{vcs_configuration, VcsConfiguration}; use crate::settings::{LanguagesSettings, LinterSettings}; use crate::{DynRef, WorkspaceError, VERSION}; @@ -29,7 +27,7 @@ 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_deserialize::{Deserialized, StringSet}; use rome_fs::{AutoSearchResult, FileSystem, OpenOptions}; use rome_js_analyze::metadata; use rome_json_formatter::context::JsonFormatOptions; @@ -42,7 +40,7 @@ use std::path::{Path, PathBuf}; /// The configuration that is contained inside the file `rome.json` #[derive(Debug, Deserialize, Serialize, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[serde(deny_unknown_fields, rename_all = "camelCase")] pub struct Configuration { /// A field for the [JSON schema](https://json-schema.org/) specification @@ -245,7 +243,7 @@ impl MergeWith> for Configuration { /// The configuration of the filesystem #[derive(Default, Debug, Deserialize, Serialize, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[serde(rename_all = "camelCase", default, deny_unknown_fields)] pub struct FilesConfiguration { /// The maximum allowed size for source code files in bytes. Files above diff --git a/crates/rome_service/src/configuration/organize_imports.rs b/crates/rome_service/src/configuration/organize_imports.rs index 7e8e792068f..b353b72ef53 100644 --- a/crates/rome_service/src/configuration/organize_imports.rs +++ b/crates/rome_service/src/configuration/organize_imports.rs @@ -1,12 +1,12 @@ use crate::configuration::merge::MergeWith; -use crate::configuration::string_set::StringSet; use crate::settings::OrganizeImportsSettings; use crate::{ConfigurationDiagnostic, MatchOptions, Matcher, WorkspaceError}; use bpaf::Bpaf; +use rome_deserialize::StringSet; use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[serde(rename_all = "camelCase", default, deny_unknown_fields)] pub struct OrganizeImports { /// Enables the organization of imports diff --git a/crates/rome_service/src/configuration/parse/json/configuration.rs b/crates/rome_service/src/configuration/parse/json/configuration.rs index 2fdb637d62d..9348dcbb060 100644 --- a/crates/rome_service/src/configuration/parse/json/configuration.rs +++ b/crates/rome_service/src/configuration/parse/json/configuration.rs @@ -4,11 +4,10 @@ use crate::configuration::parse::json::vcs::validate_vcs_configuration; use crate::configuration::vcs::VcsConfiguration; use crate::configuration::{ FilesConfiguration, FormatterConfiguration, JavascriptConfiguration, LinterConfiguration, - StringSet, }; use crate::Configuration; use rome_deserialize::json::{has_only_known_keys, VisitJsonNode}; -use rome_deserialize::{DeserializationDiagnostic, VisitNode}; +use rome_deserialize::{DeserializationDiagnostic, StringSet, VisitNode}; use rome_json_syntax::{JsonLanguage, JsonSyntaxNode}; use rome_rowan::SyntaxNode; diff --git a/crates/rome_service/src/configuration/parse/json/files.rs b/crates/rome_service/src/configuration/parse/json/files.rs index 0e1edd8f9cc..f75cd7df0c5 100644 --- a/crates/rome_service/src/configuration/parse/json/files.rs +++ b/crates/rome_service/src/configuration/parse/json/files.rs @@ -1,7 +1,6 @@ -use crate::configuration::string_set::StringSet; use crate::configuration::FilesConfiguration; use rome_deserialize::json::{has_only_known_keys, VisitJsonNode}; -use rome_deserialize::{DeserializationDiagnostic, VisitNode}; +use rome_deserialize::{DeserializationDiagnostic, StringSet, VisitNode}; use rome_json_syntax::JsonLanguage; use rome_rowan::SyntaxNode; use std::num::NonZeroU64; diff --git a/crates/rome_service/src/configuration/parse/json/formatter.rs b/crates/rome_service/src/configuration/parse/json/formatter.rs index eabb889571a..d420bacd033 100644 --- a/crates/rome_service/src/configuration/parse/json/formatter.rs +++ b/crates/rome_service/src/configuration/parse/json/formatter.rs @@ -1,8 +1,7 @@ -use crate::configuration::string_set::StringSet; use crate::configuration::{FormatterConfiguration, PlainIndentStyle}; use rome_console::markup; use rome_deserialize::json::{has_only_known_keys, with_only_known_variants, VisitJsonNode}; -use rome_deserialize::{DeserializationDiagnostic, VisitNode}; +use rome_deserialize::{DeserializationDiagnostic, StringSet, VisitNode}; use rome_formatter::LineWidth; use rome_json_syntax::{JsonLanguage, JsonSyntaxNode}; use rome_rowan::{AstNode, SyntaxNode}; diff --git a/crates/rome_service/src/configuration/parse/json/javascript.rs b/crates/rome_service/src/configuration/parse/json/javascript.rs index 4ebceddce4f..3ee6a1b60cd 100644 --- a/crates/rome_service/src/configuration/parse/json/javascript.rs +++ b/crates/rome_service/src/configuration/parse/json/javascript.rs @@ -1,8 +1,7 @@ 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}; -use rome_deserialize::{DeserializationDiagnostic, VisitNode}; +use rome_deserialize::{DeserializationDiagnostic, StringSet, VisitNode}; use rome_js_formatter::context::trailing_comma::TrailingComma; use rome_js_formatter::context::{ArrowParentheses, QuoteProperties, QuoteStyle, Semicolons}; use rome_json_syntax::{JsonLanguage, JsonSyntaxNode}; diff --git a/crates/rome_service/src/configuration/parse/json/linter.rs b/crates/rome_service/src/configuration/parse/json/linter.rs index 0f7f3545aaf..79630d9685d 100644 --- a/crates/rome_service/src/configuration/parse/json/linter.rs +++ b/crates/rome_service/src/configuration/parse/json/linter.rs @@ -1,10 +1,9 @@ use crate::configuration::linter::{RulePlainConfiguration, RuleWithOptions}; -use crate::configuration::string_set::StringSet; use crate::configuration::LinterConfiguration; use crate::{RuleConfiguration, Rules}; use rome_console::markup; use rome_deserialize::json::{has_only_known_keys, with_only_known_variants, VisitJsonNode}; -use rome_deserialize::{DeserializationDiagnostic, VisitNode}; +use rome_deserialize::{DeserializationDiagnostic, StringSet, VisitNode}; use rome_js_analyze::options::PossibleOptions; use rome_json_syntax::{AnyJsonValue, JsonLanguage, JsonObjectValue, JsonSyntaxNode}; use rome_rowan::{AstNode, AstSeparatedList, SyntaxNode}; diff --git a/crates/rome_service/src/configuration/parse/json/organize_imports.rs b/crates/rome_service/src/configuration/parse/json/organize_imports.rs index e4f7ac05420..029c8716875 100644 --- a/crates/rome_service/src/configuration/parse/json/organize_imports.rs +++ b/crates/rome_service/src/configuration/parse/json/organize_imports.rs @@ -1,7 +1,6 @@ use crate::configuration::organize_imports::OrganizeImports; -use crate::configuration::string_set::StringSet; use rome_deserialize::json::{has_only_known_keys, VisitJsonNode}; -use rome_deserialize::{DeserializationDiagnostic, VisitNode}; +use rome_deserialize::{DeserializationDiagnostic, StringSet, VisitNode}; use rome_json_syntax::{JsonLanguage, JsonSyntaxNode}; use rome_rowan::SyntaxNode; diff --git a/crates/rome_service/src/configuration/vcs.rs b/crates/rome_service/src/configuration/vcs.rs index 967e0b76ad8..0a6c758fdc5 100644 --- a/crates/rome_service/src/configuration/vcs.rs +++ b/crates/rome_service/src/configuration/vcs.rs @@ -7,7 +7,7 @@ const GIT_IGNORE_FILE_NAME: &str = ".gitignore"; /// Set of properties to integrate Rome with a VCS software. #[derive(Debug, Default, Deserialize, Serialize, Clone, Bpaf)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[serde(deny_unknown_fields, rename_all = "camelCase")] pub struct VcsConfiguration { /// The kind of client. @@ -65,7 +65,7 @@ impl MergeWith for VcsConfiguration { } #[derive(Debug, Default, Deserialize, Clone, Serialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[serde(rename_all = "camelCase")] pub enum VcsClientKind { #[default] diff --git a/crates/rome_service/src/file_handlers/javascript.rs b/crates/rome_service/src/file_handlers/javascript.rs index dd9840a6611..0b5e9f8c37c 100644 --- a/crates/rome_service/src/file_handlers/javascript.rs +++ b/crates/rome_service/src/file_handlers/javascript.rs @@ -43,7 +43,7 @@ use std::path::PathBuf; use tracing::{debug, trace}; #[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct JsFormatterSettings { pub quote_style: Option, pub jsx_quote_style: Option, @@ -54,25 +54,25 @@ pub struct JsFormatterSettings { } #[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct JsParserSettings { pub parse_class_parameter_decorators: bool, } #[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct JsonParserSettings { pub allow_comments: bool, } #[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct JsLinterSettings { pub globals: Vec, } #[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct JsOrganizeImportsSettings {} impl Language for JsLanguage { diff --git a/crates/rome_service/src/file_handlers/mod.rs b/crates/rome_service/src/file_handlers/mod.rs index a2002c1e181..59b9c7151b8 100644 --- a/crates/rome_service/src/file_handlers/mod.rs +++ b/crates/rome_service/src/file_handlers/mod.rs @@ -24,7 +24,7 @@ mod unknown; /// Supported languages by Rome #[derive(Clone, Copy, Debug, Eq, PartialEq, Default, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub enum Language { /// JavaScript JavaScript, diff --git a/crates/rome_service/src/lib.rs b/crates/rome_service/src/lib.rs index 071bdd913b3..cd4c3bfc984 100644 --- a/crates/rome_service/src/lib.rs +++ b/crates/rome_service/src/lib.rs @@ -10,14 +10,12 @@ pub mod settings; pub mod workspace; mod diagnostics; -#[cfg(feature = "schemars")] +#[cfg(feature = "schema")] pub mod workspace_types; pub use crate::configuration::{ - create_config, load_config, - string_set::{deserialize_string_set, serialize_string_set}, - Configuration, ConfigurationBasePath, ConfigurationDiagnostic, JavascriptFormatter, MergeWith, - RuleConfiguration, Rules, + create_config, load_config, Configuration, ConfigurationBasePath, ConfigurationDiagnostic, + JavascriptFormatter, MergeWith, RuleConfiguration, Rules, }; pub use crate::matcher::{MatchOptions, Matcher, Pattern}; diff --git a/crates/rome_service/src/workspace.rs b/crates/rome_service/src/workspace.rs index e7db57d8d85..34eeeba8ad2 100644 --- a/crates/rome_service/src/workspace.rs +++ b/crates/rome_service/src/workspace.rs @@ -72,20 +72,20 @@ mod client; mod server; #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct SupportsFeatureParams { pub path: RomePath, pub feature: Vec, } #[derive(Debug, serde::Serialize, serde::Deserialize, Default)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct SupportsFeatureResult { pub reason: Option, } #[derive(Debug, serde::Serialize, serde::Deserialize, Default)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct FileFeaturesResult { pub features_supported: HashMap, } @@ -184,7 +184,7 @@ impl SupportsFeatureResult { } #[derive(Debug, serde::Serialize, serde::Deserialize, Eq, PartialEq)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub enum SupportKind { /// The feature is enabled for the file Supported, @@ -206,7 +206,7 @@ impl SupportKind { } #[derive(Debug, Clone, Hash, serde::Serialize, serde::Deserialize, Eq, PartialEq)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub enum FeatureName { Format, Lint, @@ -240,13 +240,13 @@ impl FeaturesBuilder { } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct UpdateSettingsParams { pub configuration: Configuration, } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct OpenFileParams { pub path: RomePath, pub content: String, @@ -256,39 +256,39 @@ pub struct OpenFileParams { } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct GetSyntaxTreeParams { pub path: RomePath, } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct GetSyntaxTreeResult { pub cst: String, pub ast: String, } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct GetControlFlowGraphParams { pub path: RomePath, pub cursor: TextSize, } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct GetFormatterIRParams { pub path: RomePath, } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct GetFileContentParams { pub path: RomePath, } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct ChangeFileParams { pub path: RomePath, pub content: String, @@ -296,13 +296,13 @@ pub struct ChangeFileParams { } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct CloseFileParams { pub path: RomePath, } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct PullDiagnosticsParams { pub path: RomePath, pub categories: RuleCategories, @@ -310,7 +310,7 @@ pub struct PullDiagnosticsParams { } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct PullDiagnosticsResult { pub diagnostics: Vec, pub errors: usize, @@ -318,20 +318,20 @@ pub struct PullDiagnosticsResult { } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct PullActionsParams { pub path: RomePath, pub range: TextRange, } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct PullActionsResult { pub actions: Vec, } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct CodeAction { pub category: ActionCategory, pub rule_name: Option<(Cow<'static, str>, Cow<'static, str>)>, @@ -339,27 +339,27 @@ pub struct CodeAction { } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct FormatFileParams { pub path: RomePath, } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct FormatRangeParams { pub path: RomePath, pub range: TextRange, } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct FormatOnTypeParams { pub path: RomePath, pub offset: TextSize, } #[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] /// Which fixes should be applied during the analyzing phase pub enum FixFileMode { /// Applies [safe](rome_diagnostics::Applicability::Always) fixes @@ -369,7 +369,7 @@ pub enum FixFileMode { } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct FixFileParams { pub path: RomePath, pub fix_file_mode: FixFileMode, @@ -377,7 +377,7 @@ pub struct FixFileParams { } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct FixFileResult { /// New source code for the file with all fixes applied pub code: String, @@ -392,7 +392,7 @@ pub struct FixFileResult { } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct FixAction { /// Name of the rule group and rule that emitted this code action pub rule_name: Option<(Cow<'static, str>, Cow<'static, str>)>, @@ -401,7 +401,7 @@ pub struct FixAction { } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct RenameParams { pub path: RomePath, pub symbol_at: TextSize, @@ -409,7 +409,7 @@ pub struct RenameParams { } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct RenameResult { /// Range of source code modified by this rename operation pub range: TextRange, @@ -418,7 +418,7 @@ pub struct RenameResult { } #[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct ServerInfo { /// The name of the server as defined by the server. pub name: String, @@ -429,17 +429,17 @@ pub struct ServerInfo { } #[derive(Copy, Clone, Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct RageParams {} #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct RageResult { pub entries: Vec, } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub enum RageEntry { Section(String), Pair { name: String, value: MarkupBuf }, @@ -447,13 +447,13 @@ pub enum RageEntry { } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct OrganizeImportsParams { pub path: RomePath, } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct OrganizeImportsResult { pub code: String, } @@ -480,7 +480,7 @@ impl RageEntry { } #[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct IsPathIgnoredParams { pub rome_path: RomePath, pub feature: FeatureName, diff --git a/crates/rome_wasm/Cargo.toml b/crates/rome_wasm/Cargo.toml index fce0dda6d24..7b14c33f752 100644 --- a/crates/rome_wasm/Cargo.toml +++ b/crates/rome_wasm/Cargo.toml @@ -35,6 +35,6 @@ quote = "1.0.14" rome_js_factory = { workspace = true } rome_js_formatter = { workspace = true } rome_rowan = { workspace = true } -rome_service = { workspace = true, features = ["schemars"] } +rome_service = { workspace = true, features = ["schema"] } schemars = { workspace = true } serde_json = { workspace = true } diff --git a/editors/vscode/configuration_schema.json b/editors/vscode/configuration_schema.json index c66d224c072..4773e950677 100644 --- a/editors/vscode/configuration_schema.json +++ b/editors/vscode/configuration_schema.json @@ -1095,11 +1095,27 @@ "description": "Options for `useNamingConvention` rule", "allOf": [{ "$ref": "#/definitions/NamingConventionOptions" }] }, + { + "description": "Options for `noRestrictedGlobals` rule", + "allOf": [{ "$ref": "#/definitions/RestrictedGlobals" }] + }, { "description": "No options available", "type": "null" } ] }, "QuoteProperties": { "type": "string", "enum": ["asNeeded", "preserve"] }, "QuoteStyle": { "type": "string", "enum": ["double", "single"] }, + "RestrictedGlobals": { + "description": "Options for the rule `noRestrictedGlobals`.", + "type": "object", + "required": ["deniedGlobals"], + "properties": { + "deniedGlobals": { + "description": "A list of names that should trigger the rule", + "allOf": [{ "$ref": "#/definitions/StringSet" }] + } + }, + "additionalProperties": false + }, "RuleConfiguration": { "anyOf": [ { "$ref": "#/definitions/RulePlainConfiguration" }, diff --git a/npm/backend-jsonrpc/src/workspace.ts b/npm/backend-jsonrpc/src/workspace.ts index e8c45b505fa..45f4495c711 100644 --- a/npm/backend-jsonrpc/src/workspace.ts +++ b/npm/backend-jsonrpc/src/workspace.ts @@ -970,6 +970,7 @@ export type PossibleOptions = | ComplexityOptions | HooksOptions | NamingConventionOptions + | RestrictedGlobals | null; /** * Options for the rule `noNestedModuleImports`. @@ -1002,6 +1003,15 @@ export interface NamingConventionOptions { */ strictCase: boolean; } +/** + * Options for the rule `noRestrictedGlobals`. + */ +export interface RestrictedGlobals { + /** + * A list of names that should trigger the rule + */ + deniedGlobals: StringSet; +} export interface Hooks { /** * The "position" of the closure function, starting from zero. diff --git a/npm/rome/configuration_schema.json b/npm/rome/configuration_schema.json index c66d224c072..4773e950677 100644 --- a/npm/rome/configuration_schema.json +++ b/npm/rome/configuration_schema.json @@ -1095,11 +1095,27 @@ "description": "Options for `useNamingConvention` rule", "allOf": [{ "$ref": "#/definitions/NamingConventionOptions" }] }, + { + "description": "Options for `noRestrictedGlobals` rule", + "allOf": [{ "$ref": "#/definitions/RestrictedGlobals" }] + }, { "description": "No options available", "type": "null" } ] }, "QuoteProperties": { "type": "string", "enum": ["asNeeded", "preserve"] }, "QuoteStyle": { "type": "string", "enum": ["double", "single"] }, + "RestrictedGlobals": { + "description": "Options for the rule `noRestrictedGlobals`.", + "type": "object", + "required": ["deniedGlobals"], + "properties": { + "deniedGlobals": { + "description": "A list of names that should trigger the rule", + "allOf": [{ "$ref": "#/definitions/StringSet" }] + } + }, + "additionalProperties": false + }, "RuleConfiguration": { "anyOf": [ { "$ref": "#/definitions/RulePlainConfiguration" }, diff --git a/website/src/pages/lint/rules/noRestrictedGlobals.md b/website/src/pages/lint/rules/noRestrictedGlobals.md index 7e6e0fe748e..a3dd8aa6bf1 100644 --- a/website/src/pages/lint/rules/noRestrictedGlobals.md +++ b/website/src/pages/lint/rules/noRestrictedGlobals.md @@ -39,6 +39,23 @@ function f(event) { } ``` +## Options + +Use the options to specify additional globals that you want to restrict in your +source code. + +``` +{ + "//": "...", + "options": { + "deniedGlobals": ["$", "MooTools"] + } +} +``` + +In the example above, the rule will emit a diagnostics if tried to use `$` or `MooTools` without +creating a local variable. + ## Related links - [Disable a rule](/linter/#disable-a-lint-rule) diff --git a/website/src/pages/lint/rules/useImportRestrictions.md b/website/src/pages/lint/rules/useImportRestrictions.md index f412b075409..a07101c2cc7 100644 --- a/website/src/pages/lint/rules/useImportRestrictions.md +++ b/website/src/pages/lint/rules/useImportRestrictions.md @@ -32,15 +32,6 @@ Source: https://github.com/uhyo/eslint-plugin-import-access ```jsx // Attempt to import from `foo.js` from outside its `sub` module. import { fooPackageVariable } from "./sub/foo.js"; - -// Attempt to import from `bar.ts` from outside its `aunt` module. -import { barPackageVariable } from "../aunt/bar.ts"; - -// Assumed to resolve to a JS/TS file. -import { fooPackageVariable } from "./sub/foo"; - -// If the `sub/foo` module is inaccessible, so is its index file. -import { fooPackageVariable } from "./sub/foo/index.js"; ```
nursery/useImportRestrictions.js:2:36 lint/nursery/useImportRestrictions ━━━━━━━━━━━━━━━━━━━━━━━━━━━
@@ -51,24 +42,65 @@ import { fooPackageVariable } from "./sub/foo/index.js";
   > 2 │ import { fooPackageVariable } from "./sub/foo.js";
                                       ^^^^^^^^^^^^^^
     3 │ 
-    4 │ // Attempt to import from `bar.ts` from outside its `aunt` module.
   
    Please import from ./sub instead (you may need to re-export the symbol(s) from ./sub/foo.js).
   
-nursery/useImportRestrictions.js:5:36 lint/nursery/useImportRestrictions ━━━━━━━━━━━━━━━━━━━━━━━━━━━
+
+ +```jsx +// Attempt to import from `bar.ts` from outside its `aunt` module. +import { barPackageVariable } from "../aunt/bar.ts"; +``` + +
nursery/useImportRestrictions.js:2:36 lint/nursery/useImportRestrictions ━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
    Importing package private symbols is prohibited from outside the module directory.
   
-    4 │ // Attempt to import from `bar.ts` from outside its `aunt` module.
-  > 5 │ import { barPackageVariable } from "../aunt/bar.ts";
+    1 │ // Attempt to import from `bar.ts` from outside its `aunt` module.
+  > 2 │ import { barPackageVariable } from "../aunt/bar.ts";
                                       ^^^^^^^^^^^^^^^^
-    6 │ 
-    7 │ // Assumed to resolve to a JS/TS file.
+    3 │ 
   
    Please import from ../aunt instead (you may need to re-export the symbol(s) from ../aunt/bar.ts).
   
 
+```jsx +// Assumed to resolve to a JS/TS file. +import { fooPackageVariable } from "./sub/foo"; +``` + +
nursery/useImportRestrictions.js:2:36 lint/nursery/useImportRestrictions ━━━━━━━━━━━━━━━━━━━━━━━━━━━
+
+   Importing package private symbols is prohibited from outside the module directory.
+  
+    1 │ // Assumed to resolve to a JS/TS file.
+  > 2 │ import { fooPackageVariable } from "./sub/foo";
+                                      ^^^^^^^^^^^
+    3 │ 
+  
+   Please import from ./sub instead (you may need to re-export the symbol(s) from ./sub/foo).
+  
+
+ +```jsx +// If the `sub/foo` module is inaccessible, so is its index file. +import { fooPackageVariable } from "./sub/foo/index.js"; +``` + +
nursery/useImportRestrictions.js:2:36 lint/nursery/useImportRestrictions ━━━━━━━━━━━━━━━━━━━━━━━━━━━
+
+   Importing package private symbols is prohibited from outside the module directory.
+  
+    1 │ // If the `sub/foo` module is inaccessible, so is its index file.
+  > 2 │ import { fooPackageVariable } from "./sub/foo/index.js";
+                                      ^^^^^^^^^^^^^^^^^^^^
+    3 │ 
+  
+   Please import from ./sub/index.js instead (you may need to re-export the symbol(s) from ./sub/foo/index.js).
+  
+
+ ### Valid ```jsx diff --git a/xtask/codegen/Cargo.toml b/xtask/codegen/Cargo.toml index 8bc689875e4..25239599222 100644 --- a/xtask/codegen/Cargo.toml +++ b/xtask/codegen/Cargo.toml @@ -33,7 +33,7 @@ rome_json_parser = { path = "../../crates/rome_json_parser", optional = true } rome_diagnostics = { path = "../../crates/rome_diagnostics", optional = true } rome_aria = { path = "../../crates/rome_aria", optional = true } rome_service = { path = "../../crates/rome_service", features = [ - "schemars", + "schema", ], optional = true } schemars = { version = "0.8.10", optional = true } serde_json = { version = "1.0.74", optional = true } diff --git a/xtask/codegen/src/generate_configuration.rs b/xtask/codegen/src/generate_configuration.rs index d767b363fb1..e8474e86c12 100644 --- a/xtask/codegen/src/generate_configuration.rs +++ b/xtask/codegen/src/generate_configuration.rs @@ -151,7 +151,7 @@ pub(crate) fn generate_rules_configuration(mode: Mode) -> Result<()> { let groups = quote! { use serde::{Deserialize, Serialize}; - #[cfg(feature = "schemars")] + #[cfg(feature = "schema")] use schemars::JsonSchema; use crate::RuleConfiguration; use rome_analyze::RuleFilter; @@ -160,7 +160,7 @@ pub(crate) fn generate_rules_configuration(mode: Mode) -> Result<()> { use rome_diagnostics::{Category, Severity}; #[derive(Deserialize, Serialize, Debug, Clone, Bpaf)] - #[cfg_attr(feature = "schemars", derive(JsonSchema))] + #[cfg_attr(feature = "schema", derive(JsonSchema))] #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct Rules { /// It enables the lint rules recommended by Rome. `true` by default. @@ -481,7 +481,7 @@ fn generate_struct(group: &str, rules: &BTreeMap<&'static str, RuleMetadata>) -> }; quote! { #[derive(Deserialize, Default, Serialize, Debug, Clone, Bpaf)] - #[cfg_attr(feature = "schemars", derive(JsonSchema))] + #[cfg_attr(feature = "schema", derive(JsonSchema))] #[serde(rename_all = "camelCase", default)] /// A list of rules that belong to this group pub struct #group_struct_name {