refactor(linter): split options into multiple files (#5101)

This commit is contained in:
DonIsaac 2024-08-23 18:55:57 +00:00
parent 56a68f542d
commit 33599b0064
3 changed files with 201 additions and 187 deletions

View file

@ -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<Self, Self::Error> {
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<Self, OxcDiagnostic> {
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<Self, Self::Error> {
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 = <String as JsonSchema>::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 = <u32 as JsonSchema>::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<AllowWarnDeny> for Severity {
fn from(value: AllowWarnDeny) -> Self {
match value {
AllowWarnDeny::Allow => Self::Advice,
AllowWarnDeny::Warn => Self::Warning,
AllowWarnDeny::Deny => Self::Error,
}
}
}

View file

@ -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<Self, Self::Error> {
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<Self, OxcDiagnostic> {
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<Self, Self::Error> {
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 = <String as JsonSchema>::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 = <u32 as JsonSchema>::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<AllowWarnDeny> for Severity {
fn from(value: AllowWarnDeny) -> Self {
match value {
AllowWarnDeny::Allow => Self::Advice,
AllowWarnDeny::Warn => Self::Warning,
AllowWarnDeny::Deny => Self::Error,
}
}
}
impl LintOptions {
/// # Errors
///

View file

@ -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,
}
}
}