diff --git a/crates/oxc_linter/src/config/rules.rs b/crates/oxc_linter/src/config/rules.rs index f1dc72cebb962..d70da6f072810 100644 --- a/crates/oxc_linter/src/config/rules.rs +++ b/crates/oxc_linter/src/config/rules.rs @@ -39,10 +39,8 @@ impl JsonSchema for OxlintRules { #[derive(Debug, Clone, JsonSchema)] #[serde(untagged)] enum DummyRule { - #[schemars(range(min = 0, max = 2.0))] - Number(usize), - String(String), - Array(Vec), + Toggle(AllowWarnDeny), + ToggleAndConfig(Vec), } gen.subschema_for::>() } diff --git a/crates/oxc_linter/src/options.rs b/crates/oxc_linter/src/options.rs index 258ec0d2167df..a894dcdc2510a 100644 --- a/crates/oxc_linter/src/options.rs +++ b/crates/oxc_linter/src/options.rs @@ -2,6 +2,7 @@ use std::{convert::From, path::PathBuf}; use oxc_diagnostics::{Error, OxcDiagnostic, Severity}; use rustc_hash::FxHashSet; +use schemars::{schema::SchemaObject, JsonSchema}; use serde_json::{Number, Value}; use crate::{ @@ -200,6 +201,45 @@ impl TryFrom<&Number> for AllowWarnDeny { } } +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 { diff --git a/crates/oxc_linter/src/snapshots/schema_json.snap b/crates/oxc_linter/src/snapshots/schema_json.snap index 13685991c9f7b..fe08174cde260 100644 --- a/crates/oxc_linter/src/snapshots/schema_json.snap +++ b/crates/oxc_linter/src/snapshots/schema_json.snap @@ -27,6 +27,28 @@ expression: json } }, "definitions": { + "AllowWarnDeny": { + "oneOf": [ + { + "description": "Oxlint rule.\n- \"allow\" or \"off\": Turn off the rule.\n- \"warn\": Turn the rule on as a warning (doesn't affect exit code).\n- \"error\" or \"deny\": Turn the rule on as an error (will exit with a failure code).", + "type": "string", + "enum": [ + "allow", + "off", + "warn", + "error", + "deny" + ] + }, + { + "description": "Oxlint rule.\n \n- 0: Turn off the rule.\n- 1: Turn the rule on as a warning (doesn't affect exit code).\n- 2: Turn the rule on as an error (will exit with a failure code).", + "type": "integer", + "format": "uint32", + "maximum": 2.0, + "minimum": 0.0 + } + ] + }, "CustomComponent": { "anyOf": [ { @@ -70,12 +92,7 @@ expression: json "DummyRule": { "anyOf": [ { - "type": "integer", - "format": "uint", - "minimum": 0.0 - }, - { - "type": "string" + "$ref": "#/definitions/AllowWarnDeny" }, { "type": "array", diff --git a/npm/oxlint/configuration_schema.json b/npm/oxlint/configuration_schema.json index 8af6c5eee5777..c8b227b86b113 100644 --- a/npm/oxlint/configuration_schema.json +++ b/npm/oxlint/configuration_schema.json @@ -23,6 +23,28 @@ } }, "definitions": { + "AllowWarnDeny": { + "oneOf": [ + { + "description": "Oxlint rule.\n- \"allow\" or \"off\": Turn off the rule.\n- \"warn\": Turn the rule on as a warning (doesn't affect exit code).\n- \"error\" or \"deny\": Turn the rule on as an error (will exit with a failure code).", + "type": "string", + "enum": [ + "allow", + "off", + "warn", + "error", + "deny" + ] + }, + { + "description": "Oxlint rule.\n \n- 0: Turn off the rule.\n- 1: Turn the rule on as a warning (doesn't affect exit code).\n- 2: Turn the rule on as an error (will exit with a failure code).", + "type": "integer", + "format": "uint32", + "maximum": 2.0, + "minimum": 0.0 + } + ] + }, "CustomComponent": { "anyOf": [ { @@ -66,12 +88,7 @@ "DummyRule": { "anyOf": [ { - "type": "integer", - "format": "uint", - "minimum": 0.0 - }, - { - "type": "string" + "$ref": "#/definitions/AllowWarnDeny" }, { "type": "array", @@ -264,4 +281,4 @@ ] } } -} \ No newline at end of file +}