Skip to content

Commit fa20d50

Browse files
committed
refactor(apps): Use the same generate_schema_json function
1 parent 656da7b commit fa20d50

File tree

13 files changed

+1389
-747
lines changed

13 files changed

+1389
-747
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/oxfmt/src/core/oxfmtrc.rs

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use schemars::{JsonSchema, schema_for};
1+
use schemars::JsonSchema;
22
use serde::{Deserialize, Serialize};
33
use serde_json::Value;
44

@@ -813,86 +813,6 @@ pub fn populate_prettier_config(options: &FormatOptions, config: &mut Value) {
813813

814814
// ---
815815

816-
impl Oxfmtrc {
817-
/// Generates the JSON schema for Oxfmtrc configuration files.
818-
///
819-
/// # Panics
820-
/// Panics if the schema generation fails.
821-
pub fn generate_schema_json() -> String {
822-
let mut schema = schema_for!(Oxfmtrc);
823-
824-
// Allow comments and trailing commas for vscode-json-languageservice
825-
// NOTE: This is NOT part of standard JSON Schema specification
826-
// https://github.com/microsoft/vscode-json-languageservice/blob/fb83547762901f32d8449d57e24666573016b10c/src/jsonLanguageTypes.ts#L151-L159
827-
schema.schema.extensions.insert("allowComments".to_string(), serde_json::Value::Bool(true));
828-
schema
829-
.schema
830-
.extensions
831-
.insert("allowTrailingCommas".to_string(), serde_json::Value::Bool(true));
832-
833-
// Inject markdownDescription fields for better editor support (e.g., VS Code)
834-
let mut json = serde_json::to_value(&schema).unwrap();
835-
Self::inject_markdown_descriptions(&mut json);
836-
837-
// Sort keys for deterministic output across different environments.
838-
// Without this, CI and local environments may produce different key orders,
839-
// causing snapshot tests to fail.
840-
let sorted_json = Self::sort_json_keys(&json);
841-
842-
serde_json::to_string_pretty(&sorted_json).unwrap()
843-
}
844-
845-
/// Recursively sort all object keys in the JSON value for deterministic output.
846-
fn sort_json_keys(value: &serde_json::Value) -> serde_json::Value {
847-
match value {
848-
serde_json::Value::Object(map) => {
849-
let mut sorted: Vec<_> = map.iter().collect();
850-
sorted.sort_by(|(a, _), (b, _)| a.cmp(b));
851-
serde_json::Value::Object(
852-
sorted.into_iter().map(|(k, v)| (k.clone(), Self::sort_json_keys(v))).collect(),
853-
)
854-
}
855-
serde_json::Value::Array(arr) => {
856-
serde_json::Value::Array(arr.iter().map(Self::sort_json_keys).collect())
857-
}
858-
_ => value.clone(),
859-
}
860-
}
861-
862-
/// Recursively inject `markdownDescription` fields into the JSON schema.
863-
/// This is a non-standard field that some editors (like VS Code) use to render
864-
/// markdown in hover tooltips.
865-
fn inject_markdown_descriptions(value: &mut serde_json::Value) {
866-
match value {
867-
serde_json::Value::Object(map) => {
868-
// If this object has a `description` field, copy it to `markdownDescription`
869-
if let Some(serde_json::Value::String(desc_str)) = map.get("description") {
870-
map.insert(
871-
"markdownDescription".to_string(),
872-
serde_json::Value::String(desc_str.clone()),
873-
);
874-
}
875-
876-
// Recursively process all values in the object
877-
for value in map.values_mut() {
878-
Self::inject_markdown_descriptions(value);
879-
}
880-
}
881-
serde_json::Value::Array(items) => {
882-
// Recursively process all items in the array
883-
for item in items {
884-
Self::inject_markdown_descriptions(item);
885-
}
886-
}
887-
_ => {
888-
// Primitive values don't need processing
889-
}
890-
}
891-
}
892-
}
893-
894-
// ---
895-
896816
#[cfg(test)]
897817
mod tests {
898818
use super::*;

crates/oxc_linter/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,3 @@ url = { workspace = true }
7777
[dev-dependencies]
7878
insta = { workspace = true }
7979
markdown = { workspace = true }
80-
project-root = { workspace = true }

crates/oxc_linter/src/config/oxlintrc.rs

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
};
55

66
use rustc_hash::{FxHashMap, FxHashSet};
7-
use schemars::{JsonSchema, schema_for};
7+
use schemars::JsonSchema;
88
use serde::{Deserialize, Serialize};
99

1010
use oxc_diagnostics::OxcDiagnostic;
@@ -209,61 +209,6 @@ impl Oxlintrc {
209209
})
210210
}
211211

212-
/// Generates the JSON schema for Oxlintrc configuration files.
213-
///
214-
/// # Panics
215-
/// Panics if the schema generation fails.
216-
pub fn generate_schema_json() -> String {
217-
let mut schema = schema_for!(Oxlintrc);
218-
219-
// Allow comments and trailing commas for vscode-json-languageservice
220-
// NOTE: This is NOT part of standard JSON Schema specification
221-
// https://github.com/microsoft/vscode-json-languageservice/blob/fb83547762901f32d8449d57e24666573016b10c/src/jsonLanguageTypes.ts#L151-L159
222-
schema.schema.extensions.insert("allowComments".to_string(), serde_json::Value::Bool(true));
223-
schema
224-
.schema
225-
.extensions
226-
.insert("allowTrailingCommas".to_string(), serde_json::Value::Bool(true));
227-
228-
let mut json = serde_json::to_value(&schema).unwrap();
229-
230-
// Inject markdown descriptions for better editor support
231-
Self::inject_markdown_descriptions(&mut json);
232-
233-
serde_json::to_string_pretty(&json).unwrap()
234-
}
235-
236-
/// Recursively inject `markdownDescription` fields into the JSON schema.
237-
/// This is a non-standard field that some editors (like VS Code) use to render
238-
/// markdown in hover tooltips.
239-
fn inject_markdown_descriptions(value: &mut serde_json::Value) {
240-
match value {
241-
serde_json::Value::Object(map) => {
242-
// If this object has a `description` field, copy it to `markdownDescription`
243-
if let Some(serde_json::Value::String(desc_str)) = map.get("description") {
244-
map.insert(
245-
"markdownDescription".to_string(),
246-
serde_json::Value::String(desc_str.clone()),
247-
);
248-
}
249-
250-
// Recursively process all values in the object
251-
for value in map.values_mut() {
252-
Self::inject_markdown_descriptions(value);
253-
}
254-
}
255-
serde_json::Value::Array(items) => {
256-
// Recursively process all items in the array
257-
for item in items {
258-
Self::inject_markdown_descriptions(item);
259-
}
260-
}
261-
_ => {
262-
// Primitive values don't need processing
263-
}
264-
}
265-
}
266-
267212
/// Merges two [Oxlintrc] files together.
268213
///
269214
/// [Self] takes priority over `other` - if both configs define the same property,

crates/oxc_linter/src/lib.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -693,25 +693,3 @@ impl RawTransferMetadata {
693693
Self { data_offset, is_ts: false, _padding: 0 }
694694
}
695695
}
696-
697-
#[cfg(test)]
698-
mod test {
699-
use std::fs;
700-
701-
use project_root::get_project_root;
702-
703-
use crate::Oxlintrc;
704-
705-
#[test]
706-
fn test_schema_json() {
707-
let path = get_project_root().unwrap().join("npm/oxlint/configuration_schema.json");
708-
let json = Oxlintrc::generate_schema_json();
709-
let existing_json = fs::read_to_string(&path).unwrap_or_default();
710-
if existing_json.trim() != json.trim() {
711-
std::fs::write(&path, &json).unwrap();
712-
}
713-
insta::with_settings!({ prepend_module_to_snapshot => false }, {
714-
insta::assert_snapshot!(json);
715-
});
716-
}
717-
}

0 commit comments

Comments
 (0)