Skip to content

Commit

Permalink
feat(task/website): support render subschemas.all_of (#4800)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysteryven committed Aug 14, 2024
1 parent e8de4bd commit 4d28d03
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 12 deletions.
9 changes: 8 additions & 1 deletion crates/oxc_linter/src/config/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::AllowWarnDeny;
// - type SeverityConf = 0 | 1 | 2 | "off" | "warn" | "error";
// - type RuleConf = SeverityConf | [SeverityConf, ...any[]];
// <https://github.com/eslint/eslint/blob/ce838adc3b673e52a151f36da0eedf5876977514/lib/shared/types.js#L12>
// Note: when update document comment, also update `DummyRuleMap`'s description in this file.
#[derive(Debug, Clone, Default)]
pub struct OxlintRules(Vec<ESLintRule>);

Expand Down Expand Up @@ -42,7 +43,13 @@ impl JsonSchema for OxlintRules {
Toggle(AllowWarnDeny),
ToggleAndConfig(Vec<serde_json::Value>),
}
gen.subschema_for::<FxHashMap<String, DummyRule>>()

#[allow(unused)]
#[derive(Debug, JsonSchema)]
#[schemars(description = "See [Oxlint Rules](./rules)")]
struct DummyRuleMap(pub FxHashMap<String, DummyRule>);

gen.subschema_for::<DummyRuleMap>()
}
}

Expand Down
12 changes: 8 additions & 4 deletions crates/oxc_linter/src/snapshots/schema_json.snap
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ expression: json
}
]
},
"DummyRuleMap": {
"description": "See [Oxlint Rules](./rules)",
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/DummyRule"
}
},
"GlobalValue": {
"type": "string",
"enum": [
Expand Down Expand Up @@ -208,10 +215,7 @@ expression: json
}
},
"OxlintRules": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/DummyRule"
}
"$ref": "#/definitions/DummyRuleMap"
},
"OxlintSettings": {
"description": "Shared settings for plugins",
Expand Down
12 changes: 8 additions & 4 deletions npm/oxlint/configuration_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@
}
]
},
"DummyRuleMap": {
"description": "See [Oxlint Rules](./rules)",
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/DummyRule"
}
},
"GlobalValue": {
"type": "string",
"enum": [
Expand Down Expand Up @@ -204,10 +211,7 @@
}
},
"OxlintRules": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/DummyRule"
}
"$ref": "#/definitions/DummyRuleMap"
},
"OxlintSettings": {
"description": "Shared settings for plugins",
Expand Down
35 changes: 32 additions & 3 deletions tasks/website/src/linter/json_schema.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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::<Vec<_>>();
}
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<Section> {
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<Section>>();
}
vec![]
}

Expand Down
1 change: 1 addition & 0 deletions tasks/website/src/linter/snapshots/schema_markdown.snap
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Add or remove global variables.

## rules

type: `object`

See [Oxlint Rules](./rules)

Expand Down

0 comments on commit 4d28d03

Please sign in to comment.