mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +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)]
|
#[derive(Debug, Clone, JsonSchema)]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
enum DummyRule {
|
enum DummyRule {
|
||||||
#[schemars(range(min = 0, max = 2.0))]
|
Toggle(AllowWarnDeny),
|
||||||
Number(usize),
|
ToggleAndConfig(Vec<serde_json::Value>),
|
||||||
String(String),
|
|
||||||
Array(Vec<serde_json::Value>),
|
|
||||||
}
|
}
|
||||||
gen.subschema_for::<FxHashMap<String, DummyRule>>()
|
gen.subschema_for::<FxHashMap<String, DummyRule>>()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ use std::{convert::From, path::PathBuf};
|
||||||
|
|
||||||
use oxc_diagnostics::{Error, OxcDiagnostic, Severity};
|
use oxc_diagnostics::{Error, OxcDiagnostic, Severity};
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::FxHashSet;
|
||||||
|
use schemars::{schema::SchemaObject, JsonSchema};
|
||||||
use serde_json::{Number, Value};
|
use serde_json::{Number, Value};
|
||||||
|
|
||||||
use crate::{
|
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 {
|
impl From<AllowWarnDeny> for Severity {
|
||||||
fn from(value: AllowWarnDeny) -> Self {
|
fn from(value: AllowWarnDeny) -> Self {
|
||||||
match value {
|
match value {
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,28 @@ expression: json
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"definitions": {
|
"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": {
|
"CustomComponent": {
|
||||||
"anyOf": [
|
"anyOf": [
|
||||||
{
|
{
|
||||||
|
|
@ -70,12 +92,7 @@ expression: json
|
||||||
"DummyRule": {
|
"DummyRule": {
|
||||||
"anyOf": [
|
"anyOf": [
|
||||||
{
|
{
|
||||||
"type": "integer",
|
"$ref": "#/definitions/AllowWarnDeny"
|
||||||
"format": "uint",
|
|
||||||
"minimum": 0.0
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "string"
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "array",
|
"type": "array",
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,28 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"definitions": {
|
"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": {
|
"CustomComponent": {
|
||||||
"anyOf": [
|
"anyOf": [
|
||||||
{
|
{
|
||||||
|
|
@ -66,12 +88,7 @@
|
||||||
"DummyRule": {
|
"DummyRule": {
|
||||||
"anyOf": [
|
"anyOf": [
|
||||||
{
|
{
|
||||||
"type": "integer",
|
"$ref": "#/definitions/AllowWarnDeny"
|
||||||
"format": "uint",
|
|
||||||
"minimum": 0.0
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "string"
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "array",
|
"type": "array",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue