Skip to content

Commit e81587d

Browse files
authored
chore(linter): Add just task for oxlint config schema generation (#15539)
This was being duplicated in multiple places, so I moved `generate_json_schema` into Oxlintrc to avoid needing to keep those two places in sync. `just linter-schema-json` generates the configuration_schema.json so you don't have to run the tests to regenerate it, which was a very confusing behavior to figure out as a contributor. However, this does mean this is now part of the public interface of the create, so maybe it shouldn't be like this?
1 parent 5a61189 commit e81587d

File tree

5 files changed

+21
-14
lines changed

5 files changed

+21
-14
lines changed

crates/oxc_linter/src/config/oxlintrc.rs

Lines changed: 13 additions & 1 deletion
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;
7+
use schemars::{JsonSchema, schema_for};
88
use serde::{Deserialize, Deserializer, Serialize, Serializer};
99

1010
use oxc_diagnostics::OxcDiagnostic;
@@ -198,6 +198,18 @@ impl Oxlintrc {
198198
})
199199
}
200200

201+
/// Generates the JSON schema for Oxlintrc configuration files.
202+
///
203+
/// # Panics
204+
/// Panics if the schema generation fails.
205+
pub fn generate_schema_json() -> String {
206+
let mut schema = schema_for!(Oxlintrc);
207+
// setting `allowComments` to true to allow comments in JSON schema files
208+
// https://github.com/microsoft/vscode-json-languageservice/blob/356d5dd980d49c6ac09ec8446614a6f94016dcea/src/jsonLanguageTypes.ts#L127-L131
209+
schema.schema.extensions.insert("allowComments".to_string(), serde_json::Value::Bool(true));
210+
serde_json::to_string_pretty(&schema).unwrap()
211+
}
212+
201213
/// Merges two [Oxlintrc] files together
202214
/// [Self] takes priority over `other`
203215
#[must_use]

crates/oxc_linter/src/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -579,16 +579,12 @@ mod test {
579579

580580
use project_root::get_project_root;
581581

582-
use super::Oxlintrc;
582+
use crate::Oxlintrc;
583583

584584
#[test]
585585
fn test_schema_json() {
586586
let path = get_project_root().unwrap().join("npm/oxlint/configuration_schema.json");
587-
let mut schema = schemars::schema_for!(Oxlintrc);
588-
// setting `allowComments` to true to allow comments in JSON schema files
589-
// https://github.com/microsoft/vscode-json-languageservice/blob/356d5dd980d49c6ac09ec8446614a6f94016dcea/src/jsonLanguageTypes.ts#L127-L131
590-
schema.schema.extensions.insert("allowComments".to_string(), serde_json::Value::Bool(true));
591-
let json = serde_json::to_string_pretty(&schema).unwrap();
587+
let json = Oxlintrc::generate_schema_json();
592588
let existing_json = fs::read_to_string(&path).unwrap_or_default();
593589
if existing_json.trim() != json.trim() {
594590
std::fs::write(&path, &json).unwrap();

justfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ website path:
248248
cargo run -p website -- linter-cli > {{path}}/src/docs/guide/usage/linter/generated-cli.md
249249
cargo run -p website -- linter-schema-markdown > {{path}}/src/docs/guide/usage/linter/generated-config.md
250250

251+
# Generate linter schema json for `npm/oxlint/configuration_schema.json`
252+
linter-schema-json:
253+
cargo run -p website -- linter-schema-json > npm/oxlint/configuration_schema.json
254+
251255
# Automatically DRY up Cargo.toml manifests in a workspace
252256
autoinherit:
253257
cargo binstall cargo-autoinherit

npm/oxlint/configuration_schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,4 +620,4 @@
620620
}
621621
}
622622
}
623-
}
623+
}

tasks/website/src/linter/json_schema.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ use schemars::{
88
use serde::Serialize;
99

1010
pub fn print_schema_json() {
11-
println!("{}", generate_schema_json());
12-
}
13-
14-
fn generate_schema_json() -> String {
15-
let schema = schema_for!(Oxlintrc);
16-
serde_json::to_string_pretty(&schema).unwrap()
11+
println!("{}", Oxlintrc::generate_schema_json());
1712
}
1813

1914
#[test]

0 commit comments

Comments
 (0)