mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
feat(linter): better schemas for allow/warn/deny (#4150)
This commit is contained in:
parent
48947a26d2
commit
cc586145ab
4 changed files with 89 additions and 17 deletions
|
|
@ -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>>()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 = <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 {
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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 @@
|
|||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue