Skip to content

Commit

Permalink
feat(linter): better schemas for allow/warn/deny (#4150)
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Jul 10, 2024
1 parent 48947a2 commit cc58614
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 17 deletions.
6 changes: 2 additions & 4 deletions crates/oxc_linter/src/config/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<serde_json::Value>),
Toggle(AllowWarnDeny),
ToggleAndConfig(Vec<serde_json::Value>),
}
gen.subschema_for::<FxHashMap<String, DummyRule>>()
}
Expand Down
40 changes: 40 additions & 0 deletions crates/oxc_linter/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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 = <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 {
Expand Down
29 changes: 23 additions & 6 deletions crates/oxc_linter/src/snapshots/schema_json.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down Expand Up @@ -70,12 +92,7 @@ expression: json
"DummyRule": {
"anyOf": [
{
"type": "integer",
"format": "uint",
"minimum": 0.0
},
{
"type": "string"
"$ref": "#/definitions/AllowWarnDeny"
},
{
"type": "array",
Expand Down
31 changes: 24 additions & 7 deletions npm/oxlint/configuration_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down Expand Up @@ -66,12 +88,7 @@
"DummyRule": {
"anyOf": [
{
"type": "integer",
"format": "uint",
"minimum": 0.0
},
{
"type": "string"
"$ref": "#/definitions/AllowWarnDeny"
},
{
"type": "array",
Expand Down Expand Up @@ -264,4 +281,4 @@
]
}
}
}
}

0 comments on commit cc58614

Please sign in to comment.