From 16a37e2a068dd1cdcb49748003446535f4852a64 Mon Sep 17 00:00:00 2001 From: Don Isaac Date: Wed, 21 Aug 2024 17:58:27 -0400 Subject: [PATCH] refactor(linter): split options into multiple files --- .../oxc_linter/src/options/allow_warn_deny.rs | 115 +++++++++++ .../src/{options.rs => options/mod.rs} | 194 +----------------- crates/oxc_linter/src/options/plugins.rs | 79 +++++++ 3 files changed, 201 insertions(+), 187 deletions(-) create mode 100644 crates/oxc_linter/src/options/allow_warn_deny.rs rename crates/oxc_linter/src/{options.rs => options/mod.rs} (59%) create mode 100644 crates/oxc_linter/src/options/plugins.rs diff --git a/crates/oxc_linter/src/options/allow_warn_deny.rs b/crates/oxc_linter/src/options/allow_warn_deny.rs new file mode 100644 index 00000000000000..85cc303d366ea5 --- /dev/null +++ b/crates/oxc_linter/src/options/allow_warn_deny.rs @@ -0,0 +1,115 @@ +use std::convert::From; + +use oxc_diagnostics::{OxcDiagnostic, Severity}; +use schemars::{schema::SchemaObject, JsonSchema}; +use serde_json::{Number, Value}; + +#[derive(Debug, Clone, Copy, Eq, PartialEq)] +pub enum AllowWarnDeny { + Allow, // Off + Warn, // Warn + Deny, // Error +} + +impl AllowWarnDeny { + pub fn is_warn_deny(self) -> bool { + self != Self::Allow + } + + pub fn is_allow(self) -> bool { + self == Self::Allow + } +} + +impl TryFrom<&str> for AllowWarnDeny { + type Error = OxcDiagnostic; + + fn try_from(s: &str) -> Result { + match s { + "allow" | "off" => Ok(Self::Allow), + "deny" | "error" => Ok(Self::Deny), + "warn" => Ok(Self::Warn), + _ => Err(OxcDiagnostic::error(format!( + r#"Failed to parse rule severity, expected one of "allow", "off", "deny", "error" or "warn", but got {s:?}"# + ))), + } + } +} + +impl TryFrom<&Value> for AllowWarnDeny { + type Error = OxcDiagnostic; + + fn try_from(value: &Value) -> Result { + match value { + Value::String(s) => Self::try_from(s.as_str()), + Value::Number(n) => Self::try_from(n), + _ => Err(OxcDiagnostic::error(format!( + "Failed to parse rule severity, expected a string or a number, but got {value:?}" + ))), + } + } +} + +impl TryFrom<&Number> for AllowWarnDeny { + type Error = OxcDiagnostic; + + fn try_from(value: &Number) -> Result { + match value.as_i64() { + Some(0) => Ok(Self::Allow), + Some(1) => Ok(Self::Warn), + Some(2) => Ok(Self::Deny), + _ => Err(OxcDiagnostic::error(format!( + r#"Failed to parse rule severity, expected one of `0`, `1` or `2`, but got {value:?}"# + ))), + } + } +} + +impl JsonSchema for AllowWarnDeny { + fn schema_name() -> String { + "AllowWarnDeny".to_string() + } + + fn schema_id() -> std::borrow::Cow<'static, str> { + "AllowWarnDeny".into() + } + + fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { + let mut string_schema = ::json_schema(gen).into_object(); + string_schema.enum_values = + Some(vec!["allow".into(), "off".into(), "warn".into(), "error".into(), "deny".into()]); + string_schema.metadata().description = Some( + r#"Oxlint rule. +- "allow" or "off": Turn off the rule. +- "warn": Turn the rule on as a warning (doesn't affect exit code). +- "error" or "deny": Turn the rule on as an error (will exit with a failure code)."# + .to_string(), + ); + let mut int_schema = ::json_schema(gen).into_object(); + int_schema.number().minimum = Some(0.0); + int_schema.number().maximum = Some(2.0); + int_schema.metadata().description = Some( + "Oxlint rule. + +- 0: Turn off the rule. +- 1: Turn the rule on as a warning (doesn't affect exit code). +- 2: Turn the rule on as an error (will exit with a failure code)." + .to_string(), + ); + + let mut schema = SchemaObject::default(); + schema.subschemas().one_of = Some(vec![string_schema.into(), int_schema.into()]); + + schema.into() + } +} + +impl From for Severity { + fn from(value: AllowWarnDeny) -> Self { + match value { + AllowWarnDeny::Allow => Self::Advice, + AllowWarnDeny::Warn => Self::Warning, + AllowWarnDeny::Deny => Self::Error, + } + } +} diff --git a/crates/oxc_linter/src/options.rs b/crates/oxc_linter/src/options/mod.rs similarity index 59% rename from crates/oxc_linter/src/options.rs rename to crates/oxc_linter/src/options/mod.rs index 2b8d5490add1a8..e2d6e72ec4a37a 100644 --- a/crates/oxc_linter/src/options.rs +++ b/crates/oxc_linter/src/options/mod.rs @@ -1,15 +1,19 @@ +mod allow_warn_deny; +mod plugins; + use std::{convert::From, path::PathBuf}; -use oxc_diagnostics::{Error, OxcDiagnostic, Severity}; +use oxc_diagnostics::Error; use rustc_hash::FxHashSet; -use schemars::{schema::SchemaObject, JsonSchema}; -use serde_json::{Number, Value}; use crate::{ config::OxlintConfig, fixer::FixKind, rules::RULES, utils::is_jest_rule_adapted_to_vitest, FrameworkFlags, RuleCategory, RuleEnum, RuleWithSeverity, }; +pub use allow_warn_deny::AllowWarnDeny; +pub use plugins::LintPluginOptions; + #[derive(Debug)] pub struct LintOptions { /// Allow / Deny rules in order. [("allow" / "deny", rule name)] @@ -142,190 +146,6 @@ impl LintOptions { } } -#[derive(Debug)] -#[non_exhaustive] -pub struct LintPluginOptions { - pub react: bool, - pub unicorn: bool, - pub typescript: bool, - pub oxc: bool, - pub import: bool, - pub jsdoc: bool, - pub jest: bool, - pub vitest: bool, - pub jsx_a11y: bool, - pub nextjs: bool, - pub react_perf: bool, - pub promise: bool, -} - -impl Default for LintPluginOptions { - fn default() -> Self { - Self { - react: true, - unicorn: true, - typescript: true, - oxc: true, - import: false, - jsdoc: false, - jest: false, - vitest: false, - jsx_a11y: false, - nextjs: false, - react_perf: false, - promise: false, - } - } -} - -impl LintPluginOptions { - /// Create a new instance with all plugins disabled. - pub fn none() -> Self { - Self { - react: false, - unicorn: false, - typescript: false, - oxc: false, - import: false, - jsdoc: false, - jest: false, - vitest: false, - jsx_a11y: false, - nextjs: false, - react_perf: false, - promise: false, - } - } - - /// Create a new instance with all plugins enabled. - pub fn all() -> Self { - Self { - react: true, - unicorn: true, - typescript: true, - oxc: true, - import: true, - jsdoc: true, - jest: true, - vitest: true, - jsx_a11y: true, - nextjs: true, - react_perf: true, - promise: true, - } - } -} - -#[derive(Debug, Clone, Copy, Eq, PartialEq)] -pub enum AllowWarnDeny { - Allow, // Off - Warn, // Warn - Deny, // Error -} - -impl AllowWarnDeny { - pub fn is_warn_deny(self) -> bool { - self != Self::Allow - } - - pub fn is_allow(self) -> bool { - self == Self::Allow - } -} - -impl TryFrom<&str> for AllowWarnDeny { - type Error = OxcDiagnostic; - - fn try_from(s: &str) -> Result { - match s { - "allow" | "off" => Ok(Self::Allow), - "deny" | "error" => Ok(Self::Deny), - "warn" => Ok(Self::Warn), - _ => Err(OxcDiagnostic::error(format!( - r#"Failed to parse rule severity, expected one of "allow", "off", "deny", "error" or "warn", but got {s:?}"# - ))), - } - } -} - -impl TryFrom<&Value> for AllowWarnDeny { - type Error = OxcDiagnostic; - - fn try_from(value: &Value) -> Result { - match value { - Value::String(s) => Self::try_from(s.as_str()), - Value::Number(n) => Self::try_from(n), - _ => Err(OxcDiagnostic::error(format!( - "Failed to parse rule severity, expected a string or a number, but got {value:?}" - ))), - } - } -} - -impl TryFrom<&Number> for AllowWarnDeny { - type Error = OxcDiagnostic; - - fn try_from(value: &Number) -> Result { - match value.as_i64() { - Some(0) => Ok(Self::Allow), - Some(1) => Ok(Self::Warn), - Some(2) => Ok(Self::Deny), - _ => Err(OxcDiagnostic::error(format!( - r#"Failed to parse rule severity, expected one of `0`, `1` or `2`, but got {value:?}"# - ))), - } - } -} - -impl JsonSchema for AllowWarnDeny { - fn schema_name() -> String { - "AllowWarnDeny".to_string() - } - - fn schema_id() -> std::borrow::Cow<'static, str> { - "AllowWarnDeny".into() - } - - fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { - let mut string_schema = ::json_schema(gen).into_object(); - string_schema.enum_values = - Some(vec!["allow".into(), "off".into(), "warn".into(), "error".into(), "deny".into()]); - string_schema.metadata().description = Some( - r#"Oxlint rule. -- "allow" or "off": Turn off the rule. -- "warn": Turn the rule on as a warning (doesn't affect exit code). -- "error" or "deny": Turn the rule on as an error (will exit with a failure code)."# - .to_string(), - ); - let mut int_schema = ::json_schema(gen).into_object(); - int_schema.number().minimum = Some(0.0); - int_schema.number().maximum = Some(2.0); - int_schema.metadata().description = Some( - "Oxlint rule. - -- 0: Turn off the rule. -- 1: Turn the rule on as a warning (doesn't affect exit code). -- 2: Turn the rule on as an error (will exit with a failure code)." - .to_string(), - ); - - let mut schema = SchemaObject::default(); - schema.subschemas().one_of = Some(vec![string_schema.into(), int_schema.into()]); - - schema.into() - } -} - -impl From for Severity { - fn from(value: AllowWarnDeny) -> Self { - match value { - AllowWarnDeny::Allow => Self::Advice, - AllowWarnDeny::Warn => Self::Warning, - AllowWarnDeny::Deny => Self::Error, - } - } -} - impl LintOptions { /// # Errors /// diff --git a/crates/oxc_linter/src/options/plugins.rs b/crates/oxc_linter/src/options/plugins.rs new file mode 100644 index 00000000000000..d5b78e81035566 --- /dev/null +++ b/crates/oxc_linter/src/options/plugins.rs @@ -0,0 +1,79 @@ +#[derive(Debug, Clone)] +#[non_exhaustive] +pub struct LintPluginOptions { + /// On by default. + pub react: bool, + /// On by default. + pub unicorn: bool, + /// On by default. + pub typescript: bool, + /// On by default. + pub oxc: bool, + pub import: bool, + pub jsdoc: bool, + pub jest: bool, + pub vitest: bool, + pub jsx_a11y: bool, + pub nextjs: bool, + pub react_perf: bool, + pub promise: bool, +} + +impl Default for LintPluginOptions { + fn default() -> Self { + Self { + react: true, + unicorn: true, + typescript: true, + oxc: true, + import: false, + jsdoc: false, + jest: false, + vitest: false, + jsx_a11y: false, + nextjs: false, + react_perf: false, + promise: false, + } + } +} + +impl LintPluginOptions { + /// Create a new instance with all plugins disabled. + #[must_use] + pub fn none() -> Self { + Self { + react: false, + unicorn: false, + typescript: false, + oxc: false, + import: false, + jsdoc: false, + jest: false, + vitest: false, + jsx_a11y: false, + nextjs: false, + react_perf: false, + promise: false, + } + } + + /// Create a new instance with all plugins enabled. + #[must_use] + pub fn all() -> Self { + Self { + react: true, + unicorn: true, + typescript: true, + oxc: true, + import: true, + jsdoc: true, + jest: true, + vitest: true, + jsx_a11y: true, + nextjs: true, + react_perf: true, + promise: true, + } + } +}