From 4d28d037e674013365b19ff80b76c6912e319d07 Mon Sep 17 00:00:00 2001 From: mysteryven <33973865+mysteryven@users.noreply.github.com> Date: Wed, 14 Aug 2024 06:12:02 +0000 Subject: [PATCH] feat(task/website): support render `subschemas.all_of` (#4800) To solve unexpected snapshot update in #4742 : https://github.com/oxc-project/oxc/pull/4742/files#diff-4c1a03dc03cfd00f9eaecb5c66b61f4e57272c6262253f0dc82ea1b71223bac2 --- crates/oxc_linter/src/config/rules.rs | 9 ++++- .../oxc_linter/src/snapshots/schema_json.snap | 12 ++++--- npm/oxlint/configuration_schema.json | 12 ++++--- tasks/website/src/linter/json_schema.rs | 35 +++++++++++++++++-- .../src/linter/snapshots/schema_markdown.snap | 1 + 5 files changed, 57 insertions(+), 12 deletions(-) diff --git a/crates/oxc_linter/src/config/rules.rs b/crates/oxc_linter/src/config/rules.rs index d70da6f072810..67570f3c1d99b 100644 --- a/crates/oxc_linter/src/config/rules.rs +++ b/crates/oxc_linter/src/config/rules.rs @@ -14,6 +14,7 @@ use crate::AllowWarnDeny; // - type SeverityConf = 0 | 1 | 2 | "off" | "warn" | "error"; // - type RuleConf = SeverityConf | [SeverityConf, ...any[]]; // +// Note: when update document comment, also update `DummyRuleMap`'s description in this file. #[derive(Debug, Clone, Default)] pub struct OxlintRules(Vec); @@ -42,7 +43,13 @@ impl JsonSchema for OxlintRules { Toggle(AllowWarnDeny), ToggleAndConfig(Vec), } - gen.subschema_for::>() + + #[allow(unused)] + #[derive(Debug, JsonSchema)] + #[schemars(description = "See [Oxlint Rules](./rules)")] + struct DummyRuleMap(pub FxHashMap); + + gen.subschema_for::() } } diff --git a/crates/oxc_linter/src/snapshots/schema_json.snap b/crates/oxc_linter/src/snapshots/schema_json.snap index 1005698068189..929e03c8408c2 100644 --- a/crates/oxc_linter/src/snapshots/schema_json.snap +++ b/crates/oxc_linter/src/snapshots/schema_json.snap @@ -100,6 +100,13 @@ expression: json } ] }, + "DummyRuleMap": { + "description": "See [Oxlint Rules](./rules)", + "type": "object", + "additionalProperties": { + "$ref": "#/definitions/DummyRule" + } + }, "GlobalValue": { "type": "string", "enum": [ @@ -208,10 +215,7 @@ expression: json } }, "OxlintRules": { - "type": "object", - "additionalProperties": { - "$ref": "#/definitions/DummyRule" - } + "$ref": "#/definitions/DummyRuleMap" }, "OxlintSettings": { "description": "Shared settings for plugins", diff --git a/npm/oxlint/configuration_schema.json b/npm/oxlint/configuration_schema.json index f87fc078da0d5..c3d4ea510056a 100644 --- a/npm/oxlint/configuration_schema.json +++ b/npm/oxlint/configuration_schema.json @@ -96,6 +96,13 @@ } ] }, + "DummyRuleMap": { + "description": "See [Oxlint Rules](./rules)", + "type": "object", + "additionalProperties": { + "$ref": "#/definitions/DummyRule" + } + }, "GlobalValue": { "type": "string", "enum": [ @@ -204,10 +211,7 @@ } }, "OxlintRules": { - "type": "object", - "additionalProperties": { - "$ref": "#/definitions/DummyRule" - } + "$ref": "#/definitions/DummyRuleMap" }, "OxlintSettings": { "description": "Shared settings for plugins", diff --git a/tasks/website/src/linter/json_schema.rs b/tasks/website/src/linter/json_schema.rs index 3f31232965a6f..b42d138546a43 100644 --- a/tasks/website/src/linter/json_schema.rs +++ b/tasks/website/src/linter/json_schema.rs @@ -1,7 +1,7 @@ use handlebars::Handlebars; use oxc_linter::OxlintConfig; use schemars::{ - schema::{RootSchema, Schema, SchemaObject, SingleOrVec}, + schema::{RootSchema, Schema, SchemaObject, SingleOrVec, SubschemaValidation}, schema_for, }; use serde::Serialize; @@ -140,12 +140,41 @@ impl Renderer { return object .properties .iter() - .map(|(key, schema)| { + .flat_map(|(key, schema)| { let key = parent_key.map_or_else(|| key.clone(), |k| format!("{k}.{key}")); - self.render_schema(depth + 1, &key, Self::get_schema_object(schema)) + let schema_object = Self::get_schema_object(schema); + + if let Some(subschemas) = &schema_object.subschemas { + return self.render_sub_schema(depth, &key, subschemas); + } + + vec![self.render_schema(depth + 1, &key, schema_object)] }) .collect::>(); } + if let Some(subschemas) = &schema.subschemas { + let key = parent_key.unwrap_or(""); + return self.render_sub_schema(depth, key, subschemas); + } + vec![] + } + + fn render_sub_schema( + &self, + depth: usize, + key: &str, + subschemas: &SubschemaValidation, + ) -> Vec
{ + if let Some(schemas) = &subschemas.all_of { + return schemas + .iter() + .map(|schema| { + let schema = Self::get_schema_object(schema); + let schema = self.get_referenced_schema(schema); + self.render_schema(depth + 1, key, schema) + }) + .collect::>(); + } vec![] } diff --git a/tasks/website/src/linter/snapshots/schema_markdown.snap b/tasks/website/src/linter/snapshots/schema_markdown.snap index 819563aa57524..ae6f0c29ab6a0 100644 --- a/tasks/website/src/linter/snapshots/schema_markdown.snap +++ b/tasks/website/src/linter/snapshots/schema_markdown.snap @@ -52,6 +52,7 @@ Add or remove global variables. ## rules +type: `object` See [Oxlint Rules](./rules)